2012-02-10 32 views
0

我對VBA Excel的迫切需要一個驗證列表:VBA爲Excel - 創建一個基於數據從另一個工作表在同一本書

On sheet 1, i have 

A   B     C 
----------------------------------- 
     DATASET 1   variable1 
          variable2 
          variable3 
     DATASET 2   variable4 
          variable5 
          variable6 

     ....... 

旁板(板2,例如)

我有

D     F 
    ----------------------------------- 
    DATASET 1   variable1 
         variable2 
         variable3 
         ... 
         variable100 
    DATASET 2   variable4 
         variable5 
         variable6 
         variable200 

     ....... 

我怎麼能寫VBA宏創建表1數據輸入驗證列表。當我單擊單元格爲數據集1輸入變量1時,它將顯示從數據集名稱(例如數據集1)過濾的圖表2中拉出的列表?

謝謝。

回答

1

假設所示的佈局是準確和列A具有命名範圍組中的每個電勢之間的空單元,則該宏將從每一列創建一個名爲範圍使用的單元格範圍在列B A值:

Option Explicit 

Sub AddNames() 
Dim RNG As Range, LR As Long, Nm As Long 

With ActiveSheet 
    LR = .Range("B" & .Rows.Count).End(xlUp).Row 
    Set RNG = Range("A:A").SpecialCells(xlConstants) 

    For Nm = 1 To RNG.Areas.Count 
     If Nm < RNG.Areas.Count Then 
      ActiveWorkbook.Names.Add Name:=Replace(RNG.Areas(Nm).Cells(1).Value, " ", ""), _ 
       RefersToR1C1:="='" & .Name & "'!R" & RNG.Areas(Nm).Cells(1).Row & _ 
        "C2:R" & RNG.Areas(Nm + 1).Cells(1).Row - 1 & "C2" 
     Else 
      ActiveWorkbook.Names.Add Name:=Replace(RNG.Areas(Nm).Cells(1).Value, " ", ""), _ 
       RefersToR1C1:="='" & .Name & "'!R" & RNG.Areas(Nm).Cells(1).Row & _ 
        "C2:R" & LR & "C2" 
     End If 
    Next Nm 
End With 

End Sub 
+0

謝謝,傑瑞。一個問題:我怎麼能找到sheet2中的範圍? – john 2012-02-11 00:08:15

+0

你能解釋一下嗎? (如果原始答案適用於原始問題,我將不勝感激);) – 2012-02-17 00:47:03

相關問題