CopyPaste function with changing range

GerrardSVK

New Member
Joined
Sep 18, 2023
Messages
29
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

can anyone help me? I need to make something very simmilar like code I wrote but I need to change argument F6:F7. So there will be sheet where I need to put exact range and this range will be used in this macro instead of this anytime when I change it. I was thinking about change a cell content into string and then use it in that argument can it be correct (see second code). I need to do some kind of logic like in second code.


VBA Code:
Sub Makro4()
 
   Workbooks(2).ActiveSheet.Range("F6:F7").Copy
   Workbooks(1).Sheets(1).Range("F6:F7").PasteSpecial xlPasteValues
 
 
End Sub

Code:
Sub Makro1()

    Dim Range1 As String

    Range1 = ThisWorkbook.Range("C6").Value
    
    Dim Range2 As String

    Range2 = ThisWorkbook.Range("C5").Value
 

     Workbooks(2).Sheets(1).Range("Range1").Copy

     Workbooks(3).Sheets(1).Range("Range2").PasteSpecial xlPasteValues

 

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Just remove the quotes round the variables:

VBA Code:
     Workbooks(2).Sheets(1).Range(Range1).Copy

     Workbooks(3).Sheets(1).Range(Range2).PasteSpecial xlPasteValues
 
Upvote 0
Just remove the quotes round the variables:

VBA Code:
     Workbooks(2).Sheets(1).Range(Range1).Copy

     Workbooks(3).Sheets(1).Range(Range2).PasteSpecial xlPasteValueDo
[/QUOTE]
Do not work for me point is that I will have in cell C6 value for example H9 and in cell C5 there will be written H38. So If I run macro then it will copy range of cells in another workbook by written values in cells C5 and C6 So if i overwrite it from H9 to H15 it will copy range H15:H38. How to perform this task?
 
Upvote 0
I missed that you hadn't included a worksheet when getting the values - for example:

Rich (BB code):
    Range1 = ThisWorkbook.Sheets("some sheet name here").Range("C6").Value
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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