Variables in a range

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
202
Office Version
  1. 2016
Platform
  1. Windows
I have this code, that works fine, BUT the range(“E5:E1000”) and Range("T5:T1000") are set to fixed ranges [5 to 1000].


VBA Code:
Dim R As Long

R = WorksheetFunction.Index(ShGE03.Range("E5:E1000"), WorksheetFunction.Match(ShGE03.Range("A5"), ShGE03.Range("T5:T1000"), 0))

I’m trying to update this program so those ranges reference a variable (RowData) that is

VBA Code:
Dim ws As Worksheet
        Set ws = ShGE03  
Dim RowDataStart As Long

RowDataStart = 5

Dim RowDataEnd As Long

RowDataEnd = Cells(Rows.count, 5).End(xlUp).Row

Dim RowData As Long

For RowData = RowDataStart To RowDataEnd

My last attempt, that resulted in an error, was

VBA Code:
R = WorksheetFunction.Index(ws.Range(ws.Cells(RowData, 5)), WorksheetFunction.Match(ws.Range(ws.Cells(1, 5)), ws.Range(ws.Cells(RowData, 20), 0)))



Any Help or suggestions
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
VBA Code:
    R = WorksheetFunction.Index(ShGE03.Range("E5:E1000"), WorksheetFunction.Match(ShGE03.Range("A5"), ShGE03.Range("T5:T1000"), 0))
If you use range variables you can rewrite your original fixed range formula as
VBA Code:
    Dim CellRange1 As Range, CellRange2 As Range, CellRange3 As Range

    Set CellRange1 = ShGE03.Range("E5:E1000")
    Set CellRange2 = ShGE03.Range("A5")
    Set CellRange3 = ShGE03.Range("T5:T1000")

    R = WorksheetFunction.Index(CellRange1, WorksheetFunction.Match(CellRange2, CellRange3, 0))

Then it's just a matter of redefining the range variables for a new range

VBA Code:
    Dim CellRange1 As Range, CellRange2 As Range, CellRange3 As Range
    Dim ws As Worksheet
        Set ws = ShGE03
        
    With ws
        Set CellRange1 = .Range("E5", .Range("E" & .Rows.Count).End(xlUp))    'range to last cell in column E w/data
        Set CellRange2 = .Range("E1")
        Set CellRange3 = .Range("T5", .Range("T" & .Rows.Count).End(xlUp))    'range to last cell in column T w/data
    End With

    R = WorksheetFunction.Index(CellRange1, WorksheetFunction.Match(CellRange2, CellRange3, 0))

(not tested).
 
Upvote 1
Solution
VBA Code:
    R = WorksheetFunction.Index(ShGE03.Range("E5:E1000"), WorksheetFunction.Match(ShGE03.Range("A5"), ShGE03.Range("T5:T1000"), 0))
If you use range variables you can rewrite your original fixed range formula as
VBA Code:
    Dim CellRange1 As Range, CellRange2 As Range, CellRange3 As Range

    Set CellRange1 = ShGE03.Range("E5:E1000")
    Set CellRange2 = ShGE03.Range("A5")
    Set CellRange3 = ShGE03.Range("T5:T1000")

    R = WorksheetFunction.Index(CellRange1, WorksheetFunction.Match(CellRange2, CellRange3, 0))

Then it's just a matter of redefining the range variables for a new range

VBA Code:
    Dim CellRange1 As Range, CellRange2 As Range, CellRange3 As Range
    Dim ws As Worksheet
        Set ws = ShGE03
       
    With ws
        Set CellRange1 = .Range("E5", .Range("E" & .Rows.Count).End(xlUp))    'range to last cell in column E w/data
        Set CellRange2 = .Range("E1")
        Set CellRange3 = .Range("T5", .Range("T" & .Rows.Count).End(xlUp))    'range to last cell in column T w/data
    End With

    R = WorksheetFunction.Index(CellRange1, WorksheetFunction.Match(CellRange2, CellRange3, 0))

(not tested).
Works GREAT
THANKS
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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