VBA宏可以在文件夹中的所有工作簿上运行

时间:2017-03-24 10:42:04

标签: excel vba

尝试创建VBA宏以打开特定文件夹中的所有工作簿和工作表,然后将信息复制并粘贴到目标工作表中。

当我运行我的宏时,它会在targetWorkbook上给出错误91。

你可以帮帮我吗?

见下面的代码:

Sub importTransData() Dim directory As String, fileName As String, sheet As Worksheet, total As Integer

Application.ScreenUpdating = False Application.DisplayAlerts = False

Dim targetWorkbook As Workbook

targetWorkbook = ActiveWorkbook.Name

'Choose directory 
directory = "C:\Users\midijk\Desktop\Testsource\" fileName = Dir(directory & "*.xl??")

Do While fileName <> ""

Workbooks.Open (directory & fileName)

For Each sheet In Workbooks(fileName).Worksheets Dim sourceSheet As Worksheet Dim sourceWorkbook As Workbook sourceSheet = ActiveSheet.Name sourceWorkbook = ActiveWorkbook.Name

'Select A2:F2

Range("A2:F2").Select

'Select everything below

Range(Selection, Selection.End(xlDown)).Select

'Copy Selection.Copy

'Select targetWorkbook 
Workbooks(targetWorkbook).Activate

'select targetsheet 
Sheets("Transactional Data").Select

'select A1 & go down 
Range("A1").End(xlDown).Offset(1).Select

'Paste as values 
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

'go back to source workbook 
Workbooks(sourceWorkbook).Activate

'go back to source sheet 
Sheets(sourceSheet).Select

Next sheet

Workbooks(fileName).Close

fileName = Dir() Loop

Application.ScreenUpdating = True Application.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:0)

好吧我们找到了我的解决方案!

Product::create([
    'naam' => $request->get('naam'),
    'inkoopprijs' => $request->get('inkoopprijs'),
    'verkoopprijs' => $request->get('verkoopprijs'),
    'users_id' => \Auth::id()
]);