Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果我试图在excel vba中附加文件,是否有方法动态设置文件路径?_Excel_Vba - Fatal编程技术网

如果我试图在excel vba中附加文件,是否有方法动态设置文件路径?

如果我试图在excel vba中附加文件,是否有方法动态设置文件路径?,excel,vba,Excel,Vba,下面是我正在尝试处理的代码。我是Excel VBA新手 Sub Test1() Dim x As Integer Application.ScreenUpdating = False ' Set numrows = number of rows of data. NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count For x = 1 To NumRows

下面是我正在尝试处理的代码。我是Excel VBA新手

    Sub Test1()
      Dim x As Integer
      Application.ScreenUpdating = False
      ' Set numrows = number of rows of data.
      NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count
           For x = 1 To NumRows
         ActiveSheet.OLEObjects.Add(Filename:= _
        "Filelocation\filename.extension" _
        , Link:=False, DisplayAsIcon:=True, IconFileName:="C:\Windows\Installer\{90160000-000F-0000-1000-0000000FF1CE}\wordicon.exe", _
        IconIndex:=0, IconLabel:= _
        "Filelocation\filename.extension" _
        ).Select

        ActiveCell.Offset(1, 0).Select
        Next
      Application.ScreenUpdating = True
    End Sub
我想在这里附加文件夹中的下一个文件。 到现在为止,使用这段代码,我可以多次附加同一个文件。 我的要求是在向下移动时在不同的单元格中附加不同的文件。
我希望问题是清楚的:)

我已经创建了一个名为
Control
的工作表,其中包含要附加的文件的位置,并带有一个按钮,其中指定了以下宏

Sub fileInsertionForRetest()
On Error GoTo er
    Dim x As Integer
    Dim NumRows As Long
    Worksheets("Retest").Activate
    Application.ScreenUpdating = False
    Range("G2").Select
    NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count
        For x = 1 To NumRows


           Range("B" & x + 1&).EntireRow.RowHeight = 60

            ActiveSheet.OLEObjects.Add(Filename:= _
            ThisWorkbook.Sheets("Control").Range("B2").Value & Range("A" & x + 1&).Value & ".docx", Link:=False, DisplayAsIcon:=True, _
            IconFileName:="C:\Windows\Installer\{90160000-000F-0000-1000-0000000FF1CE}\wordicon.exe", _
            IconIndex:=0, IconLabel:=Range("A" & x + 1&).Value).Select

            ActiveCell.Offset(1, 0).Select
        Next
    Application.ScreenUpdating = True
Done:
    MsgBox "All file were attached successfully"
    Exit Sub
er:
MsgBox "The following error occurred: " & err.Description
End Sub

可能重复:这是否回答了您的问题?