Code is not working

abeed

Board Regular
Joined
Nov 19, 2005
Messages
79
Hi:
The following code giving application error:

rs.Range(Cells(sr, 2), Cells(sr, 13)).Value = ds.Range(Cells(tr, cn), Cells(tr, cn + 11)).Value

--
rs is my rport shhet,ds is my data sheet in 2 different workbook. I am trying to copy from ds to rs.
at the first loop iteration:
sr =10,tr=10,cn=2

--Thanks in advance
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,974
Office Version
  1. 365
Platform
  1. Windows
Excel does not like it when you try to set a multiple cell range equal to another multiple cell range.

Here are two solutions:
1. Write a loop (probably not that efficient)
2. Use Copy & Paste macro (see below)

Code:
    rs.Activate
    Range(Cells(sr, 2), Cells(sr, 13)).Copy
    ds.Activate
    Cells(tr, cn).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
 

abeed

Board Regular
Joined
Nov 19, 2005
Messages
79
Hi Jmiskey:

The code working fine if I dont use any variable(like sr, tr etc).
For example,
rs.Range("b2:b13").Value = ds.Range("b10:b21")).Value

Thanks
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,974
Office Version
  1. 365
Platform
  1. Windows
abeed,

I stand corrected.

That behavior is strange indeed. It seems that if you do not have a direct range reference, VBA does not like it unless you first activate the second worksheet.

Here is a solution I hope you will find acceptable.
Code:
    Dim Range1 As Range
    Dim Range2 As Range
    
    Set Range1 = rs.Range(Cells(sr, 2), Cells(sr, 13))
    ds.Activate
    Set Range2 = ds.Range(Cells(tr, cn), Cells(tr, cn + 11))
    
    Range1.Value = Range2.Value
 

abeed

Board Regular
Joined
Nov 19, 2005
Messages
79

ADVERTISEMENT

Hi Jmiskey:

Oaah. Its working man. I got all my data brought into my report sheet. Thanks thanks a lot. I will get back to you soon with other issues.

You people are cool!!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,974
Office Version
  1. 365
Platform
  1. Windows
Glad to help.

If you have other issues (not related to this one), be sure to post them in a new thread. That way you will get the benefit of more people looking at it as an "unanswered" post.
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473

ADVERTISEMENT

This seems to work

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> foo()
<SPAN style="color:#00007F">Dim</SPAN> rs <SPAN style="color:#00007F">As</SPAN> Worksheet, ds <SPAN style="color:#00007F">As</SPAN> Worksheet, sr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, tr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, cn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Set</SPAN> rs = Worksheets("Sheet1")
<SPAN style="color:#00007F">Set</SPAN> ds = Worksheets("Sheet2")
sr = 10
tr = 10
cn = 2
rs.Range(rs.Cells(sr, 2), rs.Cells(sr, 13)).Value = ds.Range(ds.Cells(tr, cn), ds.Cells(tr, cn + 11)).Value
<SPAN style="color:#00007F">Set</SPAN> rs = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">Set</SPAN> ds = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,974
Office Version
  1. 365
Platform
  1. Windows
Thanks jon. I learned something new today.

It looks like the key is that you have to preface EVERY range reference with the Worksheet variable; not just the ones that start RANGE, but also the ones that start CELLS. I guess the reference in front of RANGE doesn't carry over all the way through.
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473
jmiskey said:
Thanks jon. I learned something new today.

It looks like the key is that you have to preface EVERY range reference with the Worksheet variable; not just the ones that start RANGE, but also the ones that start CELLS. I guess the reference in front of RANGE doesn't carry over all the way through.

Not sure, this is behavior I'm not familiar with - the structure

Workshee('ref1").Range(Cells(rowref, colref), Cells(RowRef, ColRef)) = ...

should be sufficient as the period should bind the Range to the Worksheet, no?

I'm just not seeing the obvious...
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,974
Office Version
  1. 365
Platform
  1. Windows
I agree. I thought it should work, but apparently it doesn't, as we have shown that:

rs.Range(rs.Cells(sr, 2), rs.Cells(sr, 13)).Value = ds.Range(ds.Cells(tr, cn), ds.Cells(tr, cn + 11)).Value

works and

rs.Range(Cells(sr, 2), Cells(sr, 13)).Value = ds.Range(Cells(tr, cn), Cells(tr, cn + 11)).Value

does not work.

Seems a little redudant to have to put the Sheet references on everything inside the RANGE reference, but maybe it is just one of those Excel/VBA things.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,341
Messages
5,601,063
Members
414,427
Latest member
fraru

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
Top