VBA setting range using variables

mizogy

New Member
Joined
Jul 5, 2011
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hi All

I've run into a problem, clearly I am doing something wrong! I am trying to set a range using both columns and rows as variants.
Please see code below.
Any help, much appreciated.
Best, Mizogy


Dim FirstRow As Variant

Dim LastRow As Variant

Dim ColumnStart As Variant

Dim ColumnEnd As Variant


FirstRow = Range("A1").Value

LastRow = Range("B1").Value

ColumnStart = Range(“A2”).value

ColumnEnd = Range(“B2”).value


Range(ColumnStart & FirstRow + 1 & ":" & ColumnEnd & LastRow).ClearContents
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
What is actually in the cells the variables are referencing?
 
Upvote 0
To make code readable and easier to copy, follow suggestion in picture.
 

Attachments

  • Use Code Tags MrExcel.JPG
    Use Code Tags MrExcel.JPG
    50.2 KB · Views: 7
Upvote 0
Code:
Sub Maybe()
Dim FirstRow As Variant
Dim LastRow As Variant
Dim ColumnStart As Variant
Dim ColumnEnd As Variant
FirstRow = Range("A1").Value
LastRow = Range("B1").Value
ColumnStart = Range("A2").Value
ColumnEnd = Range("B2").Value
    Range(Cells(FirstRow + 1, ColumnStart), Cells(LastRow, ColumnEnd)).Select    '<----- Change to ClearContents when happy with the result
End Sub

BTW, the Dim's don't need to be variants. Single or Long works.
 
Upvote 0
Yes, but that's what I assumed. Might be wrong, as you alluded to.
I guess we will, or maybe not, hear from the OP.
 
Upvote 0
If your Column designations, Ranges A2 and B2, are alphabeticals, this should work.
Thanks to MARK858 for pointing this out.
Change the ".Select" to ".ClearContents" if satisfied that all works as needed.
Code:
Sub Maybe_2()
Dim FirstRow As Long
Dim LastRow As Long
Dim ColumnStart As String
Dim ColumnEnd As String
FirstRow = Range("A1").Value
LastRow = Range("B1").Value
ColumnStart = Range("A2").Value
ColumnEnd = Range("B2").Value
    Range(Cells(FirstRow + 1, Range([A2] & 1).Column), Cells(LastRow, Range([B2] & 1).Column)).Select
End Sub
 
Upvote 0
Range(Cells(FirstRow + 1, Range([A2] & 1).Column), Cells(LastRow, Range([B2] & 1).Column)
No need to make the changes in the line above, all you needed to change was the declarations as Cells accepts column letters
The code below would work fine
VBA Code:
Sub Maybe()
    Dim FirstRow As Long
    Dim LastRow As Long
    Dim ColumnStart As String
    Dim ColumnEnd As String

    FirstRow = Range("A1").Value
    LastRow = Range("B1").Value
    ColumnStart = Range("A2").Value
    ColumnEnd = Range("B2").Value
  
    Range(Cells(FirstRow + 1, ColumnStart), Cells(LastRow, ColumnEnd)).Select '<----- Change to ClearContents when happy with the result
End Sub
Test with the data below
Book1
ABC
1115
2EAC
3
Sheet2
 
Last edited:
Upvote 1
Solution
Sub Maybe() Dim FirstRow As Long Dim LastRow As Long Dim ColumnStart As String Dim ColumnEnd As String FirstRow = Range("A1").Value LastRow = Range("B1").Value ColumnStart = Range("A2").Value ColumnEnd = Range("B2").Value Range(Cells(FirstRow + 1, ColumnStart), Cells(LastRow, ColumnEnd)).Select '<----- Change to ClearContents when happy with the result End Sub
Thank you @MARK858 and @jolivanes for your help, much appreciated. Both suggestions worked based on either the column being alpha or numeric. Best, Mizogy.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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