Class with button events

Jo Helge Rorvik

New Member
Joined
Mar 23, 2023
Messages
11
Office Version
  1. 365
Hi!

I have a workbook with 3 sheet, containing 1 table on each sheet.
At the top of every sheet I have buttons Add, Change and Delete
These buttons call 3 UserForms and set them up using Tag to be one of the 3 modus.

In each UserForm I have buttons with the same name made visible in correct modus
one button btnAdd, btnChange and btnDelete.

Can I create a class that listen to button events and trigger the same event from each of the UserForms
to do the same actions on all 3 tables in every sheets?

I'm not very into event handling at this level.

In advance thanks for any replys.

Jo-Helge


For clarity!
There is one UserForm for each sheet and on every sheet they are calling the same UserForm
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hummm...
And WHY do you use three userforms and not only one?
 
Upvote 0
Hi!

Instead of the User are able to edit the sheet or table, I make the sheet Protected and have
buttons to open UserForms and manipulate/Add/Change information
Prevent sheet from having cells with invisible spaces and thing that not are related to the wanted information

Its a small Accounting system for a voluntary organization.
One of the sheets are Incoming AccountDoc's and one for Outgoing AccountDoc's.
These table actually have the same format but different account Information
A third table have member information

The 2 first have format as:
Incoming columns: AccountDoc | Date | Description || InAccount1 | InAccount2 | InAccount3 | InAccount4 |
Outgoing columns: AccountDoc | Date | Description || OutAccount1 | OutAccount2 | OutAccount3 | OutAccount4 |
-AccountDoc is a common raising Long number separating all docs
-Date is the date received
-Description is only a text describing the AccountDoc
-Rest of the fields are configurable
Tree Buttons make it possible to Add, Change and Delete Accounts
These fields are where the real amount is put under, and is of Type Accounting
The total row summarizes all Items in that Account/Column

In member table there are different columns with different formatting
My Idea was to create and send an array with just as many Items as the columns in table
If Not the array position is Empty then put it in the column this position represents in the table

To fill up a ComboBox in the UserForm AccountDoc i do:

With .ListObjects(1).HeaderRowRange
cboAccount.List = Application.Transpose( _
.Range(.Row, .Column +3), Cells(.Row, .Columns.Count)).value)
cboAccount.ListIndex = 0
End With
(rewritten might have bugs :) )

Dependent on the Account selected, the index put the content in the right position in the array
New AccountDoc and New Member add new rows in table while New Account add columns in table

I thought I had made this to a general way to fill up the table without concerning about the content
and it could be coded generally by having 3 button events Add, Change and Delete
Dependent on the active sheet and the table in that sheet and the UserForm opened for that sheet
the size of the array was more or less pre-configurable
The code that perform manipulation do not need to know the name of the table, just the row and max num of columns
There are some general things going on, you wat to add row/delete row or change information
So dependent on which row is selected when UserForm is activated, it's this row you want to manipulate
(Of course I'm careful with what's already added and what can be deleted, but things can be added in wrong order
or things like that. And nothing is deleted without printing info from selected row to user, and make him confirm)

I hope this will clarify what's my intention
But I'm open for other thoughts

Jo-Helge
 
Upvote 0
You was asking how having 3 userforms, all populated by the same controls, running the same event macro; my implicit suggestion was “use a single userform and there is no any problem to be solved”
Your detailed description of your environment and the overall objectives doesn’t say that using a single userform is not feasible. So I stay with my suggestion of using only one single userform.
I understand the data to be manipultaed lies on the activesheet, so dealing with them should be not a problem; anyway, if you need you may set different scope based on which commandbutton initiated the userform.
 
Upvote 0
You was asking how having 3 userforms, all populated by the same controls, running the same event macro; my implicit suggestion was “use a single userform and there is no any problem to be solved”
Your detailed description of your environment and the overall objectives doesn’t say that using a single userform is not feasible. So I stay with my suggestion of using only one single userform.
I understand the data to be manipultaed lies on the activesheet, so dealing with them should be not a problem; anyway, if you need you may set different scope based on which commandbutton initiated the userform.
Hi and thank's for your reply.

I'm sorry if I didn't made me clear enough in the beginning, the 2 sheets that i presented are pretty equal, I use a common UF for thoose,
Why I mentioned them, was that they where the next ones to solve

I have made code for the member table, and if I do this straight forward, I have to duplicate the code for AccountDoc with smal modifications,
They do the same operations on a different table.

The thing is that I have code to manipulate member table, and there is no secret in that, I can present it, the problem is its not written in English, i have to translate.
OnAddMember_Click()
UF_Member.Tag = 1
UF_Member.Show
End sub
OnChangeMember_Click()
UF_Member.Tag = 2
UF_Member.Show
End sub
OnDeleteMembes_Click()
UF_Member.Tag = 3
UF_Member.Show
End sub

'UF_Member code class:
Private Sub UserForm_Activate()
If Me.Tag = 1 Then
Call AddNewMember
ElseIf Me.Tag = 2 Then
Call ChangeNewMember
ElseIf Me.Tag = 3 Then
Call DeleteNewMember
Sub SetupAddMember()
Me.Caption="Create member"
...
Me.btnAddMember.Visible = True
End Sub
Sub SetupChangeMember()
Me.Caption="Change member"
...
Me.btnChangeMember.Visible = True
End Sub
Sub SetupDeleteMember()
Me.Caption="Delete member"
...
Me.btnDeleteMember.Visible = True
End Sub
Private Sub btnAddMember_Click()
Dim arr() As Variant
...
arr = StoreUFInArray()
Call AddMember(arr)
Unload Me
End Sub
Private Sub btnChangeMember_Click()
Dim arr() As Variant
...
arr = StoreUFInArray()
Call ChangeMember(CLng(Me.tb_LineInTbl), arr)
Unload Me
End Sub
Private Sub btnDeleteMember_Click()
Dim arr() As Variant
...
arr = StoreUFInArray()
Call DeleteMember(arr)
Unload Me
End Sub
So far I have decided that te UF_ code should not have any code related to the worksheet
I have isolated the code to a modAction module
The other thing is that the process to transfer the data from UF_ to sheet is done by sending an array
But if you use an event, the data had to use another way to get the data from userform

The code below is present like it is now, for 1 table in one sheet. Has to be modified .
modAction:
Public Sub AddMember(arr() As Variant)
Dim appstate As CAppState

Err.Clear
On Error GoTo ErrorHandling

With Sheet4
'turn of events and GUI updates
Set appstate = New CAppState
appstate.SetState None
With .ListObject(1)
'At start the table has a empty row, do not add then
If CLng(arr(1)) > 1 Then
.ListRows.Add AlwaysInsert:=True
End If
Call AddArrayToTableRow(.DataBodyRange.Rows.Count, arr())
Range("MedlNr").value = arr(1)
End With

Cleanup:
Set appstate = Nothing
End With
Exit Sub

ErrorHandling:
...
End Sub

Public Sub ChangeMember(row as Long, arr() As Variant)
Dim appstate As CAppState

Err.Clear
On Error GoTo ErrorHandling

With Sheet4
'turn of events and GUI updates
Set appstate = New CAppState
appstate.SetState None
With .ListObject(1)
if row > HeaderRowRange.Row and row < .HeaderRow.Rows.Count Then
Call AddArrayToTableRow(row, arr())
Endif
End With
...
End Sub

Sub AddArrayToTableRow(rad_nr As Long, arr() As Variant)
Dim Tag As Integer
Dim appstate As CAppState

Err.Clear
On Error GoTo ErrorHandling

With Sheet4
Set appstate = New CAppState
appstate.SetState None

With .ListObject(1)
If IsNumeric(arr(0)) Then
Tag = CInt(arr(0))
Else
MsgBox "Wrong Format on sent Message"
Exit Sub
End If
If Tag = 1 Then
If rad_nr = .Rows.Count Then
.Cells(rad_nr, 1).value = arr(1)
.Cells(rad_nr, 2).value = Format(CDate(arr(2)), "dd/mm/yyyy")
.Cells(rad_nr, 3).value = arr(3)
.Cells(rad_nr, 4).value = arr(4)
.Cells(rad_nr, 5).value = arr(5)
.Cells(rad_nr, 6).value = arr(6)
End If
ElseIf Tag = 2 Then
If rad_nr > 0 And rad_nr <= .Rows.Count Then
'Member num and date do not change
'Fill all fields even if its not changed
.Cells(rad_nr, 3).value = arr(3)
.Cells(rad_nr, 4).value = arr(4)
.Cells(rad_nr, 5).value = arr(5)
.Cells(rad_nr, 6).value = arr(6)
'Kontigent legges til ved Tag = 3
End If
ElseIf Tag = 3 Then
MsgBox("Wrong logic should newer end up here")
Else
MsgBox ("Member dialog wrong configurated")
End If
End With
-----
If I understand you, you think to use the events in each user form and call the
AddMember, ChangeMember and DeleteMember from UF without using events
Theese functions has to be made more general
It might be difficult to make connection between an CButton class and the UF's
I've seen somewhere to use a callback to register your class/UF at the other side of the connection

When I sad 3 UserForms. I thought in general termes, there are differences between Members and AccountDoc
I was looking for a general way to do it, and was trying to learn more about events,
It might be that this wasn't right way to solve it this time, I'm not an expert, just looking for answers

Agen I'm sorry for the inconvenience, but I'm always focusing positive and think positive
even if you had some comments to my so far added information
Thanks for now, I'll be answering a respons on monday

Jo-Helge
 
Upvote 0
I found one translation error

'UF_Member code class:
Private Sub UserForm_Activate()
If Me.Tag = 1 Then
Call SetupAddMember
ElseIf Me.Tag = 2 Then
Call SetupChangeMember
ElseIf Me.Tag = 3 Then
Call SetupDeleteMember

They should call the Setup... Sub's
 
Upvote 0
Sorry I didn't answer sooner, but I believe the topics under discussion are more complex than I am able to support in this forum
 
Upvote 0
My knowledge comes only from practice, I cannot suggest anything
Maybe it would be better open a discussion with this request, this old one hardly will catch the attention from experts
 
Upvote 0
Thank you for flagging my post, although I am afraid I haven't solved any of your doubts
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,093
Members
448,944
Latest member
SarahSomethingExcel100

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top