Need Help Deciphering this Code

lexbung

New Member
Joined
Aug 4, 2017
Messages
5
Hello,

I just had to learn VBA for my internship this summer due to a project I had to do. The project is basically figuring out how a pre-built model works. Thanks to Google and previous questions/guides in this forum, I've been able to figure out the bulk of the model, except one Sub (below).

I understand the Sub itself will be out of context but I posted it to give context to the lines I don't understand (bolded).

What does it mean (or in general) when the code is "expression1()=expression1() + expression2"? I'm having trouble wording it for Google or the forum hence why I had to make a thread about it.

Some help would be appreciated.

Please and thanks.
Rich (BB code):
Sub CutEmLoose(Liabs As Range)Dim NumRows As Long, RowNum As Long, ModAmt As Double
Dim LiabAmts() As Double, LGDAmts() As Double, LossRates() As Double
Dim i As Long, ws As Worksheet
    
    Set ws = ActiveWorkbook.Worksheets("Liability Inputs")
    NumRows = Application.Max(Liabs.Columns(1))


ReDim LiabAmts(1 To NumRows)
ReDim LGDAmts(1 To NumRows)
ReDim LossRates(1 To NumRows)
    
    For i = 3 To Liabs.Rows.Count + 2
        RowNum = Liabs.Cells(i - 2, 1).Value
        ModAmt = Liabs.Cells(i - 2, 8).Value
        If Liabs.Cells(i - 2, 11).Value <> "" Then
            LiabAmts(RowNum) = LiabAmts(RowNum) + ModAmt
            If ModAmt > 0 Then LGDAmts(RowNum) = LGDAmts(RowNum) + (Liabs.Cells(i - 2, 11) * ModAmt)
            If ModAmt > 0 Then LossRates(RowNum) = LossRates(RowNum) + (Liabs.Cells(i - 2, 12) * ModAmt)
        End If
    Next i
    For i = 3 To NumRows
        If LiabAmts(i) > 0 Then
            ws.Cells(i, 21).Value = LGDAmts(i) / LiabAmts(i)
            ws.Cells(i, 22).Value = LossRates(i) / LiabAmts(i)
        End If
    Next i
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Basically the code is looping through some data, in order to sum the data or amend the variables we can add or amend it to itself within the loop, think of a simple counter

DIM counter as integer :counter=0
For i = 1 to 3
counter=counter+1
next i

before the loop starts we add a variable counter and in each step of the loop counter retains the variable we set

first step i = 1

so i = 1 counter =counter + 1 ..... 0 = 0 + 1 counter now holds 1, next is counter = counter (now holding 1) + 1, variable in counter is now 2 and so on
 
Last edited:
Upvote 0
Basically the code is looping through some data, in order to sum the data or amend the variables we can add or amend it to itself within the loop, think of a simple counter

DIM counter as integer :counter=0
For i = 1 to 3
counter=counter+1
next i

before the loop starts we add a variable counter and in each step of the loop counter retains the variable we set

first step i = 1

so i = 1 counter =counter + 1 ..... 0 = 0 + 1 counter now holds 1, next is counter = counter (now holding 1) + 1, variable in counter is now 2 and so on

Thank you!!!
 
Upvote 0
Hello and welcome on the forum.

Learning VBA in your internship is huge bonus for you, trust me.

Concerning you problem "expression1()=expression1() + expression2".

Let's simplify it as x=x+1

You previously put a value for your "x", either in another part or in your loop.
Then you want to add 1 to your "x" value for later. However, you want to be able to reuse the same code line later in the same loop.
So, you save it again as "x".

When we think only in mathematics, it makes no sense. On the other side, you can do that in practically every programmation language.

I suggest you to try this simple sub in a new workbook to see it by yourself.
Code:
Sub understand()
Dim i as integer
Dim x as integer
For i = 1 to 10
x=x+1
cells(x,1)=i
Next
End sub

For this macro, I want you to do three things.
1. separate your screen in two (or on two screen), one with excel and one with VB editor (alt+f11)
2. activate your local value by clicking on view->locals windows
3. run your macro step by step with the F8 key

Hope it helps you
 
Last edited:
Upvote 0
Hello and welcome on the forum.

Learning VBA in your internship is huge bonus for you, trust me.

Concerning you problem "expression1()=expression1() + expression2".

Let's simplify it as x=x+1

You previously put a value for your "x", either in another part or in your loop.
Then you want to add 1 to your "x" value for later. However, you want to be able to reuse the same code line later in the same loop.
So, you save it again as "x".

When we think only in mathematics, it makes no sense. On the other side, you can do that in practically every programmation language.

I suggest you to try this simple sub in a new workbook to see it by yourself.
Code:
Sub understand()
Dim i as integer
Dim x as integer
For i = 1 to 10
x=x+1
cells(x,1)=i
Next
End sub

For this macro, I want you to do three things.
1. separate your screen in two (or on two screen), one with excel and one with VB editor (alt+f11)
2. activate your local value by clicking on view->locals windows
3. run your macro step by step with the F8 key

Hope it helps you

Thank you very much, Roxxien
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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