VBA multiple if statements, require all to run

learning1984

New Member
Joined
Mar 2, 2016
Messages
14
Good evening,

ive been attacking this code for ages and just cant get it to run. its not the tidiest code but it made sense to do it this way in my head. ive created a userform,which I want to transfer into 5 rows in a spreadsheet. the first few columns would all contain the same info, name, date etc, but the ones after will contain 1 of 5 fields
every rime I import a completed form, it only transfers the final if statement across into my spread sheet,
how can I amend the code so that the first if statement is honoured, and then it moves onto the next if statement.
as a side note, I am also adding a section of code that will force the user to fill in all 5 of these different fields, so every time I import the form, it should always create 5 rows of information.

my code is here:

Private Sub CommandButton1_Click()

Application.ScreenUpdating = False
Dim nextrow As Long


'Make Log active
log.Activate
If areadam01 <> "" Then


'Determine nextrow
nextrow = WorksheetFunction.CountA(Range("a:a")) + 1

Cells(nextrow, 2).Value = TextBox7.Value
Cells(nextrow, 3).Value = CDate(TextBox78.Value)
Cells(nextrow, 4).Value = ComboBox16.Value
Cells(nextrow, 5).Value = ComboBox15.Value
Cells(nextrow, 6).Value = ComboBox9.Value
Cells(nextrow, 7).Value = ComboBox8.Value
Cells(nextrow, 8).Value = TextBox9.Value
Cells(nextrow, 9).Value = ComboBox10.Value
Cells(nextrow, 10).Value = ComboBox1.Value
Cells(nextrow, 11).Value = Floc01.Text
Cells(nextrow, 12).Value = Equip01.Text
Cells(nextrow, 13).Value = Ref01.Text
Cells(nextrow, 14).Value = areadam01.Value
Cells(nextrow, 15).Value = areatime01.Value
Cells(nextrow, 16).Value = areadesc01.Value
End If

If areadam02 <> "" Then
nextrow = WorksheetFunction.CountA(Range("a:a")) + 1

Cells(nextrow, 2).Value = TextBox7.Value
Cells(nextrow, 3).Value = CDate(TextBox78.Value)
Cells(nextrow, 4).Value = ComboBox16.Value
Cells(nextrow, 5).Value = ComboBox15.Value
Cells(nextrow, 6).Value = ComboBox9.Value
Cells(nextrow, 7).Value = ComboBox8.Value
Cells(nextrow, 8).Value = TextBox9.Value
Cells(nextrow, 9).Value = ComboBox10.Value
Cells(nextrow, 10).Value = ComboBox1.Value
Cells(nextrow, 11).Value = Floc02.Value
Cells(nextrow, 12).Value = Equip02.Value
Cells(nextrow, 13).Value = Ref02.Value
Cells(nextrow, 14).Value = areadam02.Value
Cells(nextrow, 15).Value = areatime02.Value
Cells(nextrow, 16).Value = areadesc02.Value

End If

If areadam03 <> "" Then
nextrow = WorksheetFunction.CountA(Range("a:a")) + 1
Cells(nextrow, 2).Value = TextBox7.Value
Cells(nextrow, 3).Value = CDate(TextBox78.Value)
Cells(nextrow, 4).Value = ComboBox16.Value
Cells(nextrow, 5).Value = ComboBox15.Value
Cells(nextrow, 6).Value = ComboBox9.Value
Cells(nextrow, 7).Value = ComboBox8.Value
Cells(nextrow, 8).Value = TextBox9.Value
Cells(nextrow, 9).Value = ComboBox10.Value
Cells(nextrow, 10).Value = ComboBox1.Value
Cells(nextrow, 11).Value = Floc03.Value
Cells(nextrow, 12).Value = Equip03.Value
Cells(nextrow, 13).Value = Ref03.Value
Cells(nextrow, 14).Value = areadam03.Value
Cells(nextrow, 15).Value = areatime03.Value
Cells(nextrow, 16).Value = areadesc03.Value

End If

If areadam04 <> "" Then
nextrow = WorksheetFunction.CountA(Range("a:a")) + 1
Cells(nextrow, 2).Value = TextBox7.Value
Cells(nextrow, 3).Value = CDate(TextBox78.Value)
Cells(nextrow, 4).Value = ComboBox16.Value
Cells(nextrow, 5).Value = ComboBox15.Value
Cells(nextrow, 6).Value = ComboBox9.Value
Cells(nextrow, 7).Value = ComboBox8.Value
Cells(nextrow, 8).Value = TextBox9.Value
Cells(nextrow, 9).Value = ComboBox10.Value
Cells(nextrow, 10).Value = ComboBox1.Value
Cells(nextrow, 11).Value = Floc04.Value
Cells(nextrow, 12).Value = Equip04.Value
Cells(nextrow, 13).Value = Ref04.Value
Cells(nextrow, 14).Value = areadam04.Value
Cells(nextrow, 15).Value = areatime04.Value
Cells(nextrow, 16).Value = areadesc04.Value

End If

If areadam05 <> "" Then
nextrow = WorksheetFunction.CountA(Range("a:a")) + 1
Cells(nextrow, 2).Value = TextBox7.Value
Cells(nextrow, 3).Value = CDate(TextBox78.Value)
Cells(nextrow, 4).Value = ComboBox16.Value
Cells(nextrow, 5).Value = ComboBox15.Value
Cells(nextrow, 6).Value = ComboBox9.Value
Cells(nextrow, 7).Value = ComboBox8.Value
Cells(nextrow, 8).Value = TextBox9.Value
Cells(nextrow, 9).Value = ComboBox10.Value
Cells(nextrow, 10).Value = ComboBox1.Value
Cells(nextrow, 11).Value = Floc05.Value
Cells(nextrow, 12).Value = Equip05.Value
Cells(nextrow, 13).Value = Ref05.Value
Cells(nextrow, 14).Value = areadam05.Value
Cells(nextrow, 15).Value = areatime05.Value
Cells(nextrow, 16).Value = areadesc05.Value
End If

Dim theRange As Range, theCell As Range, nextLong As Long
Set theRange = log.Range("a4", log.Range("a" & Rows.Count).End(xlUp))
Set theCell = log.Range("a" & Rows.Count).End(xlUp).Offset(1)
nextLong = NextNumber(theRange)
theCell.Value = nextLong
theCell.NumberFormat = """TUSC.LOSS"" 000"
Unload Me
Range("a" & Rows.Count).End(xlUp).Select

End Sub
<strike></strike>
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
The code is not putting anything in column A so nextrow does not change.
Use another column to set nextrow.
 
Upvote 0
The section at the bottom is to get column A completed, it's meant to be an auto update next number

But nextrow needs to be updated 4 times before that.
Step through the code to see what values get assigned to nextrow.
 
Upvote 0
Wow im getting more confused.

If I change the code, I can get it to run for just one row of data, im unsure what code to use to make it update the next row mid code
 
Upvote 0
Step through your original code via F8 so you can see the value of nextrow

I suggest you try changing the following in all 5 places it occurs :

nextrow = WorksheetFunction.CountA(Range("a:a")) + 1

to this :

nextrow = WorksheetFunction.CountA(Range("b:b")) + 1
 
Upvote 0

Forum statistics

Threads
1,215,386
Messages
6,124,628
Members
449,176
Latest member
Dazjlbb

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