Ignore empty text boxes

Trueblue862

Board Regular
Joined
May 24, 2020
Messages
160
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet which I am using to perform simple calculations, dependent on certain variables. I'm using a userform to input data into certain cells in the spreadsheet, the only problem I'm having is I have to enter data into every field every time I want to tweak something in the calculation, which becomes very tedious. What I'm trying to achieve is essentially if the textbox has no data in it, then it is simply ignored and the code moves to the next textbox. Below is my very amateur attempt at this, I have tried a few variations on this and haven't really achieved anything in a number of hours. Any help would be greatly appreciated.

VBA Code:
Sub save_values()
  
    If TextBox1.Value <= 0 Then
        If TextBox2.Value > 0 Then Sheets("Ridge Line Metric").Range("A3") = Me.TextBox1.Value
    End If
    If TextBox2.Value <= 0 Then
          If TextBox2.Value > 0 Then Sheets("Ridge Line Metric").Range("H8") = Me.TextBox2.Value
    End If
    If TextBox3.Value <= 0 Then
         If TextBox3.Value > 0 Then Sheets("Ridge Line Metric").Range("N9") = Me.TextBox3.Value
    End If
    If TextBox4.Value <= 0 Then
         If TextBox4.Value > 0 Then Sheets("Ridge Line Metric").Range("T9") = Me.TextBox4.Value
    End If
    
    
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
VBA Code:
Sub save_values()
  
   If TextBox1.Value <> "" Then Sheets("Ridge Line Metric").Range("A3") = Me.TextBox1.Value
   If TextBox2.Value <> "" Then Sheets("Ridge Line Metric").Range("H8") = Me.TextBox2.Value
   If TextBox3.Value <> "" Then Sheets("Ridge Line Metric").Range("N9") = Me.TextBox3.Value
   If TextBox4.Value <> "" Then Sheets("Ridge Line Metric").Range("T9") = Me.TextBox4.Value
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Sorry, I was too quick on the trigger. It sort of works, but, if there is no value in textbox 1 then it exits the code. It only runs as far as the first empty textbox then exits.
 
Upvote 0
Nope, that code will check all the textboxes. There is nothing in the code that would cause it to exit.
 
Upvote 0
Are you running the code exactly as shown in post#2, or have you changed it all?
 
Upvote 0
I closed and reopened the workbook and now it's working fine. I don't know what happened there, thank you for your help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,530
Messages
6,114,162
Members
448,554
Latest member
Gleisner2

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