Launch Multiple If Statements in UserForm

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
I must be going about this in the wrong way so I need some help. I have come to this conclusion since 1) it is not working correctly, and 2) I can't find any other posts about this.

I have built a userform with multiple input boxes. There are currently 6 rows of like data that can be entered. I only want each row written to the database if there is data entered on that row when the Add button is executed. I am including the code for my add button as well as a few of the code groupings to write to the database. If someone could help be figure out where it is wrong - it works perfectly as long as all rows contain data, but the norm is maybe 2 of the 6 rows have data.

Thanks in advance for your help.


Private Sub cmd_add_Click()
If txt_4hrs.Value > "0" Then AddData2x4
If txt_6hrs.Value > "0" Then AddData2x6
If txt_8hrs.Value > "0" Then AddData2x8
If txt_10hrs.Value > "0" Then AddData2x10
If txt_12hrs.Value > "0" Then AddData2x12
If txt_4x4hrs.Value > "0" Then AddData4x4
ClearData
End Sub

====================================

Private Sub AddData2x4()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("pph")
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ws.Cells(iRow, 1).Value = Me.txt_date.Value
ws.Cells(iRow, 2).Value = "2x4"
ws.Cells(iRow, 3).Value = Me.txt_4hrs.Value
ws.Cells(iRow, 4).Value = Me.txt_4volume.Value
ws.Cells(iRow, 5).Value = Me.txt_4mdtmin.Value
End Sub

====================================

Private Sub AddData2x6()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("pph")
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ws.Cells(iRow, 1).Value = Me.txt_date.Value
ws.Cells(iRow, 2).Value = "2x6"
ws.Cells(iRow, 3).Value = Me.txt_6hrs.Value
ws.Cells(iRow, 4).Value = Me.txt_6volume.Value
ws.Cells(iRow, 5).Value = Me.txt_6mdtmin.Value
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I must be going about this in the wrong way so I need some help. I have come to this conclusion since 1) it is not working correctly,
How incredibly logical :)

FYI: I had no issue with your procedures.
(I don't know what ClearData does - assume it clears the userform controls)
 
Upvote 0
...it works perfectly as long as all rows contain data, but the norm is maybe 2 of the 6 rows have data.
What is it doing wrongly when only some of the userform rows contain data? Is it writing data to your worksheet even when the userform row is empty?

Code:
If txt_4hrs.Value > "0" Then [COLOR=green]' <-- your code[/COLOR]
I'm not entirely happy with that... for example, a null value will be greater than the string "0":-
Code:
If vbNull > "0" Then MsgBox "I didn't expect that!"

Try:-
Code:
If Len(txt_4hrs)>0 Then [COLOR=green]' for non-numeric values[/COLOR]
or:-
Code:
If Val(txt_4hrs)>0 Then [COLOR=green]' for numeric values[/COLOR]
 
Upvote 0
If the value in txt_4hrs is numeric, try removing the quotes from around the zero.
If the norm for an empty row is blank then you could use
If txt_4hrs.Value <> "" Then AddData2x4
 
Upvote 0
All the information if numeric.
At first my code just launched all the AddData routines, but that added a blank for for every dimension even if that product wasn't ran that day which was making my database 3 times bigger than need be.

Currently, it will only post data if All rows have data, or if consecutive rows have data starting with the 2x4. when there is a blank row it stops.
 
Upvote 0
RichardMGreen - I tried the <>, when I do this it post for all rows, including blanks

Ruddles - you rock - this worked perfectly

If Val(txt_4hrs)>0 Then


Thanks you guys so much...A
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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