我有一张包含许多工作表的工作簿,如果前面的单元格与给定的字符串匹配,我需要输入一个值。
我的代码适用于我需要它的工作表,但是当它到达一些不需要的工作表(其中也有数据)时会出错。
调试器突出显示的行是For Each r In Intersect (ActiveSheet.UsedRange, Range("F:F")
我是VBA / Excel脚本的新手。很抱歉,如果这是非常明显的,但我搜索了网站,找不到合适的答案,或者我只是不认识它。
Sub AllOnePool()
Dim myStr As String
Dim myPool As String
Dim sh As Worksheet
Dim xlCalc As XlCalculation
myStr = InputBox(Prompt:="Input the Target Serial Number: e.g. 93127")
myPool = InputBox(Prompt:="Input the Pool to Use: ")
For Each sh In ActiveWorkbook.Worksheets
sh.Activate
For Each r In Intersect(ActiveSheet.UsedRange, Range("F:F"))
If r.Text = myStr Then
r.Offset(0, 1) = myPool
End If
Next r
Next sh
End Sub
答案 0 :(得分:4)
您需要检查2个范围是否相交:
Sub AllOnePool()
Dim myStr As String
Dim myPool As String
Dim sh As Worksheet
Dim cel As Range
Dim xIng As Range
myStr = InputBox(Prompt:="Input the Target Serial Number: i.e. 93127")
myPool = InputBox(Prompt:="Input the Pool to Use: ")
For Each sh In ActiveWorkbook.Worksheets
With sh
Set xIng = Intersect(.UsedRange, .Range("F:F"))
If Not xIng Is Nothing Then
For Each cel In xIng
If cel.Text = myStr Then cel.Offset(0, 1) = myPool
Next
End If
End With
Next
End Sub