Simple VBA Question - Define a range based on a variable

kgallego

Board Regular
Joined
Jul 26, 2011
Messages
82
Office Version
  1. 365
Hi all,

I'm writing code to clear the contents of a range of cells. The tricky part is that the range depends on a variable. I want the code below to clear the contents of a range from column "CAR" row 3. My starting example is:

Dim CAR as double
CAR = Sheet1.cells(1,1).value

Sheet1.Range(????, "H20").ClearContents


Any ideas how I should write the first range perameter to refer to the variable as a column?

Thanks!

Kelsey
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Instead of referring to ranges like this:
Range("H20")
you can use Cells(row,column) where the column reference can be numeric or a letter.
Cells(20,"H")
or
Cells(20,8)

If you want to clear the column designated by column variables "CAR" from rows 1 to 20, you could do so like this:
Code:
Range(Cells(1,CAR), Cells(20,CAR)).ClearContents
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,240
Members
448,555
Latest member
RobertJones1986

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