Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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
Vba 循环浏览动态图纸名称_Vba_Excel - Fatal编程技术网

Vba 循环浏览动态图纸名称

Vba 循环浏览动态图纸名称,vba,excel,Vba,Excel,我在文件夹中的一堆文件中循环,试图复制一个静态列并粘贴到主工作表。然而,我循环浏览的每一张纸都有一个不同的名字 我认为守则的这一部分必须修改: xlsFiles.Sheets("Sheet3").Columns("20").Copy Destination:=wsMaster.Sheets("Sheet1").Range("A" & r).Offset(1, 0). 我可以用什么来代替床单3 以下是完整的代码: Option Explicit Dim wsMaster As Workb

我在文件夹中的一堆文件中循环,试图复制一个静态列并粘贴到主工作表。然而,我循环浏览的每一张纸都有一个不同的名字

我认为守则的这一部分必须修改:

xlsFiles.Sheets("Sheet3").Columns("20").Copy Destination:=wsMaster.Sheets("Sheet1").Range("A" & r).Offset(1, 0).
我可以用什么来代替床单3

以下是完整的代码:

Option Explicit
Dim wsMaster As Workbook, csvFiles As Workbook
Dim Filename As String
Dim File As Integer
Dim r As Long

Public Sub Consolidate()

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = True
        .Title = "Select files to process"
        .Show

        If .SelectedItems.Count = 0 Then Exit Sub

        Set wsMaster = ActiveWorkbook

        For File = 1 To .SelectedItems.Count

            Filename = .SelectedItems.Item(File)


           If Right(Filename, 5) = ".csv*" Then
    Set csvFiles = Workbooks.Open(Filename, 0, True)
    r = wsMaster.Sheets("Sheet1").UsedRange.Rows.Count
    csvFiles.Sheets(1).Columns("col name").Copy Destination:=wsMaster.Sheets("Sheet1").Range("A" & r).Offset(1, 0)
    csvFiles.Close SaveChanges:=False 'close without saving
            End If


        Next File 'go to the next file and repeat the process

    End With

    Set wsMaster = Nothing
    Set csvFiles = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True

    End With

End Sub

在上面的评论中,@sktneer已经给出了答案

您可以稍微缩短和清理If部分代码,请尝试以下代码:

If Right(Filename, 5) = ".xls*" Then
    Set xlsFiles = Workbooks.Open(Filename, 0, True)
    r = wsMaster.Sheets("Sheet1").UsedRange.Rows.Count
    xlsFiles.Sheets(3).Columns("20").Copy Destination:=wsMaster.Sheets("Sheet1").Range("A" & r).Offset(1, 0)
    xlsFiles.Close SaveChanges:=False 'close without saving
End If

如果工作表的索引始终为3 i.t,则可以使用Sheets3而不是SheetsSheet3。它在工作簿中的位置。谢谢..愚蠢的问题。如果现在有问题,lol,我将更新我的代码。不确定为什么我需要文件名,但我正在循环浏览csv文件,我将向您显示上面更新的代码。@Jonnyboi在您上面编辑的代码中,Filename是如何获取其字符串值的?其中是part=selectedit将所选项目添加回,意外地将其删除。但由于某些原因,我没有得到任何复制到我的主人sheet@Jonnyboi如果您试图打开csv文件,那么您只有一个工作表,请尝试csvFiles.Sheets1.Columns20.Copy Destination:=wsMaster.SheetsSheet1.RangeA&r.Offset1,0必须与我命名工作表的方式有关。是的,我同意,我应该把它作为表1,但是运行这个宏时没有发生任何事情。