Declaring variable

confusion123

Active Member
Joined
Jul 27, 2014
Messages
400
In the code below, is there any reason why these are declared twice, once in the Sub and once in the Function?

Rich (BB code):
Dim objLoan As Loan

Dim objLoans As Collection

Could they not been declared once only at the top of the module?

Rich (BB code):
Sub TestCollectionObject()

Dim rg As Range

Dim objLoans As Collection

Dim objLoan As Loan

Set rg = ThisWorkbook.Worksheets("Loans"). _

Range("LoanListStart").Offset(1, 0)

' get the collection of loan objects

Set objLoans = CollectLoanObjects(rg)

Debug.Print "There are " & objLoans.Count & _

" loans."

' iterate through each loan

For Each objLoan In objLoans

Debug.Print "Loan Number " & objLoan.LoanNumber & _

" has a payment of " & Format(objLoan.Payment, "Currency")

Next

Set objLoans = Nothing

Set objLoan = Nothing

Set rg = Nothing

End Sub

Function CollectLoanObjects(rg As Range) As Collection

Dim objLoan As Loan

Dim objLoans As Collection

Set objLoans = New Collection

' loop until we find an empty row

Do Until IsEmpty(rg)

Set objLoan = New Loan

With objLoan

.LoanNumber = rg.Value

.Term = rg.Offset(0, 1).Value

.InterestRate = rg.Offset(0, 2).Value

.PrincipalAmount = rg.Offset(0, 3).Value

End With

' add the current loan to the collection

objLoans.Add objLoan, CStr(objLoan.LoanNumber)

' move to next row


Set rg = rg.Offset(1, 0)

Loop

Set objLoan = Nothing

Set CollectLoanObjects = objLoans

Set objLoans = Nothing

End Function
 
Last edited:
Re: Declaring varable

I see people who always use a variable of "i" the first time they do a loop in any procedure (if they have a second loop, they will use j, etc). Nothing wrong with that.

I, to give another example, absolutely cannot shake the habit of using x and y (and thinking of them as axis) when looping through rows and columns in Excel.

Almost every example you'll find online of a Javascript for loop will also use i (including W3 examples). Eg

Code:
For(var i=0;i<50;i++)
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,216,558
Messages
6,131,400
Members
449,648
Latest member
kyouryo

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