簡體   English   中英

使用Excel VBA查找范圍

[英]Using Excel VBA to find a range

我得到了一些工作簿,這些工作簿是為眼睛格式化的,而不是任何功能正常的。 因此,我有很多看起來像這樣的列標題:

  A             B         C         D      E
1               'YTD      'Monthly  'YTD   'Monthly
2               Figures'  Figures'  Plan'  Plan'
3  
4  Account1     1         3         5      7
5  Account2     2         4         6      8

我一直在使用.Find標識'Account 1'的行,然后標識'Monthly Plan'的列,然后將該單元格值復制到數組中。 但是,在某些區域(例如上面的示例),我無法輕易找到E列,因為搜索'Monthly''Plan'顯然給我帶來了不可靠的結果。

到目前為止,我一直在使用Do... While... Loop來查找'Monthly'的單元格地址,然后立即在下面檢查單詞'Plan'的單元格值,然后使用.FindNext循環,直到出現比賽。

這不是很優雅-那么有什么方法可以搜索我要查找的單詞排列的虛擬數組/范圍?

如果您發現一個單元格與下面的單元格的串聯,該怎么辦?

您可以通過修剪或刪除空格,符號並將所有內容轉換為小寫字母來簡化檢測,從而對其進行優化。

而不是使用find您應該遍歷單元格。

編輯:這是一個可能的解決方案:

Sub Macro1()

    Dim idx_row As Long
    Dim idx_column As Long
    Dim idx_row_temp As Long
    Dim found_row As Boolean
    Dim found_column As Boolean
    Dim str_concat As String

    found_row = False
    idx_row = 1
    idx_column = 1
    Do While (Not found_row)
        If (Cells(idx_row, idx_column).Value = "Account1") Then
            found_row = True
        Else
            idx_row = idx_row + 1
        End If
    Loop

    found_column = False
    idx_row_temp = 1
    Do While (Not found_column)

        str_concat = Cells(idx_row_temp, idx_column).Value & Cells(idx_row_temp + 1, idx_column).Value

        MsgBox (str_concat)

        If (str_concat = "'MonthlyPlan'") Then
            found_column = True
        Else
            idx_column = idx_column + 1
        End If
    Loop

    MsgBox "Row: " & idx_row & vbCrLf & "Column: " & idx_column

End Sub

當然可以對其進行完善和改進,但這將是基本思想。

或者...為什么不找到第一個數值?

暫無
暫無

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

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