Help please - how to set target worksheet?

Cuzzaa

Board Regular
Joined
Apr 30, 2019
Messages
86
Hi everyone

Please could someone try and help me?

I am using the below code which works, it essentially is importing a set range of data from one worksheet into my main worksheet.

What I need to do is set the worksheet name as currently it just imports the data into the activework sheet.

Can anyone help me please? Say using the worksheet name as 'WorksheetName'.

Code:
Sub Import1()


' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook




' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook




' get the customer workbook
filter = "Text files (*.xls),*.xls"
caption = "Please select the Timesheets "
customerFilename = Application.GetOpenFilename(filter, , caption)


Set customerWorkbook = Application.Workbooks.Open(customerFilename)


' assume range is A2 - K200 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)


targetSheet.Range("A2", "K500").Value = sourceSheet.Range("A2", "K500").Value


' Close customer workbook
customerWorkbook.Close


' targetSheet.Range("M3").Value = customerFilename


Range("N4").Select
    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh


Range("A1").Select
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
What's the name of the workbook in which your code resides?
Where, in that workbook's VBA project, does the code reside (in a module, "ThisWorkbook", or a worksheet's module)?

What I need to do is set the worksheet name as currently it just imports the data into the activework sheet
Your code actually imports the data into "Sheets(1)" of the activeworkbook, which may or may not be the active worksheet.
Is the data being imported into the same workbook as the one where this code resides, and if not, what's the actual name of the target workbook?
 
Upvote 0
What's the name of the workbook in which your code resides?
Where, in that workbook's VBA project, does the code reside (in a module, "ThisWorkbook", or a worksheet's module)?


Your code actually imports the data into "Sheets(1)" of the activeworkbook, which may or may not be the active worksheet.
Is the data being imported into the same workbook as the one where this code resides, and if not, what's the actual name of the target workbook?

Sykes, you've just made me understand what's happening here so I've sorted it. Thanks so much!
 
Upvote 0
Great! Much better when you can find the solution yourself, isn't it? Albeit with a tiny nudge in the right direction!
Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top