簡體   English   中英

在Excel VBA中循環遍歷單元格值

[英]Looping through cell values in Excel VBA

我一直在嘗試編寫一個程序,該程序將循環遍歷excel工作表中的所有單元格,如果以“#”開頭,​​則應顯示一條消息。 這是代碼:

(模板是工作表變量)

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

我已經隔離出在cells()內部使用變量的錯誤。 如果我將count替換為某個數字(例如8),則效果很好。 我在“ If template.Cells(count).Value Like "[#]*" Then的行上收到錯誤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

我們可以使用sub而不是function

我們循環遍歷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

您可以使用find / find next函數,我想它比循環遍歷每個單元格和進行字符串比較要快一點。

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/zh-cn/library/office/ff196143(v=office.15).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM