Selecting a column using a variable

Linda21

New Member
Joined
Aug 2, 2010
Messages
38
I am trying to set an entire column as a variable. I want to set the column variable as the 3rd column. When running the code, I want to select the entire column 3 by using the variable. Then I want to increase each column by 1 and run code again.
Is there something wrong with the variable below?
How do I select the entire column when using a variable?

PasteColumn = 3

Thank you.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Use a range object variable for ranges:
Code:
Sub test()
Dim c As Range
Set c = Range("C:C")
c.Select
End Sub
 
Upvote 0
Thanks for the response...however I get Run-time error '91': Object variable or With block variable not set. Any ideas?

Dim c As Range
Set c = Range("C:C")

Sheets("Self").Select
Columns("J:J").Select
Selection.Copy
Sheets("Summary").Select
c.Select
 
Upvote 0
Try this:
Code:
Dim MyVariable As Long
 
      MyVariable = 3
 
      Sheets("Self").Columns("J:J").Copy Sheets("Summary").Columns(MyVariable)
That's kind of a guess.

What I think you are trying to do is copy column J from the sheet Self to column C of sheet Summary.
 
Upvote 0
Thanks for the response...however I get Run-time error '91': Object variable or With block variable not set. Any ideas?

Dim c As Range
Set c = Range("C:C")

Sheets("Self").Select
Columns("J:J").Select
Selection.Copy
Sheets("Summary").Select
c.Select
Which sheet are you wanting your c range to be defined on?

As it is you seem to be defining c as a range rather than as a variable, so if you're not clear what you want it's likely the code may become confused.

In your above snippet, move the line
Set c = Range("C:C")
to immediately below
Sheets("Summary").Select
and see if it helps.

Incidentally, too much Select commands in VBA coding is generally not a good idea.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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