running private sub from another form

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I have two forms; frmTelephoneSearch and frmAddNewContact

On frmTelephoneSearch is a button that opens frmAddNewContact

in frmTelephoneSearch i have a private sub named; UserForm_Initialize()


I got a button on frmAddNewContact with this code in it;

Code:
Private Sub cmdAddNewContact_Click()On Error GoTo cmdAdd_Click_Error
'Go To Error if an error occurs
If Me.txtFullname.value = "" Then
Call MsgBox("The required fields are not complete", vbInformation, "Edit Contact")
Exit Sub
End If


If Worksheets("Telephone").Range("A2").value = "" Then
Worksheets("Telephone").Range("A2:F2").FormulaArray = "1"
End If
'set the destination range
Set Drng = Worksheets("Telephone").Range("A1")
'move the values without selecting
Drng.End(xlDown).Offset(1, 0).value = Me.txtFullname.value
Drng.End(xlDown).Offset(0, 1).value = Me.txtDepartment.value
Drng.End(xlDown).Offset(0, 2).value = Me.txtEmailadress.value
Drng.End(xlDown).Offset(0, 3).value = Me.txtPhone.value
Drng.End(xlDown).Offset(0, 6).value = Drng.End(xlDown).Offset(-1, 6).value + 1
'give the "all OK signal"
Call MsgBox("A new contact has been added", vbInformation, "Add Contact")
'sort the data
SortIt
Unload Me
On Error GoTo 0
Exit Sub
'if error occurs then show me exactly where the error occurs
cmdAdd_Click_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdAdd_Click of Form AddNewContact"
End Sub

Now i would like to add in this code that private sub named; UserForm_Initialize() is run that is in frmTelephoneSearch.

How to do this?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If the Code is in a different Module, you cannot call it if it is marked "Private". You would need to remove the "Private" from the beginning line of that code for it to be seen by other modules.

Also, you do not need to use "Call" in front of MsgBox. Just remove the word Call. You only use Call when calling other procedures.
 
Upvote 0

Forum statistics

Threads
1,214,840
Messages
6,121,895
Members
449,058
Latest member
Guy Boot

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