Set one range to the value of another range on another sheet

szita2000

Board Regular
Joined
Apr 25, 2012
Messages
101
Office Version
  1. 365
Platform
  1. Windows
Hi guys.
I am trying to replace a copy/paste special with setting the value of a range to the values of another, but I can't work it out.
I receive no error message, but the values not getting written in to my rgto range

This is where I am at the moment: The last few lines are the ones not working.

Just thinking out loud. But ...
Is my problem that the valules are not loaded in to an array with the () operator?
Wonder if that is the solution to say the rngto = arr().

VBA Code:
Sub ArchiveThisLine()
    
    Dim b As Range
    Dim lrto As Long, rgSource As Range, rgTo As Range
    
    Application.EnableEvents = False
    
    lrto = Sheet25.Range("A" & Rows.Count).End(xlUp).Row
    
    
    'Getting the Cell
    Set b = ActiveSheet.Buttons(Application.Caller).TopLeftCell
    
    'Set Cell value to nothing (There is a 1 placed there so we won't make hundreds of buttons if there are a button there already
    b.Value = ""
    
    
    'Copy the Name, Gépegység, Veszteség (Because they merging cells. - Bastards. )
    Set rgSource = Sheet23.Range(Cells(b.Row, 2), Cells(b.Row, 4))
    Set rgTo = Sheet25.Range("A" & lrto + 1)
    
    On Error GoTo CustLabel
    rgSource.Value = rgTo.Resize(1, rgSource.Columns.Count).Value
    On Error GoTo 0
'    rgSource.Copy
'    rgTo.PasteSpecial xlPasteValues
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hey! I think I managed to solve it.

So I added an array. Loaded the source range in to it. Then I resize the rngto to as many columns as in the rgfrom and set the values to be equal to whatever is in the array.

Will mark the changes in the code

VBA Code:
Sub ArchiveThisLine()
    
    Dim b As Range
    Dim lrto As Long, rgSource As Range, rgTo As Range, arr() As Variant '<---Dim the array
    
    Application.EnableEvents = False
    
    lrto = Sheet25.Range("A" & Rows.Count).End(xlUp).Row
    
    
    'Getting the Cell
    Set b = ActiveSheet.Buttons(Application.Caller).TopLeftCell
    
    'Set Cell value to nothing (There is a 1 placed there so we won't make hundreds of buttons if there are a button there already
    b.Value = ""
    
    
    'Copy the Name, Gépegység, Veszteség (Because we merging cells. - Nyilván. )
    Set rgSource = Sheet23.Range(Cells(b.Row, 2), Cells(b.Row, 4))
    Set rgTo = Sheet25.Range("A" & lrto + 1)
    
    On Error GoTo CustLabel
    arr() = rgSource.Value '<--Load the source range in to the array
    rgTo.Resize(1, rgSource.Columns.Count).Value = arr() '<--Resize the destination range and load the cells with the items in the array
    On Error GoTo 0
'    rgSource.Copy
'    rgTo.PasteSpecial xlPasteValues
 
Upvote 0
You should not need to pass the values through an array.

First though, to make your code more robust, I would suggest adding the coloured code shown here

Rich (BB code):
Set rgSource = Sheet1.Range(Sheet1.Cells(b.Row, 2), Sheet1.Cells(b.Row, 4))

The values should be able to be transferred to the new range thus:
VBA Code:
rgTo.Resize(1, rgSource.Columns.Count).Value = rgSource.Value
 
Upvote 0
Solution
You should not need to pass the values through an array.

First though, to make your code more robust, I would suggest adding the coloured code shown here

Rich (BB code):
Set rgSource = Sheet1.Range(Sheet1.Cells(b.Row, 2), Sheet1.Cells(b.Row, 4))

The values should be able to be transferred to the new range thus:
VBA Code:
rgTo.Resize(1, rgSource.Columns.Count).Value = rgSource.Value
Oh! Thanks very much for that!
 
Upvote 0
You're welcome.
Peter.

Can I just ask why in this instance it is not enough to describe the cell as a child of the parent (Sheet1.Range) object?

One would assume that you can define a cell as Sheet1.Range(cell(1,1)
And in the above instance b.row gives us the row number in the r1c1 address within the cell.
 
Upvote 0
Can I just ask why in this instance it is not enough to describe the cell as a child of the parent (Sheet1.Range) object?

One would assume that you can define a cell as Sheet1.Range(cell(1,1)
If it is ever possible that Sheet1 is not the active sheet when the code gets to that line, then it will error as Cells(1,1), without the sheet qualification immediately before it, will refer to cell A1 on the Active Sheet, not on Sheet1. So the code would be trying to put cell A1 from the active sheet into a range on Sheet1 - resulting in an error.

If, on the other hand it is absolutely certain that Sheet1 will be the active sheet when that line is executed, then you don't need Sheet1 as a qualifier anywhere, just this would do
VBA Code:
Set rgSource = Range(Cells(b.Row, 2), Cells(b.Row, 4))
 
Upvote 0
If it is ever possible that Sheet1 is not the active sheet when the code gets to that line, then it will error as Cells(1,1), without the sheet qualification immediately before it, will refer to cell A1 on the Active Sheet, not on Sheet1. So the code would be trying to put cell A1 from the active sheet into a range on Sheet1 - resulting in an error.

If, on the other hand it is absolutely certain that Sheet1 will be the active sheet when that line is executed, then you don't need Sheet1 as a qualifier anywhere, just this would do
VBA Code:
Set rgSource = Range(Cells(b.Row, 2), Cells(b.Row, 4))
Thanks.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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