下面是我的代码,它在 excel 中使用 .Find 函数来查找 dodCell 在 sheet2 中出现的位置,并将 reeCell 从 sheet1 添加到 sheet2 中的第 18 列。这假设根据它在 rRange 中找到 strSearch 的次数来循环。
但是,它目前只运行一次并停止,我认为我的“Do While Loop”中有一个错误,但我无法修复它。
有什么想法吗?
因此,在修复了我的代码中指出的一些错误之后,我修改了 sub. 我想我已经解决了循环问题,但知道程序运行一次并且正在冻结 excel,然后我需要重新启动 Excel。我想我已经创建了一个无限循环,但不知道如何解决它的任何想法?
Sub addnumber()
'used to add ree value to Dod projects
Dim sSht As Worksheet, dSht As Worksheet
Dim lastrow As Integer
Dim firstAddress As String
Dim strSearch As String
Dim ReeCell As Range, dodCell As Range, aRange As Range, rRange As Range, aaRange As Range
Dim hold1Cell As Range, holdCell As Range, lastCell As Range
Set sSht = Worksheets("Sheet1")
Set dSht = Worksheets("Sheet2")
Set rRange = sSht.Columns(18)
Set aRange = sSht.Columns(1)
Set aaRange = dSht.Columns(1)
lastrow = sSht.Range("A" & Rows.Count).End(xlUp).Row
strSearch = "2*"
Set dodCell = rRange.Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'If something dodCell holds a value then enter loop
If Not dodCell Is Nothing Then
'Set lastCell to dodCell
firstAddress = dodCell.Address
Do
'Set ReeCell to the value of the Ree number
Set ReeCell = dodCell.Offset(0, -17)
'Set holdCell to the Cell that holds that Dod number in "Sheet2"
Set holdCell = aaRange.Find(What:=dodCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'Set hold1Cell to the location that the Ree Value should be
Set hold1Cell = holdCell.Offset(0, 9)
'Give hold1Cell the Ree # from ReeCell
hold1Cell = ReeCell.Value
Set dodCell = rRange.FindNext(dodCell)
Loop While Not dodCell Is Nothing And dodCell.Address <> firstAddress
End If
End Sub