Analyse the VB code

minhaj18

New Member
Joined
Dec 19, 2013
Messages
8
Hi,

I have got the below code from one of my friends. I'm fairly new to macro building and have to build a similar macro for another work. Can someone please analyse the code and let me know what it does. Thanks in advance. Appreciate your help.

Code:
Sub Enrollment()


sProduct = InputBox("Enter the Product Name", "Product Name")


iTargetRowCount = 3
sTargetValue = Worksheets("Enrollmentfile").Range("B" & iTargetRowCount)
flag = False
Do While sTargetValue <> ""
    sTargetValue = Worksheets("Enrollmentfile").Range("B" & iTargetRowCount)
    iTargetRowCount = iTargetRowCount + 1
    flag = True
Loop
If flag Then
    iTargetRowCount = iTargetRowCount - 1
End If


Do While sProduct <> ""
    sDate = InputBox("Enter the Effective Date in YYYYMMDD format")
    sYear = Left(sDate, 4)
    sMonth = Mid(sDate, 5, 2)
    sDay = Right(sDate, 2)
    
    sMonthName = MonthName(sMonth)
    sDateTemp = DateValue(Left(sMonthName, 3) & " " & sDay & ", " & sYear)
    dateResult = DateAdd("m", -1, sDateTemp)
    
    iRowCount = 2
    sCellValue = Worksheets("Base sheet_Do not delete").Range("B" & iRowCount)
    
    Do While sCellValue <> ""
        If UCase(sCellValue) = UCase(sProduct) Then
            Worksheets("Base sheet_Do not delete").Range("A" & iRowCount & ":AQ" & iRowCount).Copy Destination:=Worksheets("Enrollmentfile").Range("A" & iTargetRowCount)
            Exit Do
        End If
        iRowCount = iRowCount + 1
        sCellValue = Worksheets("Base sheet_Do not delete").Range("B" & iRowCount)
    Loop
    'Changes
    'Member ID
    'arrMember = Split(Worksheets("Enrollmentfile").Range("C" & iTargetRowCount), "*")
    'arrMember = (CDbl(arrMember(0)) + iTargetRowCount) & " * " & arrMember(1)
    firstName = Worksheets("Enrollmentfile").Range("E" & iTargetRowCount) & iTargetRowCount
    lastName = Worksheets("Enrollmentfile").Range("F" & iTargetRowCount) & iTargetRowCount
 
    
    Worksheets("Enrollmentfile").Range("C" & iTargetRowCount) = generateSSN() & "*01"
    Worksheets("Enrollmentfile").Range("E" & iTargetRowCount) = firstName
    Worksheets("Enrollmentfile").Range("F" & iTargetRowCount) = lastName
    Worksheets("Enrollmentfile").Range("N" & iTargetRowCount) = Year(dateResult) & Right("0" & Month(dateResult), 2) & Right("0" & Day(dateResult), 2)
    Worksheets("Enrollmentfile").Range("O" & iTargetRowCount) = Year(dateResult) & Right("0" & Month(dateResult), 2) & Right("0" & Day(dateResult), 2)
    Worksheets("Enrollmentfile").Range("P" & iTargetRowCount) = sDate
    Worksheets("Enrollmentfile").Range("S" & iTargetRowCount) = generateRandom()
    Worksheets("Enrollmentfile").Range("T" & iTargetRowCount) = generateRandom()
    Worksheets("Enrollmentfile").Range("AB" & iTargetRowCount) = generateSSN()
    Worksheets("Enrollmentfile").Range("AD" & iTargetRowCount) = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2)
    Worksheets("Enrollmentfile").Range("AE" & iTargetRowCount) = generateContractNumber()
    Worksheets("Enrollmentfile").Range("AQ" & iTargetRowCount) = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2)
    'Worksheets("Enrollmentfile").Range("AN" & iTargetRowCount) = "0"
    
    sProduct = InputBox("Enter the Product Name")
    iTargetRowCount = iTargetRowCount + 1
Loop


End Sub


Function generateRandom()
    Randomize
    tempNum = Round(Rnd * 9999999999#)
    Do While Len(tempNum) <> 10
        Randomize
        tempNum = Round(Rnd * 9999999999#)
    Loop
    generateRandom = tempNum
    
End Function


Function generateSSN()
    Randomize
    tempNum = Round(Rnd * 999999999#)
    Do While Len(tempNum) <> 9
        Randomize
        tempNum = Round(Rnd * 999999999#)
    Loop
    generateSSN = tempNum
    
End Function


Function generateContractNumber()
    Randomize
    tempNum = Round(Rnd * 9999999#)
    Do While Len(tempNum) <> 7
        Randomize
        tempNum = Round(Rnd * 9999999#)
    Loop
    generateContractNumber = tempNum
    
End Function
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,216,112
Messages
6,128,901
Members
449,477
Latest member
panjongshing

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