如何通过VBA中的aurguments列表循环函数

时间:2017-04-25 12:27:36

标签: excel vba excel-vba

我希望使这个VBA函数更具动态性并处理输入列表而不是单个静态参数。我所拥有的功能对于单个硬编码文件正常工作,我想让它循环遍历给定特定条件的所有文件。具体标准是:对于" C:\ Sales_Reports \ Special Promotions"中的每个excel文件。名称字符串以" 2017 WK 9 WOW"开头(其中9也可以是动态的)调用函数。

该功能目前从文件" 2017 WK 9 WOW"中复制18个值。无需将其打开成合并的工作簿' P' (保存宏的地方)。我有几个文件" 2017 WK 9 WOW" s没有改变结构,所有这些都需要复制到文件' P'每次添加一个包含数据的新行(行5,标题下方。

Sub GetVal()

    MsgBox TheValue("C:\Sales_Reports\Special Promotions", _
        "2017 WK 9 WOW - Ladies Tee", "Week Summary")
End Sub

Function TheValue(Path, WorkbookName, Sheet) As String
Dim testrange As Range
ActiveSheet.rows("5:5").Insert Shift:=xlDown
Set testrange = Range("d$15,e15,f15,t15,n15,s15,ab15,ae15,af15,aj15,ak15,am15,an15,ap15,aq15,at15,av15,bv6")

For Each cell In testrange

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Select Case Replace(cell.Address, "$", "")
    Case "D15"
        TgtRng = "E"
    Case "E15"
        TgtRng = "F"
    Case "F15"
        TgtRng = "G"
    Case "T15"
        TgtRng = "H"
    Case "N15"
        TgtRng = "I"
    Case "S15"
        TgtRng = "J"
    Case "AB15"
        TgtRng = "K"
    Case "AE15"
        TgtRng = "L"
    Case "AF15"
        TgtRng = "M"
    Case "AJ15"
        TgtRng = "N"
    Case "AK15"
        TgtRng = "O"
    Case "AM15"
        TgtRng = "P"
    Case "AN15"
        TgtRng = "Q"
    Case "AP15"
        TgtRng = "R"
    Case "AQ15"
        TgtRng = "S"
    Case "AT15"
        TgtRng = "T"
    Case "AV15"
        TgtRng = "U"
    Case "BV6"
        TgtRng = "V"
    Case Else
        Exit Function

End Select

Range(TgtRng & "5").Value = "='" & Path & "\[" & WorkbookName & "]" & Sheet & "'!" & cell.Address

Application.ScreenUpdating = True

Next
End Function

0 个答案:

没有答案