我一直在尝试编写一个程序,它将循环遍历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