为有限的代码表示歉意,但我认为我不知所措,需要一些帮助!
我希望循环浏览工作表1上一列的每一行,以匹配两个单元格(工作表1中的C列中的单元格与工作表2中的E列中的单元格)。如果有匹配项,我想对相邻的单元格执行操作(稍后再处理),然后恢复匹配循环,直到没有更多匹配项为止。
到目前为止,我几乎没有刮擦过表面,并进行了以下测试:
Sub Test3()
Dim x As String
Dim found As Boolean
' Select first line of data.
Worksheets("PLANNER_ONGOING_DISPLAY_SHEET").Activate
Range("C4").Select
' Set search variable value.
x = "test 73"
' Set Boolean variable "found" to false.
found = False
' Set Do loop to stop at empty cell.
Do Until IsEmpty(ActiveCell)
' Check active cell for search value.
If ActiveCell.Value = x Then
found = True
Exit Do
End If
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
' Check for found.
If found = True Then
MsgBox "Value found in cell " & ActiveCell.Address
Else
MsgBox "Value not found"
End If
End Sub
有人可以帮忙吗?对于缺少代码我再次表示歉意,对此我还是很陌生!
谢谢:)
答案 0 :(得分:0)
您可以采用更简单的方法,同时使用avoiding .Activate
and .Select
:
Sub Test3()
Dim x As String, y As String
Dim found As Boolean
Dim i As Integer, lastRow As Long
lastRow = Worksheets("PLANNER_ONGOING_DISPLAY_SHEET").Cells(Rows.Count, 3).End(xlUp).Row
x = "test 73"
For i = 4 To lastRow
'If you need to compare it to a cell in sheet2 you can also set the value of x here
y = Worksheets("PLANNER_ONGOING_DISPLAY_SHEET").Cells(i, 3).Value2
If y = x Then
' Do action here, example:
MsgBox "Value found in cell " & "C" & i
End If
Next i
End Sub
这将遍历列C
中所有使用的单元格,并将它们与x进行比较。如果一个单元格匹配,则其行号通过MsgBox
返回。