2014-08-27 203 views
1

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

+0

template.cells(计数)。价值将其给出template.cells(0)。价值从0开始。尝试从1开始:count = 1 – Alex 2014-08-27 20:13:23

回答

0

错误我们可以用一个而非功能

我们遍历所有UsedRange寻找作为单元格中的第一个字符。

Sub FindThePound() 
    Dim r As Range, pound As String, template As Worksheet 
    pound = "#" 
    Set template = ActiveSheet 
    For Each r In template.UsedRange 
     If Left(r.Value, 1) = pound Then 
      MsgBox "Found # in " & r.Address(0, 0) 
     End If 
    Next r 
End Sub 

编辑#1

该版本遍历所有的细胞,但不检查包含公式

Sub FindThePound() 
    Dim r As Range, pound As String, template As Worksheet 
    pound = "#" 
    Set template = ActiveSheet 
    For Each r In template.UsedRange 
     If r.HasFormula = False Then 
      If Left(r.Value, 1) = pound Then 
       MsgBox "Found # in " & r.Address(0, 0) 
      End If 
     End If 
    Next r 
End Sub 
+1

该代码的作品,并在某种意义上,一个正确的答案,但绝对零解释什么OP做错了什么,或者你的代码做了什么正确的... – RubberDuck 2014-08-27 23:47:17

+0

这主要是工作,它抛出当它运行到公式时出错。 (r.Value =调试器中的错误2015),地址是单元格B14,内容是:(= + B13 + B12),通常如果B13和B12中有值,则评估为正确。 – James 2014-08-28 18:17:09

+0

@詹姆斯嗨______看到我的**编辑#1 ** – 2014-08-28 18:51:47

0

你可以用查找/查找下一个,我猜位功能的细胞快于循环遍历每个单元格并进行字符串比较。

With Worksheets(1).Range("a1:a500") 'Provide the search range 
    Set c = .Find(2, lookin:=xlValues) ' searching for 2 in cell value 
    If Not c Is Nothing Then 
     firstAddress = c.Address 'first occurance 
     Do 
      'do whatever you want to do with the matches even replace them 
      c.Value = 5 
      Set c = .FindNext(c) 
     Loop While Not c Is Nothing And c.Address <> firstAddress 
    End If 
End With 

参考: http://msdn.microsoft.com/en-us/library/office/ff196143(v=office.15).aspx

+0

最后,我将需要复制所有没有'#'的单元格,我会将它们解释为从另一个工作表中获取数据的指令,以#之后的部分作为位置。所以我需要通过所有的细胞。 – James 2014-08-27 22:06:15