I have a userform1 named "srForm", that when save commandbutton is clicked will put the data in my main worksheet("srData"). In the first column "A" a unique ID will be placed generate by the userform_initialize.
The userform_initialize code is as followed:
Private Sub UserForm_Initialize()
Me.srID.Enabled = True
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("srData")
'find last data row from database
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
If ws.[a2].Value = "" Then
Me.srID.Text = "SR-" & 1
Else
With ws.Cells(iRow, 1)
Me.srID.Text = Left(.Value, 3) & CInt(Right(.Value, Len(.Value) - 3)) + 1
End With
End If
End Sub
In the userform1 there is a checkbox "zeefTest", if this is checked a 2nd userform "Fracties" will open. This userform contains more checkboxes and a select commandbutton. When the select commandbutton is checked the data will be put in worksheet("Result_Particles"). In this worksheet column "A" is also the unique ID. But this is dependent on the zeeftest checkbox being checked. The code for calling userform2 "Fracties" is :
Private Sub zeefTest_Click()
Fracties.Show
Dim sRow As Long
With Worksheets("Result_Particles").Range("A1")
sRow = Worksheets("Result_Particles").Range("A1").CurrentRegion.Rows.Count
.Offset(sRow, 0).Value = Me.srID.Value
End With
End Sub
Now when 2nd userform select commandbutton is clicked the userform unloads and the 1st userform save button can be clicked.
The problem I am having is that the userform1 code does not work correctly with code in userform2. I think it overrides the userform2 code when I click on the Save commandbutton in userform 1. The unique ID code is put in the worksheet("Result_Particles") properly, but the checkbox interior.colorindex does not write to the proper columns. It only does it correctly for the first row. The code for userform2 is:
Private Sub CmB1_Click()
Dim iRow As Long
With Worksheets("Result_Particles").Range("A1")
iRow = Worksheets("Result_Particles").Range("A1").CurrentRegion.Rows.Count
If Cbx1.Value = True Then
.Offset(iRow, 1).Interior.ColorIndex = 2
Else:
.Offset(iRow, 1).Interior.ColorIndex = 15
End If
If Cbx2.Value = True Then
.Offset(iRow, 2).Interior.ColorIndex = 2
Else:
.Offset(iRow, 2).Interior.ColorIndex = 15
End If
If Cbx3.Value = True Then
.Offset(iRow, 3).Interior.ColorIndex = 2
Else:
.Offset(iRow, 3).Interior.ColorIndex = 15
End If
If Cbx4.Value = True Then
.Offset(iRow, 4).Interior.ColorIndex = 2
Else:
.Offset(iRow, 4).Interior.ColorIndex = 15
End If
If Cbx5.Value = True Then
.Offset(iRow, 5).Interior.ColorIndex = 2
Else:
.Offset(iRow, 5).Interior.ColorIndex = 15
End If
If Cbx6.Value = True Then
.Offset(iRow, 6).Interior.ColorIndex = 2
Else:
.Offset(iRow, 6).Interior.ColorIndex = 15
End If
If Cbx7.Value = True Then
.Offset(iRow, 7).Interior.ColorIndex = 2
Else:
.Offset(iRow, 7).Interior.ColorIndex = 15
End If
If Cbx8.Value = True Then
.Offset(iRow, 8).Interior.ColorIndex = 2
Else:
.Offset(iRow, 8).Interior.ColorIndex = 15
End If
If Cbx9.Value = True Then
.Offset(iRow, 9).Interior.ColorIndex = 2
Else:
.Offset(iRow, 9).Interior.ColorIndex = 15
End If
If Cbx10.Value = True Then
.Offset(iRow, 10).Interior.ColorIndex = 2
Else:
.Offset(iRow, 10).Interior.ColorIndex = 15
End If
If Cbx11.Value = True Then
.Offset(iRow, 11).Interior.ColorIndex = 2
Else:
.Offset(iRow, 11).Interior.ColorIndex = 15
End If
If Cbx12.Value = True Then
.Offset(iRow, 12).Interior.ColorIndex = 2
Else:
.Offset(iRow, 12).Interior.ColorIndex = 15
End If
End With
Unload Me
End Sub
I hope someone can help me fix my code to work properly. I know this is not the neatest code so if anyone also knows a beter way to write the code for my checkboxes, this would also be much appreciated.
Thank you for your time.
Privateand 3) replace the keywordMewithUserform2or with userform2's name if you have renamed it..offset(iRow,0).Value = Userform2.srID.value- dimitris