我有以下代碼,由@FreeMan根據我以前的一個問題修改。我想在工作表的任何一行中找到文本「小時」。然後,將代碼應用於包含該文本的列。這段代碼應該是這樣做的,但由於某種原因它不適用於我。我真的很感謝你的幫助。先謝謝你。Excel VBA:如何在列中找到文本時應用代碼
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
問題是,你的'coltoSearch'變量將永遠是一個數字或根據你的代碼,但你使用它就像它會是一封信。 –
應該'r'是一個範圍,或者也許是一個字符串? 'Dim r as String'然後'r = Cells(i,coltosearch).Value'?當你說「......它對我不起作用」時,什麼都不會發生。 – BruceWayne
我得到錯誤400. – LillieG