2009-11-18 222 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