Selecting variable ranges using VBA

VBilgen

New Member
Joined
Feb 4, 2014
Messages
2
Hello,

I have to run a fairly tedious task of conditional formatting for large tables. The catch is that I have to run this formatting one column at a time and within sections. Obviously in small datasets, this is fine, but I am trying to get vba to do this for me since my tables can get quite large. For Example, I would want conditional formatting to show the highest Truth Score and the Highest Energy Score for VPs, and then for directors and then for Managers, and then for analysts:

(average for each)VPDirectorManagerAnalyst
Truth Question 11356
Truth Question 2
2427
Truth Question 33136
Energy Question 15178
Energy Question 22671
Energy Question 32903
Energy Question 43468

<tbody>
</tbody>

In the example above, I start with Truth Questions for VPs (3 cells), and apply formatting. Then I go to Directors, etc. After truth questions are done, I do the same for the 4 energy questions.

Each time I make these tables the number of questions per topic could change, as well as the number of breakouts (maybe 5 locations instead of by Position, etc). I would like to be able to just highlight a whole table and then run some vba and it will walk through the table for me, instead of having to manually create something like the above each time. Also, the actual types of conditional formatting I will be doing are more complex, but I know how to make those using vba. I just need help correctly finding the formatting ranges and looping through.

I greatly appreciate you pointing me in the right direction, as I am having a tough time getting this off the ground, although I know it's possible...
Thanks!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hello,

As a follow up, I've figured out how to highlight one section (i.e. All truth questions from VP to Analyst) and have the code step through each column to apply conditional formatting:

Code:
Dim lRow As LongDim rngObj As Range
Dim rngString As String


Set rngObj = Selection


FirstRow = rngObj.Row
LastRow = FirstRow + rngObj.Rows.Count - 1


FirstCol = rngObj.Column
LastCol = FirstCol + rngObj.Columns.Count - 1


For lCol = FirstCol To LastCol
    Worksheets("Sheet1").Range(Cells(FirstRow, lCol), Cells(LastRow, lCol)).Select
    Call HeatMap
Next lCol


End Sub

This still leaves me with having to select all Truth questions, run code, then select all Energy questions and run macro. If there is a way to select all data and have the code differentiate between the two section, I am very interested. Thank you again for your help!
 
Upvote 0
I've started using the cells property quite a bit, because I define row and column with a number. Mixing it with range to define selection/array corners has simplified things for me. The following code will set your arrays and then do what you need to with each array. (I subbed changing interior colors to make sure it ran correctly, so ignore that.) This assumes answers start in column 2, adjust as need be, and contiguous cells in the table (no blanks).
Code:
Sub select_ranges()
    truthfirstrow = Cells.Find(what:="Truth*", After:=[A1], Searchorder:=xlNext).Row
    energyfirstrow = Cells.Find(what:="Energy*", After:=[A1], Searchorder:=xlNext).Row
    lastcol = Range("A1").End(xlToRight).Column
    
    Set trutharray = Range(Cells(truthfirstrow, 2), Cells(energyfirstrow - 1, lastcol))
    Set energyarray = Range(Cells(energyfirstrow, 2), Cells(energyfirstrow, 2).End(xlDown).End(xlToRight))
    
    With trutharray.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent1
        .TintAndShade = 0.399975585192419
        .PatternTintAndShade = 0
    End With
    With energyarray.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0.399975585192419
        .PatternTintAndShade = 0
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,945
Messages
6,127,840
Members
449,411
Latest member
adunn_23

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