Epsilon101
Programmer
Hi,
Spot of bother with this, can anyone help? its in Excel.
I have a userform called FrmMaintenance.
It has 4 option buttons:
OptTitle
OptPassed
OptAbility
OptReason
A textbox:
TxtAdd
A Listbox:
Listbox1
3 buttons:
CmdAdd
CmdDelete
CmdOk
The idea of it is, the option buttons update the listbox with data from a different worksheet each time they are clicked. While an option is clicked, you type something into 'TxtAdd' and click the 'CmdAdd' button to add it to the bottom of the range and then the listbox updates again. Also got a 'CmdDelete' button to delete any selected data in the listbox. They are all password protected, and when writing or deleting from them i am removing the password and then adding it again.
Sheets of data:
'PassedTo'
'Reasons'
'Ability'
'Titles'
This section is FrmMaintenance code:
This section is my module:
I also tried adding the below to the command buttons 'add' and 'delete' for updating the sort while the password is off, but it came back with errors.
Thanks for any help
---------------------------------------
Neil
Spot of bother with this, can anyone help? its in Excel.
I have a userform called FrmMaintenance.
It has 4 option buttons:
OptTitle
OptPassed
OptAbility
OptReason
A textbox:
TxtAdd
A Listbox:
Listbox1
3 buttons:
CmdAdd
CmdDelete
CmdOk
The idea of it is, the option buttons update the listbox with data from a different worksheet each time they are clicked. While an option is clicked, you type something into 'TxtAdd' and click the 'CmdAdd' button to add it to the bottom of the range and then the listbox updates again. Also got a 'CmdDelete' button to delete any selected data in the listbox. They are all password protected, and when writing or deleting from them i am removing the password and then adding it again.
Sheets of data:
'PassedTo'
'Reasons'
'Ability'
'Titles'
This section is FrmMaintenance code:
Code:
Private Sub UserForm_initialize()
OptTitle = True
TxtAdd.Value = ""
TxtAdd.SetFocus
End Sub
Private Sub UserForm_Terminate()
Unload Me
End Sub
Private Sub cmdAdd_Click()
With ws
.Unprotect ("password")
.Range("A" & LRow + 1) = Me.TxtAdd.Text
.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
LRow = GetLastRow(.Name)
ListBox1.Clear
ListBox1.List = .Range("A1:A" & LRow).Value
End With
End Sub
Private Sub cmdDelete_Click()
Dim c As Range
With ws
Set c = .Range("A1:A" & LRow).Cells.Find(Me.ListBox1.Value, , , xlWhole, xlByRows)
If Not c Is Nothing Then
.Unprotect ("password")
c.Delete xlUp
.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
LRow = GetLastRow(.Name)
ListBox1.Clear
ListBox1.List = .Range("A1:A" & LRow).Value
Else
MsgBox "No Data selected!", vbCritical
End If
End With
End Sub
Private Sub OKButton_Click()
Unload Me
End Sub
Private Sub optReason_Click()
Call UpdateListBox("Reasons")
End Sub
Private Sub optAbility_Click()
Call UpdateListBox("Ability")
End Sub
Private Sub optPassed_Click()
Call UpdateListBox("PassedTo")
End Sub
Private Sub optTitle_Click()
Call UpdateListBox("Titles")
End Sub
This section is my module:
Code:
Option Explicit
'Declarations
Public LRow As Long
Public ws As Worksheet
Public gPassword As String
Function GetLastRow(sh As String) As Long
GetLastRow = ThisWorkbook.Worksheets(sh).Range("A65536").End(xlUp).Row
End Function
Sub UpdateListBox(sh As String)
'also updates variables for sheet & row
Set ws = ThisWorkbook.Worksheets(sh)
With ws
LRow = GetLastRow(.Name)
FrmMaintenance.ListBox1.Clear
FrmMaintenance.ListBox1.List = .Range("A1:A" & LRow).Value
End With
End Sub
I also tried adding the below to the command buttons 'add' and 'delete' for updating the sort while the password is off, but it came back with errors.
Code:
Private Sub cmdAdd_Click()
With ws
.Unprotect ("password")
.Range("A" & LRow + 1) = Me.TxtAdd.Text
[COLOR=red].Range("A1:A" & LRow).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal[/color]
.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
LRow = GetLastRow(.Name)
ListBox1.Clear
ListBox1.List = .Range("A1:A" & LRow).Value
End With
End Sub
Thanks for any help
---------------------------------------
Neil