Run Time Error 13 - mismatch type Macro issue

Marcfar

New Member
Joined
May 28, 2020
Messages
7
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
Hi Folks - I am a novice VBA user at best. I have some code below for a form I created using various pieces of code found on the excel forums, so there is no style/form to my code.
Anyways, when I go to submit my form partially blank, I get the error.

I am getting my error when I push the command button and the code attempting to copy the data from the form to a second sheet/tab in my file.

I am using a listbox that where when an item is double clicked it populates a 2nd listbox with them item (this is occuring in 2 different instances). The second list box collects that info, and if an item is double clicked in the 2nd list box, the data is removed from the that 2nd listbox. If the 2nd listbox is blank (no items were added to the listbox) when the form is submitted, then I get the run-time error. I assume it is because the listbox is empty and the code looking to copy data.

Anyone know of a code (I am thinking its an IF statement maybe), that would let the command button execute even if the 2nd listbox remains empty as there is no data to copy?

Here the VBA code for the listbox
VBA Code:
Private Sub ListBox4_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
   ListBox4.RemoveItem ListBox4.ListIndex

End Sub

Here is the command button coding (code in bold red is giving me the error when the listbox is empty):
Rich (BB code):
Private Sub CommandButton2_Click()

ThisWorkbook.Worksheets("Form_Data").Range("B2").Value = TextBox1
ThisWorkbook.Worksheets("Form_Data").Range("B3").Value = TextBox2
ThisWorkbook.Worksheets("Form_Data").Range("B10").Value = TextBox6
ThisWorkbook.Worksheets("Form_Data").Range("B11").Value = TextBox7
ThisWorkbook.Worksheets("Form_Data").Range("B8").Value = TextBox8
ThisWorkbook.Worksheets("Form_Data").Range("B6").Value = TextBox9

ThisWorkbook.Worksheets("Form_Data").Range("B5").Value = ComboBox1
ThisWorkbook.Worksheets("Form_Data").Range("B7").Value = ComboBox2


Dim x
x = Me.ListBox4.List
ThisWorkbook.Worksheets("Form_Data").Range("A14").Resize(UBound(x) + 1, 1).Value = x

x = Me.ListBox5.List
ThisWorkbook.Worksheets("Form_Data").Range("b14").Resize(UBound(x) + 1, 1).Value = x


If OptionButton4.Value = True Then
         ThisWorkbook.Worksheets("Form_Data").Range("B9").Value = "No"
     End If
If OptionButton5.Value = True Then
          ThisWorkbook.Worksheets("Form_Data").Range("B9").Value = "Yes"
     End If
If OptionButton6.Value = True Then
         ThisWorkbook.Worksheets("Form_Data").Range("B4").Value = "Change an Existing User"
     End If
  If OptionButton7.Value = True Then
          ThisWorkbook.Worksheets("Form_Data").Range("B4").Value = "Add a New User"
     End If
If OptionButton8.Value = True Then
          ThisWorkbook.Worksheets("Form_Data").Range("B4").Value = "Inactivate an Existing User"
     End If
   
   
'copy to email in outlook and display send window
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
'Only the visible cells in the selection
    Set rng = Sheets("Form_Data").Range("a1:b30").SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = "mfarooqui2@wrha.mb.ca"
        .CC = ""
        .BCC = ""
        .Subject = "ESP User Request Form"
        .HTMLBody = RangetoHTML(rng)
        .Display  'or use .send
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
'part of copy to email in outlook and display send window
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

'Close TempWB
    TempWB.Close savechanges:=False

'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

Any assistance would be helpful. TIA
 
Last edited by a moderator:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
How about
VBA Code:
If IsArray(x) Then ThisWorkbook.Worksheets("Form_Data").Range("A14").Resize(UBound(x) + 1, 1).Value = x
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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