Select range based on column & row values

phad012

New Member
Joined
Oct 28, 2017
Messages
5
Hello,

I have table like the one below. I'd like a macro to select a range in that table which I specify.

For example:


  • if I type 'Budget' in cell B2 and 'Quarter 4' in cell B3, I'd like the macro to select range F11:F14; or


  • if I type 'Actual' in B2 and 'Quarter 2' in B3, I'd like the macro to select range D6:D10

ABCDEF
1
2Row input:Budget
3Column input:Quarter 4
4
5LineTypeQuarter 1Quarter 2Quarter 3Quarter 4
6RevenueActual
7Personnel costsActual
8Admin costsActual
9R&D costsActual
10ProfitActual
11RevenueBudget
12Personnel costsBudget
13Admin costsBudget
14R&D costsBudget

<tbody>
</tbody>

I hope this makes sense. Any guidance appreciated :)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This short macro works for this table.
Obviously you are going to have to adjust some values as you lengthen the ranges.
Code:
Sub GetRange()
Dim rw As Long
Dim cl As String
If Range("B2").Value = "Budget" Then
rw = 11
Else
rw = 6
End If
cl = Chr(66 + Right(Range("B3").Value, 1))
Range(cl & rw & ":" & cl & rw + 4).Select
End Sub

In case you are wondering, cl starts with CHR(66) which refers to "B". It is increased by the value (1 to 4) of the Quarter that you enter.
 
Upvote 0
This short macro works for this table.
Obviously you are going to have to adjust some values as you lengthen the ranges.
Code:
Sub GetRange()
Dim rw As Long
Dim cl As String
If Range("B2").Value = "Budget" Then
rw = 11
Else
rw = 6
End If
cl = Chr(66 + Right(Range("B3").Value, 1))
Range(cl & rw & ":" & cl & rw + 4).Select
End Sub

In case you are wondering, cl starts with CHR(66) which refers to "B". It is increased by the value (1 to 4) of the Quarter that you enter.

Thanks Brian, much appreciated.

I probably should have mentioned my data set is a bit longer. What I I copied above was a simplification.

The real data set is around:


  • 100 columns - the quarters are for a number of years E.g. Q1 2007, Q2 2014, etc (not just Q1-Q4)
  • 5000 rows - there are around 20 row categories. Not just 'Actual' and 'Budget', but also things like Revised Budgets, Draft Actuals, Plans set at varying dates

So it'd be great to get something which is suited to a larger data set.

Many thanks
 
Upvote 0
Possibly....

Code:
Sub SelectRng()
    Dim x As Long, y As Long, z As Long

    x = Range(Cells(5, 2), Cells(5, Cells(5, Columns.Count).End(xlToLeft).Column)).Find(What:=Range("B3").Value, After:=Range("B5"), LookIn:=xlValues, _
                            LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column

    With Range("B5:B" & Range("A" & Rows.Count).End(xlUp).Row)

        y = .Find(What:=Range("B2").Value, After:=Range("B5"), LookIn:=xlValues, _
                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row

        z = .Find(What:=Range("B2").Value, After:=Range("B5"), LookIn:=xlValues, _
                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    End With

    Range(Cells(y, x), Cells(z, x)).Select

End Sub
 
Upvote 0
Works perfect thanks so much!

That's my first post on this forum and I'm hugely impressed with the quick responses to a problem which I thought would take ages to solve.

Saved me a lot of time and I'm very grateful, thank you

Possibly....

Code:
Sub SelectRng()
    Dim x As Long, y As Long, z As Long

    x = Range(Cells(5, 2), Cells(5, Cells(5, Columns.Count).End(xlToLeft).Column)).Find(What:=Range("B3").Value, After:=Range("B5"), LookIn:=xlValues, _
                            LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column

    With Range("B5:B" & Range("A" & Rows.Count).End(xlUp).Row)

        y = .Find(What:=Range("B2").Value, After:=Range("B5"), LookIn:=xlValues, _
                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row

        z = .Find(What:=Range("B2").Value, After:=Range("B5"), LookIn:=xlValues, _
                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    End With

    Range(Cells(y, x), Cells(z, x)).Select

End Sub
 
Upvote 0
Welcome to the forum and happy it helped.

The code could be shortened a bit to make it tidier but one thing you should really do is change the 3 places xlPart appears to xlWhole as it is more appropriate for what you are doing (I was a bit lazy and recorded some of the code to start with :biggrin:).
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,048
Members
449,206
Latest member
Healthydogs

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