VBA Excel中有一个任务。
。
这个范围实际上包含将近1,600个单元格。但我把它简化了来理解这个问题。
请帮忙解决这个问题。
示例文件附呈。有一个代码(下面),但它不工作。
Sub KopirovanieIVstavkaVRaznyeWorkbook()
Dim MyRange As Range
Dim MyCell As Range
Dim MyFiles As String
Set MyRange = Application.Workbooks(List.xlsm).Worksheets("Sheet1").Range("B2:B4")
For Each MyCell In MyRange
If MyCell > 0 Then
MyFiles = Dir("C:\Users\User\Desktop\Papka\*.xlsx")
Do While MyFiles <> “”
Workbooks.Open "C:\Users\User\Desktop\Papka\" & MyFiles
ActiveWorkbook.Worksheets(1).Range("A2") = MyCell
ActiveWorkbook.Close SaveChanges:=True
MyFiles = Dir
Exit Do
Loop
Else
MyCell.Offset(0, 1).Value = "Pusto"
End If
Next MyCell
End Sub
发布于 2020-07-05 11:01:54
尝尝这个
Sub KopirovanieIVstavkaVRaznyeWorkbook()
Dim MyRange As Range
Dim MyFile As String
Dim i As Long
Dim wb As Workbook
Set MyRange = ThisWorkbook.Sheets("Sheet1").Range("B2:B4")
With MyRange
For i = 1 To .Rows.Count
' Assumes Excel files are named "Excel-file n.xlsx", where n is an integer
MyFile = "C:\Users\User\Desktop\Papka\Excel-file " & i & ".xlsx"
Set wb = Workbooks.Open(MyFile)
' Assume the target A1 is in the first sheet of the workbook
wb.Sheets(1).Range("A1").Value = .Cells(i, 1).Value
wb.Close SaveChanges:=True
.Cells(i, 1) = "Pusto"
Next i
End With
End Sub
请注意这些假设
https://stackoverflow.com/questions/62737930
复制相似问题