1

I'm trying to copy data from multiple worksheet of a particular excel and paste into the master worksheet. I've written the following code; although it doesn't throw any error, it doesn't go back to the master worksheet ("DestFile" in the code) not does it paste the data. Appreciate any support here.

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
Sarfaraz
  • 11
  • 1
  • I would start here https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba and apply the guidelines outlined there - your code will be easier to manage and more robust. – Tim Williams Jun 18 '20 at 16:27

0 Answers0