我在下面的宏中定义了14个范围。我想要做的是将所有这些复制并粘贴到一个for循环而不是单独。我打算像这样构造我的for循环,但我不确定如何连接对象名称和变量i的语法。任何帮助将不胜感激。
Sub GenerateNewReport()
Dim rng1 As Range
Set rng1 = FindHeader("Category", "Detail")
Set rng2 = FindHeader("RXCUI", "Detail")
Set rng3 = FindHeader("NDC", "Detail")
Set rng4 = FindHeader("DDI", "Detail")
Set rng5 = FindHeader("GPI", "Detail")
Set rng6 = FindHeader("Med Name", "Detail")
Set rng7 = FindHeader("Strength", "Detail")
Set rng8 = FindHeader("Dose Form", "Detail")
Set rng9 = FindHeader("FORMULARY_TIER", "Detail")
Set rng10 = FindHeader("QUANTITY_MAX", "Detail")
Set rng11 = FindHeader("QUANTITY_TIME", "Detail")
Set rng12 = FindHeader("PA_REQUIRED", "Detail")
Set rng13 = FindHeader("PA_Group_NAME", "Detail")
Set rng14 = FindHeader("STEP_THERAPY", "Detail")
rng1.AutoFilter _
field:=1, _
Criteria1:=Array("Immunological Agents", "antidepressants", "antipsychotics", "anticonvulsants", "antiretrovirals", "antineoplastics"), _
Operator:=xlFilterValues
For i = 2 To 14
'I know this is string concatenation but I am unsure how to do this for variable names...
rng & i.copy
rng & i.paste
Next i
End Sub
答案 0 :(得分:0)
如果你没有在其他任何地方使用变量名,你可以创建一个范围集合,然后对每个循环使用a,如下所示:
Dim rng1 As Range
Dim rngcollec As New Collection
Dim rngitem as Range
Set rng1 = FindHeader("Category", "Detail")
Set rng2 = FindHeader("RXCUI", "Detail")
rngcollec.Add rng2
Set rng3 = FindHeader("NDC", "Detail")
rngcollec.Add rng3
Set rng4 = FindHeader("DDI", "Detail")
rngcollec.Add rng4
Set rng5 = FindHeader("GPI", "Detail")
rngcollec.Add rng5
Set rng6 = FindHeader("Med Name", "Detail")
rngcollec.Add rng6
Set rng7 = FindHeader("Strength", "Detail")
rngcollec.Add rng7
Set rng8 = FindHeader("Dose Form", "Detail")
rngcollec.Add rng8
Set rng9 = FindHeader("FORMULARY_TIER", "Detail")
rngcollec.Add rng9
Set rng10 = FindHeader("QUANTITY_MAX", "Detail")
rngcollec.Add rng10
Set rng11 = FindHeader("QUANTITY_TIME", "Detail")
rngcollec.Add rng11
Set rng12 = FindHeader("PA_REQUIRED", "Detail")
rngcollec.Add rng12
Set rng13 = FindHeader("PA_Group_NAME", "Detail")
rngcollec.Add rng13
Set rng14 = FindHeader("STEP_THERAPY", "Detail")
rngcollec.Add rng14
rng1.AutoFilter _
field:=1, _
Criteria1:=Array("Immunological Agents", "antidepressants", "antipsychotics", "anticonvulsants", "antiretrovirals", "antineoplastics"), _
Operator:=xlFilterValues
For Each rngitem in rngcollec
rngitem.copy
rngitem.paste
Next