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.
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: