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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.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高效地将多个电子表格导入到一个主控表中_Vba_Excel - Fatal编程技术网

使用Excel VBA高效地将多个电子表格导入到一个主控表中

使用Excel VBA高效地将多个电子表格导入到一个主控表中,vba,excel,Vba,Excel,我是VBA新手,我有一些我已经编写的代码,虽然它可以工作,但我认为它太庞大了,如果需要对它进行更改,它就不是很好了 代码打开一个电子表格,运行一个函数(称为“LastRow”)复制数据,另一个函数(称为“NxtRow”)使用宏将数据粘贴到电子表格的下一个空行,然后关闭复制数据的工作表并移动到下一个工作表。基本上,它是将多张图纸连接成一张 我认为必须有一种方法来编写代码,调用函数一次,然后在列表中的每一页中循环。这可能吗 我的代码是: NxtRow()函数 LstRow()函数 VBA Sub()

我是VBA新手,我有一些我已经编写的代码,虽然它可以工作,但我认为它太庞大了,如果需要对它进行更改,它就不是很好了

代码打开一个电子表格,运行一个函数(称为“LastRow”)复制数据,另一个函数(称为“NxtRow”)使用宏将数据粘贴到电子表格的下一个空行,然后关闭复制数据的工作表并移动到下一个工作表。基本上,它是将多张图纸连接成一张

我认为必须有一种方法来编写代码,调用函数一次,然后在列表中的每一页中循环。这可能吗

我的代码是:

NxtRow()函数

LstRow()函数

VBA Sub()


这种方式持续大约30张。是否有一种更简单的方法来编写此文件,并在以后需要时使其更易于修改?

我只需要将文件名制作一个小数组,然后使用for循环,根据需要重复函数调用多次

Sub ImpData()

    'Deactivate Screen Updating and Display Alerts
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    Dim filenames As Variant
    filenames = Array("file1", "file2")

    For i = 1 To UBound(filenames) + 1
        Workbooks.Open Filename:=filenames(i - 1)
        LastRow
        NxtRow
        Windows("Worksheet" & i & ".xlsx").Activate
        ActiveWindow.Close
    Next i

    'Reactivate Screen Updating and Display Alerts
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

End Sub

好吧,你可以看看我最终得到的答案:如果这有助于投票的话!他们的名字中是用“1”、“2”等命名的,还是通用的,这样你们就可以在这里发布?另外,你们可以发布lastrow和nextrow,这样我们就可以对他们进行更改吗?你太依赖于
激活
选择
@Marcucciboy否由于数据保护原因,必须更改给定的名称在Excel中取消新的PowerQuery功能…非常适合这种挑战这太棒了!谢谢你的帮助@FirepowerG很高兴它起了作用!请考虑点击复选框接受它作为最佳答案:
Public Function LastRow()
Dim LstRow As Long, LstCol As Long, Rng As Range, A3 As Range
LstRow = Range("A" & Rows.Count).End(xlUp).Row
LstCol = Range("O" & LstRow).Column
Set Rng = Range(Cells(LstRow, 1), Cells(LstRow, LstCol))
Set A3 = Range("A3")
Range(A3, Rng).Select
Selection.Copy
End Function
Sub ImpData()

'   Deactivate Screen Updating and Display Alerts
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'   Import Worksheet 1
Workbooks.Open Filename:= _
"Worksheet1_Filename.xlsx"
LastRow
NxtRow
Windows("Worksheet1.xlsx").Activate
ActiveWindow.Close

'   Import Worksheet 2
Workbooks.Open Filename:= _
"Worksheet2_Filename.xlsx"
LastRow
NxtRow
Windows("Worksheet2.xlsx").Activate
ActiveWindow.Close

'   Import Worksheet 3
Workbooks.Open Filename:= _
"Worksheet3_Filename.xlsx"
LastRow
NxtRow
Windows("Worksheet3.xlsx").Activate
ActiveWindow.Close
Sub ImpData()

    'Deactivate Screen Updating and Display Alerts
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    Dim filenames As Variant
    filenames = Array("file1", "file2")

    For i = 1 To UBound(filenames) + 1
        Workbooks.Open Filename:=filenames(i - 1)
        LastRow
        NxtRow
        Windows("Worksheet" & i & ".xlsx").Activate
        ActiveWindow.Close
    Next i

    'Reactivate Screen Updating and Display Alerts
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

End Sub