Type mismatch error when no values added

mykulpasskwa

Board Regular
Joined
Mar 20, 2018
Messages
66
Hello again

I have the code below working that someone wrote for me. It works great, but when a textbox on the multipage is blank I get a Type mismatch error. I've tried writing an error check and everything, but I just keep getting errors. Does anyone know how to make it so that when the text box is blank it just adds the textboxes that do have numbers?

Code:
Private Sub cbEnterFees_Click()
   
'Enters UserForm data onto spreadsheet
    Dim ans As Long
    Dim i As Long
  
                 
    i = 0
    For Each Control In MultiPage1.Pages(i).Controls 'For each control on Multipage1
        If TypeName(Control) = "TextBox" Then ans = ans + Control.Value 'the answer is the answer plus the value in the textbox
        Next
    ActiveCell.Offset(0, 2).Value = ans
    Unload FeesForm


 
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hello again

I have the code below working that someone wrote for me. It works great, but when a textbox on the multipage is blank I get a Type mismatch error. I've tried writing an error check and everything, but I just keep getting errors. Does anyone know how to make it so that when the text box is blank it just adds the textboxes that do have numbers?

Code:
Private Sub cbEnterFees_Click()
   
'Enters UserForm data onto spreadsheet
    Dim ans As Long
    Dim i As Long
  
                 
    i = 0
    For Each Control In MultiPage1.Pages(i).Controls 'For each control on Multipage1
        If TypeName(Control) = "TextBox" Then ans = ans + Control.Value 'the answer is the answer plus the value in the textbox
        Next
    ActiveCell.Offset(0, 2).Value = ans
    Unload FeesForm


 
End Sub

Code:
Private Sub cbEnterFees_Click()
'Enters UserForm data onto spreadsheet
    Dim ans As Long
    Dim i As Long
    i = 0
    For Each Control In MultiPage1.Pages(i).Controls 'For each control on Multipage1
        If TypeName(Control) = "TextBox" Then
            If Control.Value <> "" Then
                ans = ans + Val(Control.Value) 'the answer is the answer plus the value in the textbox
            End If
        Next
    ActiveCell.Offset(0, 2).Value = ans
    Unload FeesForm
End Sub
That will validate it for you.
 
Upvote 0
I love this community. Thanks! I added another End If after the first End If cause I was getting a For without Next error, but otherwise that worked PERFECTLY. Thanks again.

Code:
Private Sub cbEnterFees_Click()
'Enters UserForm data onto spreadsheet
 'Modified  5/21/2019  9:02:00 PM  EDT
    Dim ans As Long
    Dim i As Long
    i = 0
    For Each Control In MultiPage1.Pages(i).Controls 'For each control on Multipage1
        If TypeName(Control) = "TextBox" Then
            If Control.Value <> "" Then
                ans = ans + Val(Control.Value) 'the answer is the answer plus the value in the textbox
            End If
            End If
        Next
    ActiveCell.Offset(0, 2).Value = ans
    Unload FeesForm


 
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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