Problems with formatting last row with data from column A to S

awasi

New Member
Joined
Jun 16, 2013
Messages
4
Hey guys,

I hope you can help me since I cannot seem to find the piece of code necessary to perform the following action.
Have tried multiple last-row codes but nothing worked.
I use Excel 2007 with Windows XP

I have a set of data ranging from column A to S. The amount of collumns varies.
Already managed to pull sums/averages in the first empty cell per column. This row (with the sums and averages) needs to be in different colours now, just like the first row in the sheet.
The first row was pretty easy and looks like that:

Range("A1:S1").Select With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.499984740745262
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With

I need to perform the same formatting for the last row with data (sums/averages). However, since there is only data in column A to S and the amount of rows varies, I need a piece of code that only selects the last row from column A to S.

I appreciate your help very much!

Regards
Awasi
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Consider this previous posting of mine. For your particular question, change the Cells to Columns("A:S")....

For last value
---------------
LastUsedRow = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row

LastUsedCol = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column

For last value or formula
------------------------------
LastUsedRow = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row

LastUsedCol = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlFormulas).Column

A note about those headings... the ones labeled "For last value" means it will return the last used row and column for a displayed value (whether a constant or one from a formula) even if there are formulas after those cells displaying the empty string (""); whereas those labeled "For last value or formula" returns the last cell with either a value or a formula even if that formula is displaying the empty string ("").
 
Upvote 0
Hey,
thank you very much for your reply.
Could you tell me how to connect the two so that I select the last row from column A to S?
I have tried several variations of the below bold line but cant seem to find the right one.
I am a real beginner, so it would be great if you could help me out once more.

LastUsedRow = ActiveSheet.Columns("A:S").Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row
LastUsedCol = ActiveSheet.Columns("A:S").Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column
Range("LastUsedRow:LastUsedCol").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.499984740745262
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
 
Upvote 0
I am sure Rick won't mind me jumping in... try
Code:
Sub test()
    Dim LastUsedRow As Long, LastUsedCol As Long


    LastUsedRow = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlRows, _
                                         SearchDirection:=xlPrevious, LookIn:=xlValues).Row


    LastUsedCol = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlByColumns, _
                                         SearchDirection:=xlPrevious, LookIn:=xlValues).Column


    With Range(Cells(LastUsedRow, 1), Cells(LastUsedRow, LastUsedCol))
        With .Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.499984740745262
            .PatternTintAndShade = 0
        End With
        .Font.ThemeColor = xlThemeColorDark1
    End With
End Sub
 
Upvote 0
Wow, works perfectly fine.
I would have, by no means, been able to firgure that out myself.
Thank you very much guys! You saved my day :)
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,930
Members
452,367
Latest member
TePunaBloke

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