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
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,660
Office Version
365
Platform
Windows
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?
 

Cuzzaa

Board Regular
Joined
Apr 30, 2019
Messages
86
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!
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,660
Office Version
365
Platform
Windows
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.
 

Cuzzaa

Board Regular
Joined
Apr 30, 2019
Messages
86
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.
Yes absolutely, thanks a lot mate!
 

Watch MrExcel Video

Forum statistics

Threads
1,101,850
Messages
5,483,303
Members
407,393
Latest member
GeorgeBrown

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top