ThisWorkbook paste

waldymar

Board Regular
Joined
Feb 19, 2009
Messages
238
Dear all,

I'm trying to copy a data from the open file into the code-running file:

Code:
myFile = varPath & varFileName
                                
    Set wb = Workbooks.Open(myFile)
    Set tbl = wb.Worksheets(varSheetName).Range(varRangeName)
    
    tbl.Offset(1, 0).Resize(tbl.Rows.Count - 1, tbl.Columns.Count).Copy
    
    ThisWorkbook.Worksheets(calc).Cells(y + 1, 1).PasteSpecial

There is a problem with the last row. "calc" is the property name of the sheet in the ThisWorkbook. How can I fix it?

Thanks in advance
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
If by 'property' name, you mean the name on the sheet's tab, put quotes around it.
 
Upvote 0
Okay, that is called the CodeName. Try:

<font face=Courier New>calc.Cells(y + 1, 1).PasteSpecial</FONT>
 
Upvote 0
thanks, it works.

question in order to understand: shouldn't i specify the workbook as i have 2 of them open? what if the code will look for the sheets name (calc) in the wrong workbook?
 
Upvote 0
no, property name means vba name of the sheet and not excel one

Just a note of clarification to help. A worksheet has many properties, such as the CodeName property, the Cells property, and of course, a Name property.

thanks, it works.

question in order to understand: shouldn't i specify the workbook as i have 2 of them open? what if the code will look for the sheets name (calc) in the wrong workbook?

Well, think of the CodeName as an Object name, which in this case, said object belongs to the workbook in which it resides. You could think of this like ThisWorkbook. Since it belongs to the specific workbook, Excel knows what it belongs to when you write ThisWorkbook.Woksheets("My Sheet").

Similarly, if you added a UserForm and named it 'frmDataForm', you would just use frmDataForm to refer to it.

Does that make sense?

Mark
 
Upvote 0
BTW, you may wish to use some type of consistent naming convention for such objects. Something like shtData or wksData for a sheet's codename, or frmDataEntry for a userform, will save you headaches later when you go back to edit/update code you wrote a few months previous.
 
Upvote 0
I think I realize that we spoke about different names. Codename you were talking about is probably something like:

Dim calc as a worksheet
set calc=ThisWorkbook.Woksheets("My Sheet")

I meant a different sheet name:
when you are in VBA window it has 3 sections:
  1. the main one where you write code
  2. project VBAProject
  3. properties
if you go to properties attributed to your worksheet then you can give a vba name to it. it prevent code working from the sheet name change in excel.

what do you think?
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

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