Dynamic UserForm v1.0
Dynamic UserForm v1.0
4
VBA SOURCE CODE BOOK
Download Your Free Watch The Training Video Got Questions? Join Our 48k
Workbook Here Here Community
COLOFON
Created: January 4, 2022 by Rob Haman (EXCEL SUPPORT)
DYNAMIC RACI MATRIX FILL & SING DOCUMENT DYNAMIC SKILLS MATRIX DOCUMENT WORKFLOW PDF TO EXCEL
(21-222) CREATOR (21-221) (21-220) MANAGER (21-219) (21-218)
1 Option Explicit
2
3 Private Sub Worksheet_Deactivate()
4 Shapes("EditBtn" ).Visible = msoFalse 'Hide Edit Icon on Sheet deactivate
5 End Sub
6
7 Private Sub Worksheet_SelectionChange(ByVal Target As Range)
8 If Not Intersect(Target, Range("A3:Z999" )) Is Nothing And Range("A" & Target.Row).Value
<> Empty Then
9 Setup.Range("E5" ).Value = Target.Row
10 ShowEditIcon
11 End If
12 End Sub
1 Option Explicit
1 Option Explicit
2
3 Private Sub Worksheet_Deactivate()
4 Shapes("EditBtn" ).Visible = msoFalse 'Hide Edit Icon on Sheet deactivate
5 End Sub
6
7 Private Sub Worksheet_SelectionChange(ByVal Target As Range)
8 If Not Intersect(Target, Range("A3:Z999" )) Is Nothing And Range("A" & Target.Row).Value
<> Empty Then
9 Setup.Range("E5" ).Value = Target.Row
10 ShowEditIcon
11 End If
12
13 End Sub
1 Option Explicit
2
3 Private Sub Worksheet_Change(ByVal Target As Range)
4 If Not Intersect(Target, Range("G5:H99" )) Is Nothing And Range("G" & Target.Row).Value
<> Empty And Range("H" & Target.Row).Value <> Empty Then
5 If IsNumeric(Range("H" & Target.Row).Value) = False Then Exit Sub 'Header Row must
be numeric value
6 Dim ShtNm As String
7 Dim HeadRow As Long, HeadCol As Long, ShtCol As Long, FirstCol As Long, LastCol As
Long, SetupCol As Long, SetupRow As Long
8 HeadRow = Range("H" & Target.Row) 'Header Row
9 ShtNm = Range("G" & Target.Row).Value 'Worksheet Name
10 SetupCol = Range("U4:AB4" ).Find(ShtNm, , xlValues, xlWhole).Column
11 SetupRow = 5
12 Range(Cells(5, SetupCol), Cells(34, SetupCol)).ClearContents 'Clear Previous headers
1 Option Explicit
1 Option Explicit
2 'Private Declare Function GetDC& Lib "user32.dll" (ByVal hwnd&)
3 'Private Declare Function GetDeviceCaps& Lib "gdi32" (ByVal hDC&, ByVal nIndex&)
4
5 #If VBA7 Then
6 Private Declare PtrSafe Function GetDC& Lib "user32.dll" (ByVal hwnd&)
7 Private Declare PtrSafe Function GetDeviceCaps& Lib "gdi32" (ByVal hDC&, ByVal nIndex&)
8 #Else
9 Private Declare Function GetDC& Lib "user32.dll" (ByVal hwnd&)
10 Private Declare Function GetDeviceCaps& Lib "gdi32" (ByVal hDC&, ByVal nIndex&)
11 #End If
12
13 Sub QuickViewForm_Show()
14 Dim ShtNm As String, BackPic As String, PicField As String, PicName As String, PicFolder
As String, PicPath As String, LinkedSht As String, LinkedID As String, ColWidths As
String
15 Dim XPos As Double, YPos As Double 'Used for Form positioning
16 Dim TxtField As MSForms.TextBox
17 Dim LblField As MSForms.Label
18 Dim ImgField As MSForms.Image
19 Dim ListBox As MSForms.ListBox
20 Dim Cntrl As Control
21
22 Dim TopPos As Long, LeftPos As Long, BotPos As Long, RowLimit As Long, ColCount As Long,
RowCount As Long, FontSize As Long
23 Dim ActRow As Long, ActCol As Long, DetailRow As Long, HeadRow As Long, DataCol As Long,
StartCol As Long, EndCol As Long, LinkIDDataCol As Long
24 Dim LastRow As Long, LastCol As Long, LastResultRow As Long, LastResultCol As Long,
LastDataCol As Long
25 Dim ResultRng As Range
26
27 With ActiveSheet
28 ShtNm = ActiveSheet.Name 'Active Sheet Name
29 If ActiveSheet.Name = "Setup" Then
30 MsgBox "Please run this on sheets other than the Setup sheet"
31 Exit Sub
32 End If
33
34 ActRow = ActiveCell.Row 'Active Row
35 ActCol = ActiveCell.Column 'Active Column
36
37 On Error Resume Next
38 DetailRow = Setup.Range("G5:G34" ).Find(ShtNm, , xlValues, xlWhole).Row
39 On Error GoTo 0
40 If DetailRow = 0 Then
41 MsgBox "Please make sure to setup this sheet in the Dynamic Userform Table in
the Setup Sheet"
42 Exit Sub
43 End If
44 RowLimit = Setup.Range("E4" ).Value 'Row Limit
45 'Header Row, Start Column, Ending Col area all required
46 If Setup.Range("H" & DetailRow).Value = "" Or Setup.Range("I" & DetailRow).Value =
"" Or Setup.Range("J" & DetailRow).Value = "" Then
47 MsgBox "Pelase make sure the Dynamic Userform detail row contains a header row
along with starting and ending columns"
48 Exit Sub
49 End If
50 HeadRow = Setup.Range("H" & DetailRow).Value 'Header Row
51 StartCol = Setup.Range("I" & DetailRow).Value 'Start Col
52 EndCol = Setup.Range("J" & DetailRow).Value 'End Column
53 BackPic = Setup.Range("FormBackPic" ).Value 'Form Background Picture
54 FontSize = Setup.Range("E6" ).Value 'Set Font Size
55 If Dir(BackPic, vbDirectory) = "" Then BackPic = "" 'Clear String if incorrect
file path
56 'Picture Path
57 If Setup.Range("K" & DetailRow).Value <> "" Then
58 PicField = Setup.Range("K" & DetailRow).Value 'Picture Field Name
59 End If
60 If Setup.Range("L" & DetailRow).Value <> "" Then
61 PicFolder = Setup.Range("L" & DetailRow).Value & "/" 'Picture Folder
62 End If
63
64 'Set Initial Left and top Positions
65 LeftPos = 10 'Set Initial Left Pos
66 TopPos = 30 'Set Initial Top Pos
67
68 'Clear All Labels and Text Boxes & List Poxes & Pictures
69 With DynamicForm
70 For Each Cntrl In .Controls
71 On Error Resume Next
72 If InStr(Cntrl.Name, "Txt" ) > 0 Then Cntrl.Remove 'Delete all Text Boxes
73 If InStr(Cntrl.Name, "Lbl" ) > 0 Then Cntrl.Remove 'Delete all Labels
74 If InStr(Cntrl.Name, "Pic" ) > 0 Then Cntrl.Remove 'Delete all Pictures
75 If InStr(Cntrl.Name, "ListBox" ) > 0 Then Cntrl.Remove 'Delete all Combo
Boxes
76 On Error GoTo 0
77 Next Cntrl
78 End With
79
80
81 With DynamicForm
82
83 'Set Fileds
84 For DataCol = StartCol To EndCol
85 If PicField = ActiveSheet.Cells(HeadRow, DataCol).Value Then 'Check For
Picture data type
86 PicName = ActiveSheet.Cells(ActRow, DataCol).Value 'Picture Name
87 If PicName <> Empty Then PicPath = PicFolder & PicName 'Picture Path
88 If Dir(PicPath, vbDirectory) = "" Then GoTo NextField
89 Set LblField = .Controls.Add("Forms.Label.1" , "LblBox" & DataCol)
90 With LblField
91 .Top = TopPos
92 .Width = 80
93 .Font.Size = FontSize 'Dynamic Font Size
94 .Left = LeftPos
95 .BackStyle = fmBackStyleTransparent
96 .Caption = ActiveSheet.Cells(HeadRow, DataCol).Value 'Set Header
Label
97 End With
98
99 Set ImgField = .Controls.Add("Forms.Image.1" , "PicField" & DataCol)
100 With ImgField
101 .Picture = LoadPicture(PicPath): .PictureAlignment =
fmPictureAlignmentCenter: .PictureSizeMode = fmPictureSizeModeStretch
249
250
251 Sub ShowEditIcon()
252 With ActiveSheet.Shapes("EditBtn" )
253 .Left = 1
254 .Top = ActiveCell.Top
255 .Visible = msoCTrue
256 End With
257 End Sub
1
2 Option Explicit
3
4 Sub SaveData()
5 Dim ActRow As Long, DataCol As Long, DetailRow As Long, StartCol As Long, EndCol As Long
6 Dim TxtField As MSForms.TextBox
7 Dim Cntrl As Control
8 Dim ShtNm As String
9
10 If Setup.Range("E5" ).Value = Empty Then Exit Sub
11 ShtNm = ActiveSheet.Name 'Active Sheet Name
12 ActRow = ActiveCell.Row 'Active Row
13 On Error Resume Next
14 DetailRow = Setup.Range("G5:G34" ).Find(ShtNm, , xlValues, xlWhole).Row
15 On Error GoTo 0
16 If DetailRow = 0 Then
17 MsgBox "Please make sure to setup this sheet in the Dynamic Userform Table in the
Setup Sheet"
18 Exit Sub
19 End If
20
21 StartCol = Setup.Range("I" & DetailRow).Value 'Start Col
22 EndCol = Setup.Range("J" & DetailRow).Value 'End Column
23
24 If StartCol = 0 Or EndCol = 0 Then
25 MsgBox "Please make sure to add Start and Ending columns for this table within the
Setup Sheet"
26 Exit Sub
27 End If
28
29 For DataCol = StartCol To EndCol
30 On Error GoTo NextCol 'Ignore errors for non-existging text Fields (such as
pictures)
31 Set TxtField = DynamicForm.Controls("TxtBox" & DataCol)
32 If Sheets(ShtNm).Cells(ActRow, DataCol).Value <> TxtField.Value Then Sheets(ShtNm).
Cells(ActRow, DataCol).Value = TxtField.Value 'Make update on any change
33 NextCol:
34 Next DataCol
35 End Sub
36
1 Option Explicit
2 Dim WkSht As Worksheet
3 Dim ShtRow As Long
4 Dim BackPic As FileDialog
5 Sub ListAllSheets()
6 Setup.Range("B4:B999" ).ClearContents
7 ShtRow = 4
8 For Each WkSht In ThisWorkbook.Worksheets
9 Setup.Range("B" & ShtRow).Value = WkSht.Name
10 ShtRow = ShtRow + 1
11 Next WkSht
12 End Sub
13
14 Sub Setup_AddFormBackroundPic()
15 Set BackPic = Application.FileDialog(msoFileDialogFilePicker)
16
17 With BackPic
18 .Title = "Please select a the member default picture"
19 .Filters.Add "Picture Files" , "*.jpg,*.gif" , 1
20 .AllowMultiSelect = False
21 If .Show <> -1 Then GoTo NoSelection
22 Setup.Range("E3" ).Value = .SelectedItems(1) 'Set Folder Path
23 NoSelection:
24 End With
25 End Sub
26
1
2 Option Explicit
3
4
5 Private Sub CancelBtn_Click()
6 Me.Hide
7 Unload Me
8 End Sub
9
10 Private Sub SaveBtn_Click()
11 SaveData
12 Me.Hide
13 Unload Me
14 End Sub
15
16
17