我正在尝试运行一个在列中查找值的循环,如果找到它,则在其右侧的单元格中输入另一个值。
m = 2
h = 1
Cells(m, 23).Select
Do
Cells(m, 23).Select
If ActiveCells <> " " Then
Cells(m, 24) = "Test"
End If
If InStr(Cells(m, 24).Text, "-") Then
h = h + 1
End If
m = m + 1
我发现脚本运行并且当单元格包含单词“Region”时似乎无法识别。它只是跳过,好像细胞是空的。
对于VBA来说还是一个新手,所以这可能是也可能不是一个简单的修复。
谢谢!
答案 0 :(得分:1)
试试这个:
Dim lr As Long
Dim m As Long: m = 2
Dim h As Long: h = 1
'Properly reference objects
With Sheets("YourActualSheetName")
'To add better control, identify boundaries of your loop,
'so find the last row that contain data.
lr = .Columns(23).Find(What:="*", _
After:=.Cells(1, 23), _
SearchDirection:=xlPrevious).Row
Do
'You can use one liner If's for some cases like this one
If .Cells(m, 23) = "Region" Then .Cells(m, 24) = "Type"
If InStr(.Cells(m, 24), "-") <> 0 Then h = h + 1
m = m + 1
Loop Until m > lr
End With
Edit1: 正如评论中所述
Dim m As Long: m = 2
Dim h As Long: h = 1
With Sheets("YourActualSheetName")
Do
If .Cells(m, 23) = "Region" Then .Cells(m, 24) = "Type"
If InStr(.Cells(m, 24), "-") <> 0 Then h = h + 1
m = m + 1
Loop Until h = 9
End With