I've got a piece of VBA code that will import a piece of data from another workbook into my open excel spreadsheet however, it won't import if into the sheet that I have already created. It wants to open a new sheet every time.
This is the code:
Sub ImportCurrentMonthData()
' Get workbook...
Dim ws As Worksheet
Dim filter As String
Dim targetWorkbook As Workbook, wb As Workbook
Dim Ret As Variant
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Excel files (*.xlsb),*.xlsb"
Caption = "Please Select an input file "
Ret = Application.GetOpenFilename(filter, , Caption)
If Ret = False Then Exit Sub
Set wb = Workbooks.Open(Ret)
wb.Sheets(1).Move After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
ActiveSheet.Name = "CurrentMonth"
I have a sheet called "CurrentMonth" already and I want to data to go into that spreadsheet. What do I need to change in the VBA code for this happen?
Thanks in advance.
wb.Worksheets("CurrentMonth").ActivateThe question is how do you import the data?"CurrentMonth"worksheet in your active workbook, or in the one you've just opened? If in your active workbook, before opening the second one, you should move the code immediately afterSet targetWorkbook = Application.ActiveWorkbook. But where is your problem? Your code does not do anything. You practically name the active sheet of the opened workbook like "CurrentMonth". This is what you want doing?"CurrentMonth"worksheet in your active workbook, or in the one you've just opened?". "But where is your problem?" Do you receive an error? The above code does not do anything, except opening the new workbook and rename its active sheet as "CurrentMonth", "Is this what you want doing?"