Cannot execute multiple Range in a Sub

techissue2008

Board Regular
Joined
Jun 13, 2008
Messages
80
Hi

I tried the following code but it shows an error:

Code:
Sub test2col()
Sheets("Sheet6").Cells.ClearContents
Sheets("Sheet6").Range("A:B").Value = Sheets("Sheet1").Range("A:B").Value
Sheets("Sheet6").Range("D").Value = Sheets("Sheet1").Range("C").Value
End Sub

It said
Code:
Sheets("Sheet6").Range("D").Value = Sheets("Sheet1").Range("C").Value
End Sub

has error.

Does a Sub not support to use Range more than one time? If so, do I need to write many Sub to do it?

Thanks for advance.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Range("D") or ("C") are not valid ranges...

Just C or D are no cells, so you can't refer to them. If you want to copy columns, use Range("D:D") and ("C:C").
 
Upvote 0
Thank you for your help.

May I ask do you know whether VBA can write comment?

e.g. //, <!-- -->, #

Does it support it?
 
Upvote 0
Sheets("Sheet6").Range("D").Value = Sheets("Sheet1").Range("C").Value
Rich (BB code):
Sheets("Sheet6").Columns("D").Value = Sheets("Sheet1").Columns("C").Value
 
Upvote 0
Try the below. You need to reference a column as "C:C" not "C"
Code:
Sub test2col()
Sheets("Sheet6").Cells.ClearContents
Sheets("Sheet6").Range("A:B").Value = Sheets("Sheet1").Range("A:B").Value
Sheets("Sheet6").Range("D:D").Value = Sheets("Sheet1").Range("C:C").Value
End Sub
 
Upvote 0
Lost you a bit with #,./ etc

Do you mean can you do this?
Code:
Sub test()
Range("B6") = "#"
End Sub
 
Upvote 0
If you mean comments in your own code, prefix your comments with ' or #

'This is a comment
This is not a comment
#This is a comment

If you mean comments in cells, use:
Code:
Range("B6").AddComment.Text "This is a comment"</pre>
 
Upvote 0
No, I just want to write a comment to remark it.

Code:
Sub test1()
Sheets("Sheet6").Cells.ClearContents
Sheets("Sheet6").Range("A:A").Value = Sheets("Sheet1").Range("D:D").Value // retireve one column data need "A:A"
End Sub

I just want to add a comment or phase out some code by /* ... */
 
Upvote 0
Code:
Sub test1()
Sheets("Sheet6").Cells.ClearContents
Sheets("Sheet6").Range("A:A").Value = Sheets("Sheet1").Range("D:D").Value ' retireve one column data need "A:A"
End Sub</pre>
</pre>
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,585
Members
448,972
Latest member
Shantanu2024

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