Я пытаюсь скопировать данные из нескольких листов определенного Excel и вставить их в основной лист. Я написал следующий код; хотя он не вызывает никаких ошибок, он не возвращает go обратно в основной рабочий лист («DestFile» в коде) и не вставляет данные. Цените любую поддержку здесь.
Sub Monthly_Balance_Fetcher_Click()
Dim DestFile As Workbook, SourceFile As String, GetBook As String, SourceBook As String, myNum As Long, LatestDate As Long, SelectedDate As Long
LatestDate = Range("D1").Value
Set DestFile = ThisWorkbook 'ThisWorkbook is always the workbook that has the code (as opposed to ActiveWorkbook)
GetBook = ActiveWorkbook.Name
SourceFile = Application.GetOpenFilename(Title:="Please browse for the latest monthly TB file, prefer if you save it to your C first")
Workbooks.Open (SourceFile)
SourceBook = ActiveWorkbook.Name
Sheets("Group").Select
myNum = Application.InputBox("Please enter the column number you want to copy from")
Cells(3, myNum).Select
SelectedDate = Cells(3, myNum).Value
If SelectedDate > LatestDate Then
For i = 1 To totalsheets
If Worksheets(i).Name <> "Summary" And Worksheets(i).Name <> "Process Steps" And Worksheets(i).Name <> "Sheet1" And Worksheets(i).Name <> "Adjustments" And Worksheets(i).Name <> "Targets" Then
Worksheets(i).Activate
Range(Cells(6, myNum)).Copy
Range(Cells(13, myNum)).PasteSpecial Paste:=xlPasteValues
DestFile.Activate
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
Cells(lastrow + 1, 3) = Worksheets(i).Name
Range(Cells("G" & lastrow + 1)).PasteSpecial Paste:=xlPasteValues
Cells(lastrow + 1, 8) = SelectedDate
End If
Next
Else
MsgBox "Data from the selected date already exists! This macro will now stop", vbExclamation
DestFile.Activate
Cells(3, 5) = SelectedDate
Cells(2, 4) = LatestDate
End If
End Sub