K
Ken Hudson
In a larger section of code I am trying to use a variable to reference a
check box. I have listed code samples below to attempt to explain my
question. Assume that I have six columns of data. In row one I have dates. In
row two I have check boxes. I want to loop through the columns, comparing two
sets of dates at a time. Based on that comparison, I want to hit a check box
in one of the two columns below those dates. The first code section does what
I want but is not efficient.
Is there a way to code the change in the second section to somehow use the
lColCount variable to identify the check box to be hit?
Option Explicit
Dim lColCount As Long
Sub PopCheckBoxes()
For lColCount = 1 To 6 Step 2
If Cells(1, lColCount) > Cells(1, lColCount + 1) Then
If lColCount = 1 Then
ActiveSheet.CheckBoxes("Check box 1").Value = xlOn
Else
ActiveSheet.CheckBoxes("Check box 2").Value = xlOn
End If
If lColCount = 3 Then
ActiveSheet.CheckBoxes("Check box 3").Value = xlOn
Else
ActiveSheet.CheckBoxes("Check box 4").Value = xlOn
End If
If lColCount = 5 Then
ActiveSheet.CheckBoxes("Check box 5").Value = xlOn
Else
ActiveSheet.CheckBoxes("Check box 6").Value = xlOn
End If
End If
Next lColCount
End Sub
----------------------------------------------------------------------
Sub PopCheckBoxes()
For lColCount = 1 To 6 Step 2
If Cells(1, lColCount) > Cells(1, lColCount + 1) Then
'(Can I use lColCount to reference this check box) = xlOn
Else
'(Can I use lColCount to reference this check box) = xlOn
End If
Next lColCount
End Sub
check box. I have listed code samples below to attempt to explain my
question. Assume that I have six columns of data. In row one I have dates. In
row two I have check boxes. I want to loop through the columns, comparing two
sets of dates at a time. Based on that comparison, I want to hit a check box
in one of the two columns below those dates. The first code section does what
I want but is not efficient.
Is there a way to code the change in the second section to somehow use the
lColCount variable to identify the check box to be hit?
Option Explicit
Dim lColCount As Long
Sub PopCheckBoxes()
For lColCount = 1 To 6 Step 2
If Cells(1, lColCount) > Cells(1, lColCount + 1) Then
If lColCount = 1 Then
ActiveSheet.CheckBoxes("Check box 1").Value = xlOn
Else
ActiveSheet.CheckBoxes("Check box 2").Value = xlOn
End If
If lColCount = 3 Then
ActiveSheet.CheckBoxes("Check box 3").Value = xlOn
Else
ActiveSheet.CheckBoxes("Check box 4").Value = xlOn
End If
If lColCount = 5 Then
ActiveSheet.CheckBoxes("Check box 5").Value = xlOn
Else
ActiveSheet.CheckBoxes("Check box 6").Value = xlOn
End If
End If
Next lColCount
End Sub
----------------------------------------------------------------------
Sub PopCheckBoxes()
For lColCount = 1 To 6 Step 2
If Cells(1, lColCount) > Cells(1, lColCount + 1) Then
'(Can I use lColCount to reference this check box) = xlOn
Else
'(Can I use lColCount to reference this check box) = xlOn
End If
Next lColCount
End Sub