ChloeLM
New Member
- Joined
- Apr 12, 2021
- Messages
- 5
- Office Version
- 365
- 2019
- 2016
- Platform
- Windows
- Mobile
- Web
Hello everyone
I'm a VBA novice who has muddled through a piece of code and got it working. However it's very long (and slow!) and i'm looking for some advice on how to make it more elegant (and efficient!) in order to speed up my procedure. Am i using an efficient way to loop through the procedure? Is there a more efficient way to refer to the range other than guessing what the maximum range might be (I tried to incorporate Range("A4:A4").End(xlDown) unsuccessfully)? Is there an optimal order to ask it to do things in which speeds things up?
I have a financial profit and loss spreadsheet where column A contains row headings/sub headings, and column B:E contains corresponding numbers - one financial year in each column. It is normally between 120 and 500 rows long. The spreadsheet is a CSV which is exported from another system, some of the layout is predictable, some is different each time. In particular the main row headings/totals have predictable text, while the rows in between are variable. I need to format the predictable rows.
E.g. In column A there are 64 possible main row headings that might appear (they are not all used each time). These are predictable and I have defined them as constants in the declarations section e.g. Const myLabel7="Staff Costs" and so on for all 64. I then have subheadings under these that have numbers adjacent to them, and where the text in column A is always different. For example, underneath Staff Costs I could have any number of variations - 'paid labour', 'salaries' all of which do have corresponding number amounts. There are also 64 possible 'total' headings that might appear, which correspond to the main row headings e.g. 'Staff Costs Total'. Again i've defined them as constants in the declarations section e.g. Const myLabelTotal7="Staff Costs Total"
In the first draft of my VBA programme I have managed to get it to format the rows in the way that I want but it's slow. I have used the code below (which shows the first 2 blocks, there are another 126 blocks like this), and it is looking for particular text, deciding what type of heading it is based on whether there are adjacent numbers, and formatting it accordingly (i have 5 different versions of the row format e.g. myHeight, myHeight1, myHeight2 - and corresponding myTint, myItalics etc):
All advice appreciated!
Cheers
Chloe
I'm a VBA novice who has muddled through a piece of code and got it working. However it's very long (and slow!) and i'm looking for some advice on how to make it more elegant (and efficient!) in order to speed up my procedure. Am i using an efficient way to loop through the procedure? Is there a more efficient way to refer to the range other than guessing what the maximum range might be (I tried to incorporate Range("A4:A4").End(xlDown) unsuccessfully)? Is there an optimal order to ask it to do things in which speeds things up?
I have a financial profit and loss spreadsheet where column A contains row headings/sub headings, and column B:E contains corresponding numbers - one financial year in each column. It is normally between 120 and 500 rows long. The spreadsheet is a CSV which is exported from another system, some of the layout is predictable, some is different each time. In particular the main row headings/totals have predictable text, while the rows in between are variable. I need to format the predictable rows.
E.g. In column A there are 64 possible main row headings that might appear (they are not all used each time). These are predictable and I have defined them as constants in the declarations section e.g. Const myLabel7="Staff Costs" and so on for all 64. I then have subheadings under these that have numbers adjacent to them, and where the text in column A is always different. For example, underneath Staff Costs I could have any number of variations - 'paid labour', 'salaries' all of which do have corresponding number amounts. There are also 64 possible 'total' headings that might appear, which correspond to the main row headings e.g. 'Staff Costs Total'. Again i've defined them as constants in the declarations section e.g. Const myLabelTotal7="Staff Costs Total"
In the first draft of my VBA programme I have managed to get it to format the rows in the way that I want but it's slow. I have used the code below (which shows the first 2 blocks, there are another 126 blocks like this), and it is looking for particular text, deciding what type of heading it is based on whether there are adjacent numbers, and formatting it accordingly (i have 5 different versions of the row format e.g. myHeight, myHeight1, myHeight2 - and corresponding myTint, myItalics etc):
VBA Code:
Sub FormatHeadingsMyLabels()
'Looks for text and highlights the entire row.
Range("A1").Activate
For Each cell In Range("A4:A500")
'***VARIABLE1***
If InStr((cell.Value), myLabel1) <> 0 Then 'if cell name *contains* then do something
'***VARIABLE1***
cell.Offset(0, 5).Activate 'go 5 cells to the right
Select Case IsEmpty(ActiveCell) 'is this cell empty?
Case False 'no [ignore?]
Case True 'yes - the do the thing on the next line
ActiveCell.Offset(0, 1).Activate 'activate the next cell along
ActiveCell = 1 'and put 1 in it
ActiveCell.EntireRow.Select
Selection.Font.Italic = myitalics 'make it italic
Selection.RowHeight = myHeight 'change the row height
Selection.Font.Bold = myBold 'make the row bold
With Selection.Font 'change the row font and colour
.ThemeColor = mycolour
.TintAndShade = mytint
.Size = mySize
End With
With Selection
.AddIndent = True
.IndentLevel = myindent
End With
End Select
End If
Next
For Each cell In Range("A4:A500")
'***VARIABLE2***
If InStr((cell.Value), myLabel2) <> 0 Then 'if cell name *contains* then do something
'***VARIABLE2***
cell.Offset(0, 5).Activate 'go 5 cells to the right
Select Case IsEmpty(ActiveCell) 'is this cell empty?
Case False 'no [ignore?]
Case True
ActiveCell.Offset(0, 1).Activate 'activate the next cell along
ActiveCell = 1 'and put 1 in it
'yes - the do the thing on the next line
ActiveCell.EntireRow.Select
Selection.Font.Italic = myitalics 'make it italic
Selection.RowHeight = myHeight 'change the row height
Selection.Font.Bold = myBold 'make the row bold
With Selection.Font 'change the row font and colour
.ThemeColor = mycolour
.TintAndShade = mytint
.Size = mySize
End With
With Selection
.AddIndent = True
.IndentLevel = myindent
End With
End Select
End If
Next
All advice appreciated!
Cheers
Chloe