我正在研究 Excel 宏。当我从另一个 Excel 工作表获取数据时,我需要什么,代码应该首先检查是否有任何其他行具有相同的 FundName,如果找到,则条件适用。
我只是给出要检查 FundId 的 Excel 工作表示例:
S.No Funds
1 A
2 B
3 C
4 D
5 A
代码如下:
Set shtData = wbraw.Sheets(1) ' this line is correct
Set CCell = shtData.Cells.Find("Funds", LookIn:=xlValues, LookAt:=xlWhole).Offset(1, 0)
Set DCell = CCell.End(xlDown)
Dim SearchString as String
SearchString = "A"
Set FindRow = shtData.Range(CCell, DCell).Find(What:=SearchString, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Set NextRow = shtData.Range(CCell, DCell).FindNext(After:=FindRow)
上面两行代码没有按照我想要的方式工作。假设如果 SearchString 设置为“A”,则 FindRow 和 NextRow 都应该具有该值。如果 SearchString 设置为“B”,那么根据给定的 Excel 工作表 FindRow 应该具有该值,但 NextRow 返回 Nothing,以便我可以应用我的条件。
如果有人可以帮助我,请。
Find
将使用第一个单元格Range
为了After
参数,如果不指定,则从B2之后开始搜索,因此找到的第一个单元格是B6。
如果订单对您很重要,请致电Find
最后一个单元格提供为After
:
Dim counter As Integer
counter = 0
With shtData.Range(CCell, DCell)
Set c = .Find(SearchString, LookIn:=xlValues, LookAt:=xlWhole, After:=DCell)
If Not c Is Nothing Then
firstAddress = c.Address
Do
counter = counter + 1
Debug.Print "The next match #" & counter & " is " & c.Address
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)