2012-02-10 95 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

相关问题