Copy cells from another workbook

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
2,138
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi All

Im sure i have done this before, but i must be missing something, probably time for a break.

Im sure i can copy a rance of cells from one workbook to another? Without using range("a2:a50") as example

VBA Code:
Sub DB()
    Set RNG = Workbooks(MYBOOK).Sheets("APP") 'MYBOOK IS A PUBLIC VAIRABLE ALREADY DEFINED AND IS GOOD
    WORD = RNG.DOG_SELECTOR.Value 'THERE IS A VALUE HERE, SO THIS IS FINE
    WORD = Mid(WORD, 10, 100)
    A = 1
    LR=50
    Set RNGC = Workbooks(CURRENT_DAY & ".xlsx").Sheets(WORD) 'CURRENT_DAY IS A PUBLIC VAIRABLE ALREADY DEFINED AND IS GOOD
    RNGC.Range(Cells(2, A), Cells(LR, A)).Copy 'BUGS OUT HERE, WONT COPY,


End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I don't understand the use of variables very well, perhaps because we don't use the same nomenclature, for example, we regularly use RNG for a range and SH or WS for a sheet. But they are each person's way of programming.


Your code is missing a reference to the sheet object in each cell object:
Rich (BB code):
    Set RNGC = Workbooks(CURRENT_DAY & ".xlsx").Sheets(WORD) 'CURRENT_DAY IS A PUBLIC VAIRABLE ALREADY DEFINED AND IS GOOD
    RNGC.Range(RNGC.Cells(2, A), RNGC.Cells(LR, A)).Copy 'BUGS OUT HERE, WONT COPY,

🧙‍♂️
 
Upvote 0
Solution
Just to say you can omit the 1st RNGC
VBA Code:
Range(RNGC.Cells(2, A), RNGC.Cells(LR, A)).Copy
 
Upvote 0
I don't understand the use of variables very well, perhaps because we don't use the same nomenclature, for example, we regularly use RNG for a range and SH or WS for a sheet. But they are each person's way of programming.


Your code is missing a reference to the sheet object in each cell object:
Rich (BB code):
    Set RNGC = Workbooks(CURRENT_DAY & ".xlsx").Sheets(WORD) 'CURRENT_DAY IS A PUBLIC VAIRABLE ALREADY DEFINED AND IS GOOD
    RNGC.Range(RNGC.Cells(2, A), RNGC.Cells(LR, A)).Copy 'BUGS OUT HERE, WONT COPY,

🧙‍♂️
Worked spot on, now i look at it, its actually obvious.

Yeah, my code is on my little home projects and on my business systems. only i understand it ;)

Thanks

Dave
 
Upvote 0
Just to say you can omit the 1st RNGC
VBA Code:
Range(RNGC.Cells(2, A), RNGC.Cells(LR, A)).Copy
Hi Mark

Thanks

But actually it only worked with the RNGC at the beginning as well as per DanteAmor suggestion.

But thankyou both

Dave
 
Upvote 0
But actually it only worked with the RNGC at the beginning as well as per DanteAmor
Hmm, works for me fine copying from a different workbook without the leading RNGC with the code below with a workbook called myBook copying into the workbook that contains the code.

VBA Code:
Sub test()
Dim A As Long, LR As Long, RNGC As Worksheet
    A = 1
    LR = 50
    Set RNGC = Workbooks("myBook" & ".xlsx").Sheets("Sheet1")
    Range(RNGC.Cells(2, A), RNGC.Cells(LR, A)).Copy
Sheets("Sheet2").Range("B1").PasteSpecial

Application.CutCopyMode = False
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,728
Members
453,368
Latest member
positivemind

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