Question regarding vba

Models in Finance

New Member
Joined
Jun 9, 2018
Messages
1
Hi all,

Can someone please help me out with this issue:

I have a 10x10 table in excel with random numbers.
I need to go through the rows and then through the columns,
until the numbers add up until the sum exceeds 25.

The last number needs to be in blue font, boldfaced and with a red border (using a With statement).

I need to do this with VBA.

Thank you very much!!

Model in Finance
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Assuming your table starts at B2 :
Code:
Sub ID()
Dim rng As Range, fr%, fc%, r%, c%, cel As Range, x&
Set rng = [B2].Resize(10, 10)
fr = rng(1).Row
fc = rng(1).Column
With rng
    .Borders.LineStyle = xlNone
    .Font.ColorIndex = xlAutomatic
    .Font.Bold = False
End With
For r = fr To fr + 10
    For c = fc To fc + 10
        Set cel = Cells(r, c)
        x = x + cel
        If x > 25 Then
            With cel
                .Font.Color = RGB(0, 0, 255)
                .Font.Bold = True
                With .Borders
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = 3
                End With
            End With
            Exit Sub
        End If
    Next
Next
End Sub

The above goes across the first row, then across the second row, etc.
 
Last edited:
Upvote 0
Maybe (starting at A1)...

Rich (BB code):
Sub sum25plus()
    Dim i As Long, j As Long, x As Long
    
    x = 0
    
    For i = 1 To 10
        For j = 1 To 10
            x = x + Cells(i, j).Value
            If x > 25 Then
                With Cells(i, j)
                    .Font.ColorIndex = 5
                    .Font.Bold = True
                    .Borders.ColorIndex = 3
                    Exit Sub
                End With
            End If
        Next
    Next
End Sub

Edit: Seeing the code by footoo I really should have also put in code to clear the formats first as well :(
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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