1

Hi there i have create the following code to format the Grand Total Row in excel spreadsheet. My problem is that I want to select dynamic the cells from Grand Total and right because I don’t have always 15 columns. I make a try with ActiveCell but it didnt work. Can anyone help me to change this code to fit my need?

Range("A1").Select
FindRow1 = Range("A:A").FIND(What:="Grand Total", LookIn:=xlValues, LookAt:=xlWhole).Activate
ActiveCell.Resize(, 15).Select

'Range(ActiveCell, Cells(, ActiveCell.End(xlToRight).Column)).Select

Selection.Font.Bold = True

With Selection.Interior
    .Pattern = xlSolid
     .PatternColorIndex = xlAutomatic
     .ThemeColor = xlThemeColorAccent6
     .TintAndShade = 0.399975585192419
    .PatternTintAndShade = 0

End With

Selection.Font.Size = 12

[EDIT]: Here's a screenshot of my problem after trying suggested solutions:

enter image description here

Community
  • 1
  • 1
Vaggelis
  • 57
  • 1
  • 7
  • The following link won't *directly* solve your problem, but will help improve your VBA to the point that you will be able to solve such problems on your own: [How to avoid using Select in Excel VBA](https://stackoverflow.com/q/10714251/4996248) – John Coleman Feb 22 '19 at 13:09

2 Answers2

2
  1. You don't have to select the range. Just be sure of the address of the range you're using, and you're good.
  2. It'd be better if you specify the worksheet you're working with, so if you have multiple sheets in the workbook you'd still be working on the right one.
  3. Instead of activating the cell you found with Find, pass the row of that cell to a variable called myRow and use this variable in another function to define the range you need.
  4. Once you have defined the range you need, pass it to a variable like myRange, and use it instead of using Selection in the rest of your code.
  5. To make your range change its size dynamically (assuming you want your range to have one row and all the filled cells of that row), then you'll need to find the column of the last filled cell in your table, pass it to a variable lastCol and use it to define your range.
Sub formatRange()

    Dim ws As Worksheet
    Dim myRow As Long, lastCol As Integer, myRange As Range

    Set ws = ThisWorkbook.ActiveSheet  'Change this to the name of the sheet you're working with

    myRow = ws.Range("A:A").Find(What:="Grand Total", LookIn:=xlValues, LookAt:=xlWhole).Row 'The row that has "Grand Total"
    lastCol = ws.Cells(myRow, Columns.Count).End(xlToLeft).Column 'The column of the last filled cell in `myRow`

    Set myRange = ws.Range("A" & myRow).Resize(1, lastCol) 'The desired range has 1 row and (lastCol) columns

    myRange.Font.Bold = True

    With myRange.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0.399975585192419
        .PatternTintAndShade = 0
    End With

    myRange.Font.Size = 12

End Sub
  • Consider this case:

If the column of the last cell in the row myRow is NOT the same as the last column in the whole table (see screenshot), you have 2 choices to define your lastCol: Table Screenshot 1&2

  1. You can define lastCol as the last column of the row myRow (screenshot 1), and in that case you keep the code above as it is.
  2. You can define it as the last column of the whole table (screenshot 2), and in that case you'd have to replace the lastCol line above with this:
        'The column of the last filled cell in the whole table
        lastCol = ws.Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByColumns, _
                    SearchDirection:=xlPrevious).Column
    

P.S, If the column of the last cell is the same in all your rows, you can ignore this last paragraph.

NadAlaba
  • 292
  • 3
  • 15
  • Hi Nad None! Thank you very much for your help I have a lot of word and I didn’t test your code, but now I am try to run it works but I have problem with the last column the code : “lastCol = ws.Cells(myRow, "A").End(xlToRight).Column” I have 4 columns and it finds only 3 of the total row… why I must change something to your code – Vaggelis Feb 26 '19 at 07:03
  • If you would add a screenshot of your table it would give me some insight about what's wrong. But I'll tell you what this `lastCol` line of code does, and maybe it would help you out: It gives you the column -its number- of the last cell (right-most one) that is not empty in the row `myRow`. So your problem could be that the last cell in the row `myRow` isn't the last cell in your whole table. In that case you'd have to replace `myRow` in the `lastCol` line to the row that has the last cell in the whole table – NadAlaba Feb 26 '19 at 18:46
  • Hi Nad None! I have a sceenshot but how I put it to comment ? – Vaggelis Feb 27 '19 at 07:44
  • @Vaggelis You can either edit your question to put the screenshot there, or upload it to https://imgur.com/ and post the link here. – NadAlaba Feb 27 '19 at 14:56
  • Hi Nad! I edit my question and give you a sceenshot the problem is obvious, next mont I have one more column, I can understand why the code doesn’t go to the last column, I hope you understand ? – Vaggelis Feb 28 '19 at 14:27
  • @Vaggelis I see the problem now, it was happening because you had a blank cell in the middle of the row `myRow`. I edited my answer to fix the issue. – NadAlaba Feb 28 '19 at 16:41
1

Another example as to how to avoid Select/Selection/Activate/ActiveXXX pattern and how to use nested With...End With structure:

With Range("A1", Range("A:A").Find(What:="Grand Total", LookIn:=xlValues, LookAt:=xlWhole).End(xlToRight))

    With .Font
        .Bold = True
        .Size = 12
    End With 

    With .Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0.399975585192419
        .PatternTintAndShade = 0
    End With

End With 
DisplayName
  • 13,283
  • 2
  • 11
  • 19