excel vba中find找数据,为什么只找一次啊
发布网友
发布时间:2022-05-02 16:13
我来回答
共3个回答
热心网友
时间:2022-06-20 20:14
是你的逻辑设计的原因,你的find()方法在循环体之外。你看一下你的结构:
Set findCell = Sheet1.Columns(coldw).Find(str2)
if then
do
for
for
.........
next
next
loop
endif
把它设置到循环体之内去就不会只找一次了。
热心网友
时间:2022-06-20 20:14
你是想在某列查找str2吧,必须加上Findnext属性,即查找下一个:
举例:
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
此例在a1:a500查找数值为2的单元格,并将单元格的值重新赋值为5
热心网友
时间:2022-06-20 20:15
Columns和Ranges都可以用findnext
Set findCell = Sheet1.Columns(coldw).Find(str2)
If Not findCell Is Nothing Then
firstAddress = findCell.Address
Do
dayInRow = findCell.Row
For i = 1 To day
mydate = DateSerial(year, month, i)
Sheet15.Cells(i + 1, 1) = mydate
j = 1
For lst = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lst) = True Then
str1 = ListBox1.List(lst)
Name1 = Left(str1, InStr(str1, "&") - 1)
col = Right(str1, Len(str1) - Len(Name1) - 1)
If CDate(Sheet1.Cells(dayInRow, col)) < mydate Then
Sheet15.Cells(i + 1, j + 1) = Sheet15.Cells(i + 1, j + 1) + 1
End If
j = j + 1
End If
Next
Next
set findCell = Sheet1.Columns(coldw).FindNext(findCell)
Loop While Not findCell Is Nothing And firstAddress <> findCell.Address
End If