Excel VBA:如何在列中find文本时应用代码

我有以下代码,由@FreeMan从我以前的问题之一修改。 我想在工作表的任何一行中find文本“小时”。 然后,将代码应用于包含该文本的列。 这个代码应该这样做,但由于某种原因,这对我不起作用。 我真的很感激你的帮助。 先谢谢你。

Sub CeldasinInfo() Dim i As Long, r As Range, coltoSearch As String Dim Result as String Dim ErrCount as integer ErrCount = 0 coltoSearch = "A" coltoSearch = Range("1:1").find(What:="Hours", LookIn:=xlValues, LookAt:=xlWhole).Column Result = "No Value in:" & vbcrlf For i = 1 To Range(coltoSearch & Rows.Count).End(xlUp).Row Set r = Range(coltoSearch & i) If Len(r.Value) = 0 Then r.Interior.ColorIndex = 3 ' Red r.Select MsgBox "No Value, in " & r.Address Result = Result & r.Address & vbcrlf ErrCount = ErrCount + 1 if ErrCount Mod 10 = 0 then 'change to 15 or 20 or whatever works well MsgBox Result Result = "No Value in:" & vbcrlf End If Sheets("Results").Range("A" & Sheets("Results").Range("A" & Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = r.Address End If Next If ErrCount > 0 then MsgBox "There were " & ErrCount & " errors detected." & vbcrlf & result else MsgBox "No errors detected" End If End Sub 

你需要改变这两行代码:

 For i = 1 To Range(coltoSearch & Rows.Count).End(xlUp).Row Set r = Range(coltoSearch & i) 

至:

 For i = 1 To Cells(Rows.Count, coltoSearch).End(xlUp).Row Set r = Cells(i, coltoSearch) 

删除行: coltoSearch = "A"

coltoSearch应该是一个整数。