2009-11-18 98 views
0

我已經給出了一些格式化爲眼睛而不是任何功能的工作簿。因此,我有相當看起來像這樣的一些列標題:在使用Excel VBA查找範圍

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直到有一個匹配循環。

這不是非常優雅 - 那麼有沒有一種方法可以搜索一個虛擬數組/範圍,以及我正在尋找的單詞安排?

+0

你是說列標題可能總是不一樣,'月計劃'可能不在E列?你不想改變你收到的文件,只參考它們? – datatoo 2009-11-23 17:04:07

回答

1

如果您發現一個單元格和下面的直接連接會怎樣?

您可以通過修剪或刪除空格,符號以及將所有內容轉換爲小寫字母來簡化檢測。

而不是使用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 

當然可以完善和改進,但是這將是基本的想法。

或...爲什麼你找不到第一個數值?

+0

我不明白?我如何使用。查找連接? 我無法編輯這些'源'文件(例如上面的示例)。 – 2009-11-18 14:10:02

+0

我已經編輯了答案來澄清它。 – 2009-11-18 15:51:54