Adding Formulae and Arrows to Macro

RichJW

Board Regular
Joined
Jul 7, 2015
Messages
94
Hi all,

I hope you can help with this. I’ll try to explain as well as I can.

I’ve a macro based report pulling in data from numerous spreadsheets into one.

In my “Values” tab, I have data in columns D and E, which equal either Red, Amber, Green or Blue. This data starts from row 2 in each column, but the end row changes each week with the amount of data pulled in (columns A, B, D, E will always have data in them).

I wish to compare the number of Reds in column D with the number of Reds in column E, the number of Ambers in column D with the number of Ambers in E, etc. for Green and Blue as well.
Ideally, then I would like to have Red/Green arrows to indicate whether the amount in column D is greater or less than that in E for each of the 4 colours.

I’m having problems inputting formulas into the macro, as I have only inputted text before, so it shows the “=countif” text rather than the formula.

Have any of you wonderful, knowledgeable people got any jazzy idea as to how this can be made to work and look good?

Thanks,
Rich
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
When you say columns D & E contains Red, Amber, Green, or Blue; are you saying this is text in those columns or the cell color or what?
 
Upvote 0
Hi Roderick,

Both. It shows the text "Red", "Green" etc, then the cell is coloured the same colour.

Thanks,
Rich
 
Upvote 0
Hi Rich, in this case could put 4 rows above where the data starts, a row for each color and then use code like this:

Code:
="RED="&COUNTIF(D3:D65536,"red")

Obviously "Blue"= and so on. Unless you anticipate more that 65536 rows this would work for new additions.
 
Upvote 0
Here is some VBA to get you started
- paste into a new standard module

A worksheet is added containing a small table based on values in columns D & E of sheet "Values"

Code:
Private countE As Integer, countD As Integer    'must be above all procedures in module

Sub Test()
    Dim r As Long, colour As Variant, colD As Range, colE As Range
    Set colD = Sheets("Values").Range("D:D")
    Set colE = colD.Offset(, 1)
    r = 1
    Set ws = Worksheets.Add
    ws.Range("B1:D1").Value = Array("D", "E", "Icon")
    
    For Each colour In Array("Red", "Amber", "Green", "Blue")
        r = r + 1
        With WorksheetFunction
            countD = .CountIf(colD, colour)
            countE = .CountIf(colE, colour)
        End With
            
        With ws
            .Cells(r, 1) = colour
            .Cells(r, 2) = countD
            .Cells(r, 3) = countE
            Call AddArrow(.Cells(r, 4))
        End With
    Next
    ws.Cells(1).CurrentRegion.HorizontalAlignment = xlCenter
End Sub
Code:
Private Sub AddArrow(cel As Range)
    Dim f As Variant, c As String
    Select Case countD
        Case Is = countE
            f = vbBlack
            c = Chr(134)
        Case Is > countE
            f = vbGreen
            c = Chr(230)
        Case Is < countE
            f = vbRed
            c = Chr(230)
    End Select
    
    With cel
        .Font.Name = "Wingdings 3"
        .Font.Color = f
        .Value = c
    End With
    
End Sub
 
Last edited:
Upvote 0
Thanks Yongle, that looks really good.

Instead of adding it to a new sheet, how could I get the macro to paste it into the next available cell in column F of the Values tab?
Thanks,
Rich
 
Upvote 0
Replace Test1 with this
Code:
Sub Test2()
    Dim rr As Long, r As Long, colour As Variant, colD As Range, colE As Range, ws As Worksheet
    Set ws = Sheets("Values")
    Set colD = ws.Range("D:D")
    Set colE = colD.Offset(, 1)
    rr = ws.Range("F" & Rows.Count).End(xlUp).Row + 1
    r = rr
    For Each colour In Array("Red", "Amber", "Green", "Blue")
        r = r + 1
        With WorksheetFunction
            countD = .CountIf(colD, colour)
            countE = .CountIf(colE, colour)
        End With
            
        With ws
            .Cells(r, 3) = colour
            .Cells(r, 4) = countD
            .Cells(r, 5) = countE
            Call AddArrow(.Cells(r, 6))
        End With
    Next
    ws.Cells(rr, 3).CurrentRegion.HorizontalAlignment = xlCenter
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
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