Use of variables


Sub VBAClassVariableEX()

'VBA macro Code to understand the function of Variables

Workbooks.Add
ActiveSheet.Name = "VBAClassVariableEx"
Dim i As Integer
Cells(1, 1).Value = "Sr.No"
For i = 2 To 12
Cells(i, 1).Value = i - 1
Next

Cells(1, 2).Value = "Particular"
Cells(2, 2).Value = "First Name"
Cells(3, 2).Value = "Last Name"
Cells(4, 2).Value = "Date of Birth"
Cells(5, 2).Value = "Birth Day, Month"
Cells(6, 2).Value = "Birth Year"
Cells(7, 2).Value = "Years"
Cells(8, 2).Value = "Months"
Cells(9, 2).Value = "Days"
Cells(10, 2).Value = "Hours"
Cells(11, 2).Value = "Minute"
Cells(12, 2).Value = "Seconds"
Cells(1, 3).Value = "Details"

Dim username As String
Dim firstname As String
Dim lastname As String
Dim yearpassed As Single
Dim monthpassed As Single
Dim daypassed As Single
Dim hourpassed As Single
Dim minpassed As Long
Dim secondspassed As Single
Dim userbirth As Date
Dim currentdate As Date
Dim bday As String
Dim bmonth As String
Dim bday2 As Integer
Dim byear As Integer
Const secspermin = 60
Const minsperhour = 60
Const hoursperday = 24
Const daysperyear = 365.25

username = LCase(InputBox("First Name and Last Name?", "name"))
userbirth = DateValue(InputBox("Date of Birth (dd/mm/yyyy)", "Age"))

currentdate = Now

secondspassed = DateDiff("s", userbirth, currentdate)
minpassed = secondspassed / secspermin
hourpassed = minpassed / minsperhour
daypassed = hourpassed / hoursperday
yearpassed = daypassed / daysperyear
monthpassed = yearpassed * 12

firstname = Left(username, InStr(1, username, " "))
firstname = UCase(Left(firstname, 1)) & Right(firstname, Len(firstname) - 1)
Cells(2, 3).Value = firstname

lastname = Right(username, (Len(username) - InStr(1, username, " ")))
lastname = StrConv(lastname, vbProperCase)
Cells(3, 3).Value = lastname

bday = Format(userbirth, "dddd")
bmonth = Format(userbirth, "mmmm")
bday2 = Day(userbirth)
byear = Year(userbirth)

Cells(4, 3).Value = bday
Cells(5, 3).Value = bmonth & " " & Str(bday2)
Cells(6, 3).Value = byear
Cells(7, 3).Value = yearpassed
Cells(8, 3).Value = monthpassed
Cells(9, 3).Value = daypassed
Cells(10, 3).Value = hourpassed
Cells(11, 3).Value = minpassed
Cells(12, 3).Value = secondspassed


End Sub


Comments

Popular posts from this blog

TDS Calculation System

VBA - Additional Controls Dialog Box "MISSING"

Loan EMI Calculator Dashboard