Tuesday, August 4, 2015

Adding Controls Dynamically in User Form

Private Sub ComboBox1_Change()
Dim searchval As String, thelbl As Object, thelbl1 As Object, txtbox As Object
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error Resume Next
ThisWorkbook.Sheets(2).Range("B2") = ComboBox1.Value
For Each cont In Me.Controls
    Me.Controls.Remove cont.Name
Next
Application.Calculation = xlCalculationAutomatic
If (Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets(2).Range("E:E"), ">0") + 1) > 2 Then
    Set myrng1 = ThisWorkbook.Sheets(2).Range("E2:E" & Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets(2).Range("E:E"), ">0") + 1)
Else
    Set myrng1 = ThisWorkbook.Sheets(2).Range("E2:E3")
End If
ComboBox1.List = myrng1.Value
cellrow = CLng(Application.WorksheetFunction.Match(CInt(ComboBox1.Value), ThisWorkbook.Sheets(1).Range("F:F"), 0))
Set lookupadd = ThisWorkbook.Sheets(1).Range("F3:F" & ThisWorkbook.Sheets(1).Cells(Rows.Count, 6).End(xlUp).Row).Find(Application.WorksheetFunction.Trim(ComboBox1.Value), LookIn:=xlValues)

    Set lblName = Existingemp.Controls.Add("Forms.Label.1", "Name", True)
    Set lblNameval = Existingemp.Controls.Add("Forms.Label.1", "Name", True)
    Set lblStatus = Existingemp.Controls.Add("Forms.Label.1", "Status", True)
    Set lblResignationDate = Existingemp.Controls.Add("Forms.Label.1", "ResignDate", True)
    Set txtBoxResignationDate = Existingemp.Controls.Add("Forms.TextBox.1", "ResignDate", True)
    Set lblNoticeDuration = Existingemp.Controls.Add("Forms.Label.1", "NoticeDuration", True)
    Set txtBoxNoticeDuration = Existingemp.Controls.Add("Forms.TextBox.1", "NoticeDuration", True)
    Set lblRelvDate = Existingemp.Controls.Add("Forms.Label.1", "RelvDate", True)
    Set txtBoxRelvDate = Existingemp.Controls.Add("Forms.TextBox.1", "RelvDate", True)
    Set lblOffLast = Existingemp.Controls.Add("Forms.Label.1", "OffLast", True)
    Set txtBoxOffLast = Existingemp.Controls.Add("Forms.TextBox.1", "OffLast", True)
    Set lblOffEmailIdStatus = Existingemp.Controls.Add("Forms.Label.1", "OffEmailIdStatus", True)
    Set txtBoxOffEmailIdStatus = Existingemp.Controls.Add("Forms.TextBox.1", "OffEmailIdStatus", True)
    Set lblHandoverAssetData = Existingemp.Controls.Add("Forms.Label.1", "HandoverAssetData", True)
    Set cmbboxHandoverAssetData = Existingemp.Controls.Add("Forms.ComboBox.1", "HandoverAssetData", True)
    Set lblExitInterview = Existingemp.Controls.Add("Forms.Label.1", "ExitInterview", True)
    Set cmbExitInterview = Existingemp.Controls.Add("Forms.ComboBox.1", "ExitInterview", True)
    Set lblReleivingLetterStatus = Existingemp.Controls.Add("Forms.Label.1", "ReleivingLetterStatus", True)
    Set cmbReleivingLetterStatus = Existingemp.Controls.Add("Forms.ComboBox.1", "ReleivingLetterStatus", True)
    Set lblFandF = Existingemp.Controls.Add("Forms.Label.1", "FandF", True)
    Set cmbFandF = Existingemp.Controls.Add("Forms.ComboBox.1", "FandF", True)
    Set lblRemark = Existingemp.Controls.Add("Forms.Label.1", "Remark", True)
    Set txtRemark = Existingemp.Controls.Add("Forms.TextBox.1", "Remark", True)
    Set cmbbox = Existingemp.Controls.Add("Forms.ComboBox.1", "cmbbox", True)
   
   
        With lblName
            .BackColor = RGB(141, 180, 226)
            .Caption = "Name"
            .Font.Bold = True
            .Left = 124
            .Width = 36
            .Top = 7
            .BorderStyle = 1
            .Height = 16
        End With
        With lblNameval
           
            .BackColor = RGB(141, 180, 226)
            .Font.Bold = True
            .Caption = ThisWorkbook.Sheets(1).Range(lookupadd.Address).Offset(0, -3) & " " & ThisWorkbook.Sheets(1).Range(lookupadd.Address).Offset(0, -2) & " " & ThisWorkbook.Sheets(1).Range(lookupadd.Address).Offset(0, -1)
            .Left = 160
            .Width = 127
            .Top = 7
            .BorderStyle = 1
            .Height = 16
        End With
        With lblResignationDate
            .BackColor = RGB(255, 153, 102)
            .Caption = "Resignation Date"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 55
            .BorderStyle = 1
            .Height = 15
        End With
        With txtBoxResignationDate
           
            .Value = Format(Date, "mm/dd/yyyy")
            .Font.Bold = True
            .Left = 85
            .Width = 75
            .Top = 55
            .BorderStyle = 1
            .Height = 15
        End With
        With lblNoticeDuration
            .BackColor = RGB(255, 153, 102)
            .Caption = "Notice Durartion"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 70.5
            .BorderStyle = 1
            .Height = 20
        End With
        With txtBoxNoticeDuration
           
           
            .Font.Bold = True
            .Left = 85
            .Width = 75
            .Top = 70.5
            .BorderStyle = 1
            .Height = 20
        End With
        With lblRelvDate
            .BackColor = RGB(255, 153, 102)
            .Caption = "Releieving Date"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 91.5
            .BorderStyle = 1
            .Height = 15
        End With
        With txtBoxRelvDate
           
            .Value = Format(Date + 60, "mm/dd/yyyy")
            .Font.Bold = True
            .Left = 85
            .Width = 75
            .Top = 91.5
            .BorderStyle = 1
            .Height = 15
        End With
        With lblOffLast
            .BackColor = RGB(255, 153, 102)
            .Caption = "Official Last Date"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 107
            .BorderStyle = 1
            .Height = 15
        End With
        With txtBoxOffLast
           
            .Value = Format(Date + 60, "mm/dd/yyyy")
            .Font.Bold = True
            .Left = 85
            .Width = 75
            .Top = 107
            .BorderStyle = 1
            .Height = 15
        End With
       
        With lblOffEmailIdStatus
            .BackColor = RGB(255, 153, 102)
            .Caption = "Off. Emailid Status"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 55
            .BorderStyle = 1
            .Height = 15
        End With
        With txtBoxOffEmailIdStatus
           
           
            .Font.Bold = True
            .Left = 256
            .Width = 220
            .Top = 55
            .BorderStyle = 1
            .Height = 15
        End With
        With lblHandoverAssetData
            .BackColor = RGB(255, 153, 102)
            .Caption = "Handover Asset & Data"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 70.5
            .BorderStyle = 1
            .Height = 20
        End With
        With cmbboxHandoverAssetData
           
            .List = Array("Yes", "No", "Not Required")
            .Font.Bold = True
            .Left = 256
            .Width = 40
            .Top = 70.5
            .BorderStyle = 1
            .Height = 20
        End With
        With lblExitInterview
            .BackColor = RGB(255, 153, 102)
            .Caption = "Exit Interview"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 91.5
            .BorderStyle = 1
            .Height = 15
        End With
        With cmbExitInterview
           
            .List = Array("Yes", "No", "Not Required")
            .Font.Bold = True
            .Left = 256
            .Width = 40
            .Top = 91.5
            .BorderStyle = 1
            .Height = 15
        End With
        With lblReleivingLetterStatus
            .BackColor = RGB(255, 153, 102)
            .Caption = "Releiving Letter"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 107
            .BorderStyle = 1
            .Height = 15
        End With
        With cmbReleivingLetterStatus
           
            .List = Array("Issued", "Pending", "Not Required")
            .Font.Bold = True
            .Left = 256
            .Width = 40
            .Top = 107
            .BorderStyle = 1
            .Height = 15
        End With
        With lblFandF
            .BackColor = RGB(255, 153, 102)
            .Caption = "F&F Status"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 122.5
            .BorderStyle = 1
            .Height = 15
        End With
        With cmbFandF
           
            .List = Array("Yes", "No", "On Hold", "Not Required")
            .Font.Bold = True
            .Left = 85
            .Width = 40
            .Top = 122.5
            .BorderStyle = 1
            .Height = 15
        End With
        With lblRemark
            .BackColor = RGB(255, 153, 102)
            .Caption = "Remark"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 122.5
            .BorderStyle = 1
            .Height = 15
        End With
        With txtRemark
           
           
            .Font.Bold = True
            .Left = 256
            .Width = 220
            .Top = 122.5
            .BorderStyle = 1
            .Height = 15
        End With
        With lblStatus
            .Caption = "Status"
            .Font.Bold = True
            .BackColor = RGB(255, 153, 102)
            .Left = 350
            .Width = 75
            .Top = 140
            .BorderStyle = 1
            .Height = 14.5
        End With
       
       
        With cmbbox
            .List = Array("Resigned")
            .Left = 425.5
            .Width = 51
            .Top = 140
            .BorderStyle = 1
            .Height = 14.5
           
        End With
Existingemp.Height = 180
Existingemp.Width = 481

Application.EnableEvents = True
End Sub