我一直在嘗試編寫一個程序,它將循環遍歷Excel表中的所有單元格,並且如果以'#'開始,它應該顯示一條消息。下面的代碼:循環遍歷Excel中的單元格值VBA
(模板是一個工作表變量)
Private Function processTemplate()
Dim total As Long
total = template.UsedRange.count
Dim count As Integer
count = 0
While count <= total
If template.Cells(count).Value Like "[#]*" Then 'Here I get a error
MsgBox "Found #"
End If
count = count + 1
Wend
End Function
我已經分離出的錯誤使用電池的內部變量()。如果我替換一些數字(如8),它工作正常。我得到錯誤1004上線If template.Cells(count).Value Like "[#]*" Then
如果我總共Integer
它在同一個地方有相同的錯誤。經過大約2-3小時的研究/將我的頭撞在牆上我不知道。我最初在將template.cells(row, col).Value
分配給字符串變量時出錯。
這裏是我的代碼現在:
Private Sub processTemplate()
MsgBox Len("")
Dim str As String
Dim rows As Long
Dim cols As Long
rows = template.UsedRange.Height
cols = template.UsedRange.Width
Dim row As Integer
row = 1
While row < rows
Dim col As Integer
col = 1
While col < cols
str = template.Cells(row, col).Text
If Len(str) > 0 Then
If Left(template.Cells(row, col).Text, 1) = "#" Then
MsgBox "Found IT"
End If
End If
Rem MsgBox template.Parent.Name & ": " & template.Name & ", Cell(" & row & ", " & col & "): " & template.Cells(row, col).Value
col = col + 1
Wend
row = row + 1
Wend
End Sub
現在我得到str = template.Cells(row, col).Text
template.cells(計數)。價值將其給出template.cells(0)。價值從0開始。嘗試從1開始:count = 1 – Alex 2014-08-27 20:13:23