循环和增加值

我试图运行一个循环,在列中寻找一个值,如果它被发现然后另一个值input右边的单元格中。

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 

我发现的是脚本运行,似乎并没有确定何时单元格中包含“地区”一词。 它正在跳过,好像电池是空的。

对于VBA来说还是很新的,所以这可能或者不是一个简单的解决scheme。

谢谢!

尝试这个:

 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 

编辑1: 如评论中所述

 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