我想在一个特定的文件夹中循环Excel文件。打开一个文件后,宏转到一个指定的选项卡,从四个单元格(比如A2; B3; C5和D 6,我命名为Region,DateSales,Sales和Salesman)复制数据到主文件。
我找到了密码。
Sub getDataFromWbs()
Dim wb As Workbook, ws As Worksheet
Dim Region As String
Dim DateSales As Date
Dim Sales As Integer
Dim Salesman As String
Application.ScreenUpdating = False
Set fso = CreateObject("Scripting.FileSystemObject")
'Path to the folder
Set fldr = fso.GetFolder("C:\Users\xxxxx\yyyyyy\Desktop\Sales\")
'Next available row in Master Workbook
y = ThisWorkbook.Sheets("Sheet1").Cells(rows.Count, 1).End(xlUp).Row + 1
'Loop through each file in that folder
For Each wbFile In fldr.files
'Make sure looping only through files ending in .xlsx (Excel files)
If fso.GetExtensionName(wbFile.Name) = "xlsx" Then
'Open current book
Set wb = Workbooks.Open(wbFile.Path)
Region = Sheets(1).Cells(1, 2).Value
DateSales = Sheets(1).Cells(2, 3).Value
Sales = Sheets(1).Cells(3, 5).Value
Salesman = Sheets(1).Cells(4, 6).Value
'Loop through each sheet (ws)
For Each ws In wb.Sheets
'Last row in that sheet (ws)
wsLR = ws.Cells(rows.Count, 1).End(xlUp).Row
'Loop through each record (row 2 through last row)
For x = 2 To wsLR
'Put column 1,2,3 and 4 of current sheet (ws) into row y of master sheet, then increase row y to next row
ThisWorkbook.Sheets("Sheet1").Cells(y, 1) = ws.Cells(x, 1).Value = Region 'col 1
ThisWorkbook.Sheets("Sheet1").Cells(y, 2) = ws.Cells(x, 2).Value = DateSales
ThisWorkbook.Sheets("Sheet1").Cells(y, 3) = ws.Cells(x, 3).Value = Sales
ThisWorkbook.Sheets("Sheet1").Cells(y, 4) = ws.Cells(x, 4).Value = Salesman
y = y + 1
Next x
Next ws
'Close current book
wb.Close
End If
Next wbFile
End Sub
字符串
最终结果是主文件中的“否”和“真”值。
2条答案
按热度按时间bxfogqkk1#
从关闭的工作簿导入单元格值(FSO)
字符串
b4lqfgs42#
正如注解中提到的,您将比较结果写入单元格值。这将是TRUE或TRUE,这就是您看到的结果。要复制实际数据,请尝试更改以下四行代码:
字符串