Copying Values Using Macro

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a macro and I simply want to copy the values from the "Current_Parts" range, which is in another worksheet and to paste it into the "Paste_Area1" range which is in the current worksheet and is of the same shape and size.

I thought I could do it as easy as in the Range("Paste_Area1") = Sheets("Quotes Database").Range("Current_Parts") line below, but it does not work.



Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address(False, False) = "D5" Then


Range("Paste_Area1") = Sheets("Quotes Database").Range("Current_Parts")


End If

End Sub


Can anyone help? Do I need to use a PasteValues type command?

Thanks,

MikeG
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I have a macro and I simply want to copy the values from the "Current_Parts" range, which is in another worksheet and to paste it into the "Paste_Area1" range which is in the current worksheet and is of the same shape and size.

I thought I could do it as easy as in the Range("Paste_Area1") = Sheets("Quotes Database").Range("Current_Parts") line below, but it does not work.



Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address(False, False) = "D5" Then


Range("Paste_Area1") = Sheets("Quotes Database").Range("Current_Parts")


End If

End Sub


Can anyone help? Do I need to use a PasteValues type command?

Thanks,

MikeG

Put the Current_Parts Range values into an array as follows :

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim RangeArray As Variant

    If Target.Address(False, False) = "D5" Then
    
        RangeArray = Sheets("Quotes Database").Range("Current_Parts")
        Range("Paste_Area1") = RangeArray
    
    
    End If

End Sub
 
Upvote 0
Hello Jaafar,

Is there a reason why you use the RangeArray? Why not simply:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(False, False) = "D5" Then _
        Range("Paste_Area1") = Sheets("Quotes Database").Range("Current_Parts")
End Sub

or even


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0,0) = "D5" Then [Paste_Area1] = [Quotes Database!Current_Parts]
End Sub

:-)
 
Upvote 0
wigi.

That would'nt work because the line :
Code:
Sheets("Quotes Database").Range("Current_Parts")
returns a Range object not an array of values.

Putting the Range into an intermediary variant array coerces the result into an array of values.


Alternatively, one could do the same using the Value Property of the Range object as follows :

Code:
Range("Paste_Area1") = Sheets("Quotes Database").Range("Current_Parts")[COLOR=Red][B].Value[/B][/COLOR]
 
Upvote 0
Indeed, I had to insert the .Value, you were right.
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,461
Members
452,915
Latest member
hannnahheileen

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