Hide Rows based on Combobox Selection
Is this combobox in a userform? How is your variable i evaluated? I will
assume it is a whole number so I declared it as a Long datatype. Make sure
you declare all your variables in the future, like I did. I assumed this
combobox is located in a userform. I modified your code to prevent errors.
If you get an error please indicate what line the error is on and what the
error description is so we can help you. Try this code.
Private Sub ComboBox2XR_Change()
Dim MyString1 As String
Dim Range1 As Range
Dim c As Long
Dim MyString2 As String
Dim Range2 As Range
Dim d As Long
Dim i As Long
MyString1 = "ACTXR2" & i & "_BEGIN"
MyString2 = "ACTXR2" & i & "_END"
With Sheets("ReportRequestXR")
Set Range1 = .Cells.Find(What:=MyString1, _
After:=.Range("A1"), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
Set Range2 = .Cells.Find(What:=MyString2, _
After:=Range("A1"), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If Not Range1 Is Nothing Then
c = Range1.Row
Else
MsgBox "Can't find " & MyString1
Exit Sub
End If
If Not Range2 Is Nothing Then
d = Range2.Row
Else
MsgBox "Can't find " & MyString2
Exit Sub
End If
Select Case ComboBox2XR.Value
Case Is = ""
ComboBox2XR.Enabled = True
ComboBox2XR.Visible = True
Case Is = "Encounter-Level"
ComboBox2XR.Enabled = True
Sheets("ReportRequestXR").Rows(c & ":" & d).Hidden = True
Case Is = "Accession-Level"
ComboBox2XR.Enabled = True
Sheets("ReportRequestXR").Rows(c & ":" & d).Hidden = True
End Select
End Sub
--
Cheers,
Ryan
"Katie" wrote:
I have a spreadsheet that I am automating. I have 15 lines with 15 comboboxes
each are labeled uniquely and based on the selection I need it to hide rows
on multiple sheets.
Here is what I have for one combobox, they are all very similar:
Private Sub ComboBox2XR_Change()
Dim c As String
Dim d As String
c = _
Worksheets("ReportRequestXR").Cells.Find(What:="Ac tXR2" & i &
"_BEGIN", after:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False).Row
d = _
Worksheets("ReportRequestXR").Cells.Find(What:="AC TXR2" & i &
"_END", after:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False).Row
If ComboBox2XR.Value = "" Then
ComboBox2XR.Enabled = True
ComboBox2XR.Visible = True
End If
If ComboBox2XR.Value = "Encounter-Level" Then
ComboBox2XR.Enabled = True
Worksheets("ReportRequestXR").Rows(c & ":" & d).Hidden = xlVeryHidden
ElseIf ComboBox2XR.Value = "Accession-Level" Then
ComboBox2XR.Enabled = True
Worksheets("ReportRequestXR").Rows(c & ":" & d).Visible = True
End If
End Sub
The rows are not hiding as planned, any ideas?
|