-
Excel Programming for Userforms
Review file attached.
"If it can be done, it can be done in Excel."
[VBA]
Dim var1 As Integer 'declare variable across UserForm1
'Be sure no other UserForms or worksheets are open by the same name from other workbooks.
Private Sub UserForm_Initialize()
'I presume you have ListBox1.ColumnCount set to 8
'and the columnswidths set: (20,18,24,,18,25,,10)
UserForm1.Caption = "UserForm Tips for Database Management"
ListBox1.RowSource = Sheet1.Name & "!A2:H" & Sheet1.Range("A1").CurrentRegion.Rows.Count
ComboBox1.RowSource = Sheet2.Name & "!A2:A" & Sheet2.Range("A1").CurrentRegion.Rows.Count
CommandButton1.Caption = "Modify & Return"
CommandButton2.Caption = "Add to Bottom"
CommandButton3.Caption = "Delete & Return"
CommandButton4.Caption = "Exit"
TextBox1 = Now()
TextBox2 = "wuzup"
End Sub
Private Sub ListBox1_Click()
'prevents unintended changes to ListBox1 during other code execution
If var1 = 1 Then Exit Sub
'0 is page1, 1 is page2
MultiPage1.Pages(1).Enabled = True
UserForm1.MultiPage1.Value = 1
MultiPage1.Pages(0).Enabled = False
ComboBox1 = Sheet1.Range("A2").Offset(ListBox1.ListIndex, 0).Value 'list
TextBox1 = Sheet1.Range("B2").Offset(ListBox1.ListIndex, 0).Value 'Date
TextBox2 = Sheet1.Range("C2").Offset(ListBox1.ListIndex, 0).Value 'text
End Sub
Private Sub TextBox1_Change()
Call EnableButton
End Sub
Private Sub TextBox2_Change()
Call EnableButton
End Sub
Private Sub ComboBox1_Change()
Call EnableButton
End Sub
Sub EnableButton()
If ComboBox1.ListIndex > -1 And IsDate(TextBox1) And TextBox2 <> "" Then 'IsNumber()...
CommandButton1.Enabled = True
CommandButton2.Enabled = True
CommandButton3.Enabled = True
Else
CommandButton1.Enabled = False
CommandButton2.Enabled = False
CommandButton3.Enabled = False
End If
End Sub
Private Sub CommandButton1_Click()
'Modify list and return to Page1
var1 = 1
Sheet1.Range("A2").Offset(ListBox1.ListIndex, 0).Value = ComboBox1
Sheet1.Range("B2").Offset(ListBox1.ListIndex, 0).Value = TextBox1
Sheet1.Range("C2").Offset(ListBox1.ListIndex, 0).Value = Val(TextBox2)
var1 = Empty
ListBox1.TopIndex = ListBox1.ListIndex 'returns to same place on ListBox1
ListBox1.ListIndex = -1 'turns off selection on ListBox1
MultiPage1.Pages(0).Enabled = True
UserForm1.MultiPage1.Value = 0
MultiPage1.Pages(1).Enabled = False
End Sub
Private Sub CommandButton2_Click()
'Add to Bottom
Sheet1.Range("A1:C1").Offset(Sheet1.Range("A1").CurrentRegion.Rows.Count, 0).Value = Array(ComboBox1, TextBox1, TextBox2)
ListBox1.RowSource = Sheet1.Name & "!A2:H" & Sheet1.Range("A1").CurrentRegion.Rows.Count
ListBox1.ListIndex = -1
ListBox1.TopIndex = ListBox1.ListCount 'scrolls to bottom of ListBox1
MultiPage1.Pages(0).Enabled = True
UserForm1.MultiPage1.Value = 0
MultiPage1.Pages(1).Enabled = False
End Sub
Private Sub CommandButton3_Click()
'Delete Row & Return
var2 = ListBox1.ListIndex
Sheet1.Rows(ListBox1.ListIndex + 2 & ":" & ListBox1.ListIndex + 2).Delete Shift:=xlUp
ListBox1.RowSource = Sheet1.Name & "!A2:H" & Sheet1.Range("A1").CurrentRegion.Rows.Count
ListBox1.TopIndex = var2
ListBox1.ListIndex = -1
MultiPage1.Pages(0).Enabled = True
UserForm1.MultiPage1.Value = 0
MultiPage1.Pages(1).Enabled = False
'Or delete the whole range (but not the header)
'Sheet1.Range("A1").CurrentRegion.Offset(1, 0).Clear 'then reset ListBox1.RowSource
End Sub
Private Sub CommandButton4_Click()
varOff = 1
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Can only close from 1st page
If UserForm1.MultiPage1.Value = 1 Then
'disable Close
If CloseMode = 0 Then Cancel = True 'CloseMode = 1 in Unload Me
Else
'require password to turn off UserForm
'Dim var1 As String
'var1 = InputBox("UserForm1 is protected.", "Password")
'If var1 <> "bark" Then Cancel = True
End If
End Sub
[/VBA]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules