I have a userform for inputting data and emailing the data. The problem I'm having is when I open the userform, if excel is already open it closes excel, and you can't open it. Would like to be able to open and close other worksheets while the Userform is open. Would also like to be able to open and close Userform while other worksheets are open.
I changed the Showmodal property of the Userform1 to false. Now I can open other worksheets only after the Userform is open, and when anything is closed everything closes.
Thank you for your assistance.
Nate
I changed the Showmodal property of the Userform1 to false. Now I can open other worksheets only after the Userform is open, and when anything is closed everything closes.
Thank you for your assistance.
Nate
VBA Code:
Private Sub cmdExit_Click()
Application.DisplayAlerts = False
Dim iRow As Long
Dim ws As Worksheet
Dim PPNbrs As Variant
Dim j As Long
Set ws = Worksheets("Sheet1")
'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
'check for a Region
If Trim(Me.ComboBox1.Value) = "" Then
Me.ComboBox1.SetFocus
MsgBox "Please Select Region"
Exit Sub
End If
'check for a PertinentDetails
If Trim(Me.txtPertinentDetails.Value) = "" Then
Me.txtPertinentDetails.SetFocus
MsgBox "Please enter Pertinent Details"
Exit Sub
End If
'check for a Site Name
If Trim(Me.txtSiteName.Value) = "" Then
Me.txtSiteName.SetFocus
MsgBox "Please enter a Site Name"
Exit Sub
End If
'check for a Site ID
If Trim(Me.txtSiteID.Value) = "" Then
Me.txtSiteID.SetFocus
MsgBox "Please enter a Site ID"
Exit Sub
End If
'check for Expectations
If Trim(Me.txtExpectations.Value) = "" Then
Me.txtExpectations.SetFocus
MsgBox "Please enter Expectations"
Exit Sub
End If
'Check to make sure that TxtPatientID is splittable
If InStr(Me.txtPatientID.Value, ",") > 0 Then
PPNbrs = Split(Me.txtPatientID.Value, ",")
Else
PPNbrs = Array(Me.txtPatientID.Value)
End If
For j = LBound(PPNbrs) To UBound(PPNbrs)
'copy the data to the database
ws.Cells(iRow + j, 1) = txtDate.Text
ws.Cells(iRow + j, 2) = txtSSRName.Text
ws.Cells(iRow + j, 3) = txtFRSName.Text
ws.Cells(iRow + j, 4) = txtContactNumber.Text
ws.Cells(iRow + j, 5) = ComboBox1
ws.Cells(iRow + j, 6) = txtPreviouslyReported.Text
ws.Cells(iRow + j, 7) = cboTopics
ws.Cells(iRow + j, 8) = cboIssues
ws.Cells(iRow + j, 9) = ComboBox3
ws.Cells(iRow + j, 10) = PPNbrs(j)
ws.Cells(iRow + j, 12) = ComboBox2
ws.Cells(iRow + j, 16) = txtPertinentDetails.Text
ws.Cells(iRow + j, 17) = txtOtherImportant.Text
ws.Cells(iRow + j, 19) = txtClaimDenial.Text
ws.Cells(iRow + j, 20) = txtPayer.Text
ws.Cells(iRow + j, 21) = txtDOS.Text
ws.Cells(iRow + j, 22) = txtAppealInfo.Text
ws.Cells(iRow + j, 23) = txtSiteName.Text
ws.Cells(iRow + j, 25) = txtHCPName.Text
ws.Cells(iRow + j, 24) = txtSiteID.Text
ws.Cells(iRow + j, 26) = txtSiteContact.Text
ws.Cells(iRow + j, 27) = txtSitePhone.Text
ws.Cells(iRow + j, 28) = txtBestTime.Text
ws.Cells(iRow + j, 29) = txtExpectations.Text
Next j
ws.Cells.WrapText = False
'clear the data
Me.txtDate.Text = ""
Me.txtSSRName.Text = ""
Me.txtFRSName.Text = ""
Me.txtContactNumber.Text = ""
Me.ComboBox1 = ""
Me.txtPreviouslyReported.Text = ""
Me.cboTopics = ""
Me.cboIssues = ""
Me.ComboBox3 = ""
Me.txtPatientID.Text = ""
Me.ComboBox2 = ""
Me.txtPertinentDetails.Text = ""
Me.txtOtherImportant.Text = ""
Me.txtClaimDenial.Text = ""
Me.txtPayer.Text = ""
Me.txtDOS.Text = ""
Me.txtAppealInfo.Text = ""
Me.txtSiteName.Text = ""
Me.txtHCPName.Text = ""
Me.txtSiteID.Text = ""
Me.txtSiteContact.Text = ""
Me.txtSitePhone.Text = ""
Me.txtBestTime.Text = ""
Me.txtExpectations.Text = ""
Application.Visible = True
Unload Me
ActiveWorkbook.Save
Application.DisplayAlerts = True
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the ActiveSheet to a new workbook
Sheets("Sheet1").Copy
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2016
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End With
' 'Change all cells in the worksheet to values if you want
' With Destwb.Sheets(1).UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "REGION- SUBJECT OF ESCALATION ISSUE- SITE NAME- SITE ID- PATIENT ID"
.Body = "*****Reminder Fill out the subject of the email with the REGION- SUBJECT OF ESCALATION ISSUE- SITE NAME- SITE ID- PATIENT ID- ***** Thanks, Nate"
.Attachments.Add Destwb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display
End With
On Error GoTo 0
.Close Savechanges:=False
End With
'Delete the file you have sent
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Sheet1.Range("A2:AJ100").ClearContents
ActiveWorkbook.Save
Application.DisplayAlerts = True
End Sub