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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

dafan

Well-known Member
Joined
May 6, 2008
Messages
692
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

techissue2008

Board Regular
Joined
Jun 13, 2008
Messages
80
Thank you for your help.

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

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

Does it support it?
 
Upvote 0

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
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

sbeatton

Active Member
Joined
May 19, 2004
Messages
411
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

sbeatton

Active Member
Joined
May 19, 2004
Messages
411
Lost you a bit with #,./ etc

Do you mean can you do this?
Code:
Sub test()
Range("B6") = "#"
End Sub
 
Upvote 0

dafan

Well-known Member
Joined
May 6, 2008
Messages
692
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

techissue2008

Board Regular
Joined
Jun 13, 2008
Messages
80
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

dafan

Well-known Member
Joined
May 6, 2008
Messages
692
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,191,097
Messages
5,984,650
Members
439,898
Latest member
Zack0611

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