2014-08-27 135 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