If checkbox is checked, transfer the data to a table, if not, do not transfer the data

egrospeRP

New Member
Joined
Sep 5, 2018
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi Excel Gurus,
I built a userform that allows the end user to input data that will ultimately transfer into a table. For 1 risk item, there are 3 possible risk assessments. The 3 possible risk assessments are transferred into a table called "Risk_Assessment". The way the userform functions, the user has an option to check a box to "enable" the fields to input the information for each risk assessment.

The logic is, if the checkbox is checked, when the submit button is clicked, it will transfer all the related information into a table, but if it's not checked, it shouldn't transfer any information. The challenge is for example, I only have one risk assessment, the other two risk assessments still get transfered into the excel table but with blank values.

How do i make this work? The vba code is below. Thanks in advance!

VBA Code:
Private Sub cmdSubmit_Click()

'Transfer to Risk Register table
Dim RiskNo As String
Dim RiskCategory As String
Dim RiskStatement As String
Dim RiskOwner As String
Dim ExistingControls As String
Dim ControlOwner As String
Dim ALARP As String
Dim Comments As String

RiskNo = txtRiskNo.Text
RiskCategory = cboRiskCat.Text
RiskStatement = txtRiskStatement.Text
RiskOwner = txtRiskOwner.Text
ExistingControls = txtExistControl.Text
ControlOwner = txtContOwner.Text
ALARP = cboALARP.Text
Comments = txtComments.Text

Dim wsRR As Worksheet
Set wsRR = Sheets("Risk Register")

Dim tblRR As ListObject
Set tblRR = wsRR.ListObjects("Risk_Register")
Dim newRowRR As ListRow
Set newRowRR = tblRR.ListRows.Add
With newRowRR
        .Range(1) = RiskNo
        .Range(2) = RiskCategory
        .Range(3) = RiskStatement
        .Range(4) = RiskOwner
        .Range(5) = ExistingControls
        .Range(6) = ControlOwner
        .Range(7) = ALARP
        .Range(11) = Comments
End With

'Transfer to Risk Assessment table

'Risk Assessment1
Dim RiskGrouping1 As String
Dim Severity1 As String
Dim Likelihood1 As String
Dim Mitigation1 As String
Dim MitigationOwner1 As String
Dim TargetDate1 As String
Dim MitSeverity1 As String
Dim MitLikelihood1 As String
Dim Comments1 As String
Dim MitStatus1 As String

RiskGrouping1 = cboRiskGrouping1.Text
Severity1 = cboSeverity1.Text
Likelihood1 = cboLikelihood1.Text
Mitigation1 = txtMitigation1.Text
MitigationOwner1 = txtMitigationOwner1.Text
TargetDate1 = txtTargetDate1.Text
MitSeverity1 = cboMSeverity1.Text
MitLikelihood1 = cboMLikelihood1.Text
Comments1 = txtComments1.Text
MitStatus1 = cboMitStatus1.Text

Dim wsRA1 As Worksheet
Set wsRA1 = Sheets("Risk Assessment")

Dim tblRA1 As ListObject
Set tblRA1 = wsRA1.ListObjects("Risk_Assessment")
Dim newRowRA1 As ListRow
Set newRowRA1 = tblRA1.ListRows.Add

If chkEnable1.Value = True Then
    With newRowRA1
            .Range(1) = RiskNo
            .Range(2) = 1
            .Range(3) = RiskGrouping1
            .Range(4) = Severity1
            .Range(5) = Likelihood1
            .Range(7) = Mitigation1
            .Range(8) = MitigationOwner1
            .Range(9) = TargetDate1
            .Range(10) = MitSeverity1
            .Range(11) = MitLikelihood1
            .Range(13) = MitStatus1
            .Range(14) = Comments1
    End With
    Else
End If


'Risk Assessment2
Dim RiskGrouping2 As String
Dim Severity2 As String
Dim Likelihood2 As String
Dim Mitigation2 As String
Dim MitigationOwner2 As String
Dim TargetDate2 As String
Dim MitSeverity2 As String
Dim MitLikelihood2 As String
Dim Comments2 As String
Dim Mitstatus2 As String

RiskGrouping2 = cboRiskGrouping2.Text
Severity2 = cboSeverity2.Text
Likelihood2 = cboLikelihood2.Text
Mitigation2 = txtMitigation2.Text
MitigationOwner2 = txtMitigationOwner2.Text
TargetDate2 = txtTargetDate2.Text
MitSeverity2 = cboMSeverity2.Text
MitLikelihood2 = cboMLikelihood2.Text
Comments2 = txtComments2.Text
Mitstatus2 = cboMitStatus2.Text

Dim wsRA2 As Worksheet
Set wsRA2 = Sheets("Risk Assessment")

Dim tblRA2 As ListObject
Set tblRA2 = wsRA2.ListObjects("Risk_Assessment")
Dim newRowRA2 As ListRow
Set newRowRA2 = tblRA2.ListRows.Add


If chkEnable2.Value = True Then
    With newRowRA2
            .Range(1) = RiskNo
            .Range(2) = 2
            .Range(3) = RiskGrouping2
            .Range(4) = Severity2
            .Range(5) = Likelihood2
            .Range(7) = Mitigation2
            .Range(8) = MitigationOwner2
            .Range(9) = TargetDate2
            .Range(10) = MitSeverity2
            .Range(11) = MitLikelihood2
            .Range(13) = Mitstatus2
            .Range(14) = Comments2
    End With
    Else
End If


'Risk Assessment3
Dim RiskGrouping3 As String
Dim Severity3 As String
Dim Likelihood3 As String
Dim Mitigation3 As String
Dim MitigationOwner3 As String
Dim TargetDate3 As String
Dim MitSeverity3 As String
Dim MitLikelihood3 As String
Dim Comments3 As String
Dim MitStatus3 As String

RiskGrouping3 = cboRiskGrouping3.Text
Severity3 = cboSeverity3.Text
Likelihood3 = cboLikelihood3.Text
Mitigation3 = txtMitigation3.Text
MitigationOwner3 = txtMitigationOwner3.Text
TargetDate3 = txtTargetDate3.Text
MitSeverity3 = cboMSeverity3.Text
MitLikelihood3 = cboMLikelihood3.Text
Comments3 = txtComments3.Text
MitStatus3 = cboMitStatus3.Text

Dim wsRA3 As Worksheet
Set wsRA3 = Sheets("Risk Assessment")

Dim tblRA3 As ListObject
Set tblRA3 = wsRA3.ListObjects("Risk_Assessment")
Dim newRowRA3 As ListRow
Set newRowRA3 = tblRA3.ListRows.Add


If chkEnable3.Value = True Then
    With newRowRA3
            .Range(1) = RiskNo
            .Range(2) = 3
            .Range(3) = RiskGrouping3
            .Range(4) = Severity3
            .Range(5) = Likelihood3
            .Range(7) = Mitigation3
            .Range(8) = MitigationOwner3
            .Range(9) = TargetDate3
            .Range(10) = MitSeverity3
            .Range(11) = MitLikelihood3
            .Range(13) = MitStatus3
            .Range(14) = Comments3
    End With
    Else
End If

Unload Me

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi @egrospeRP. Thanks for posting on the forum.

Before adding the new row in the table you must verify if the checkbox is marked.
I see in your code that the controls are repeated 3 times, and what changes is the number 1, 2 or 3, for example: cboRiskGrouping1, cboRiskGrouping2 and cboRiskGrouping3.
I also recommend you that it is not necessary to pass the value of the control to a variable and then the variable to the cell. You can directly pass the value of the control to the cell:

So considering the above, the code can be simplified like this:
VBA Code:
Private Sub cmdSubmit_Click()
  Dim wsRR As Worksheet, wsRA As Worksheet
  Dim tblRR As ListObject, tblRA As ListObject
  Dim i As Long

  Set wsRR = Sheets("Risk Register")
  Set tblRR = wsRR.ListObjects("Risk_Register")
  Set wsRA = Sheets("Risk Assessment")
  Set tblRA = wsRA.ListObjects("Risk_Assessment")
 
  'Transfer to Risk Register table
  With tblRR.ListRows.Add
    .Range(1) = txtRiskNo.Text
    .Range(2) = cboRiskCat.Text
    .Range(3) = txtRiskStatement.Text
    .Range(4) = txtRiskOwner.Text
    .Range(5) = txtExistControl.Text
    .Range(6) = txtContOwner.Text
    .Range(7) = cboALARP.Text
    .Range(11) = txtComments.Text
  End With
 
  'Transfer to Risk Assessment table 'Risk Assessment1
  For i = 1 To 3
    If Controls("chkEnable" & i).Value = True Then
      With tblRA.ListRows.Add
        .Range(1) = txtRiskNo.Text
        .Range(2) = i
        .Range(3) = Controls("cboRiskGrouping" & i).Text
        .Range(4) = Controls("cboSeverity" & i).Text
        .Range(5) = Controls("cboLikelihood" & i).Text
        .Range(7) = Controls("txtMitigation" & i).Text
        .Range(8) = Controls("txtMitigationOwner" & i).Text
        .Range(9) = Controls("txtTargetDate" & i).Text
        .Range(10) = Controls("cboMSeverity" & i).Text
        .Range(11) = Controls("cboMLikelihood" & i).Text
        .Range(13) = Controls("cboMitStatus" & i).Text
        .Range(14) = Controls("txtComments" & i).Text
      End With
    End If
  Next
  Unload Me
End Sub

--------------

Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Last edited:
Upvote 0
Solution
Hi @egrospeRP. Thanks for posting on the forum.

Before adding the new row in the table you must verify if the checkbox is marked.
I see in your code that the controls are repeated 3 times, and what changes is the number 1, 2 or 3, for example: cboRiskGrouping1, cboRiskGrouping2 and cboRiskGrouping3.
I also recommend you that it is not necessary to pass the value of the control to a variable and then the variable to the cell. You can directly pass the value of the control to the cell:

So considering the above, the code can be simplified like this:
VBA Code:
Private Sub cmdSubmit_Click()
  Dim wsRR As Worksheet, wsRA As Worksheet
  Dim tblRR As ListObject, tblRA As ListObject
  Dim i As Long

  Set wsRR = Sheets("Risk Register")
  Set tblRR = wsRR.ListObjects("Risk_Register")
  Set wsRA = Sheets("Risk Assessment")
  Set tblRA = wsRA.ListObjects("Risk_Assessment")
 
  'Transfer to Risk Register table
  With tblRR.ListRows.Add
    .Range(1) = txtRiskNo.Text
    .Range(2) = cboRiskCat.Text
    .Range(3) = txtRiskStatement.Text
    .Range(4) = txtRiskOwner.Text
    .Range(5) = txtExistControl.Text
    .Range(6) = txtContOwner.Text
    .Range(7) = cboALARP.Text
    .Range(11) = txtComments.Text
  End With
 
  'Transfer to Risk Assessment table 'Risk Assessment1
  For i = 1 To 3
    If Controls("chkEnable" & i).Value = True Then
      With tblRA.ListRows.Add
        .Range(1) = txtRiskNo.Text
        .Range(2) = i
        .Range(3) = Controls("cboRiskGrouping" & i).Text
        .Range(4) = Controls("cboSeverity" & i).Text
        .Range(5) = Controls("cboLikelihood" & i).Text
        .Range(7) = Controls("txtMitigation" & i).Text
        .Range(8) = Controls("txtMitigationOwner" & i).Text
        .Range(9) = Controls("txtTargetDate" & i).Text
        .Range(10) = Controls("cboMSeverity" & i).Text
        .Range(11) = Controls("cboMLikelihood" & i).Text
        .Range(13) = Controls("cboMitStatus" & i).Text
        .Range(14) = Controls("txtComments" & i).Text
      End With
    End If
  Next
  Unload Me
End Sub

--------------

Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
This solved my problem thank you so much!!!!!
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,371
Members
449,097
Latest member
thnirmitha

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