ArmySanta
02-25-2023, 08:15 AM
Hi, I need help with VBA. Please indulge me.
My buttons called crse1, crse2, crse3, crse4 and crse5 are supposed to call their respective cases in the CallHideAndRevealCells() sub. In each of the cases, I set the value of the Range and pass that range into their respective functions depending on the amount of mods it is has. I tried asking chatGPT but its not working.
My intent is for the cells to reveal themselves everytime I click the button and immediately go through the loop and hide all but the first line with an "n" in the cells.
Please help.
Cheers,
ArmySanta
Sub CallHideAndRevealCells() ' Get the name of the button that was clicked
Dim buttonName As String
Dim rng As Range
buttonName = Application.Caller
' Call the appropriate function based on the button name
Select Case buttonName
Case "crse1"
Set rng = Range("C2:C21")
Hide_And_Reveal_Courses_1_Mod rng
Case "crse2"
Set rng = Range("C49:C88")
Hide_And_Reveal_Courses_2_Mods rng
Case "crse3"
Set rng = Range("C194:C253")
Hide_And_Reveal_Courses_3_Mods rng
Case "crse4"
Set rng = Range("C666:C745")
Hide_And_Reveal_Courses_4_Mods rng
Case "crse5"
Set rng = Range("C768:C867")
Hide_And_Reveal_Courses_5_Mods rng
End Select
End Sub
Sub Hide_And_Reveal_Courses_1_Mod(rng As Range)
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModOneRange As Range
Set ModOneRange = rng
rng.EntireRow.Hidden = False
'Loop through the cells in the module
For Each ModOne1 In ModOneRange.Cells
'If the cell is "n", hide the next set of the course
If ModOne1.Value = "n" Then
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells(ModOneRange.Cells.Count)).Cells
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
End Sub
Sub Hide_And_Reveal_Courses_2_Mods(rng As Range)
' Declare variables to hold the different ranges we'll be working with
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModTwo1 As Range
Dim ModTwo2 As Range
Dim ModOneRange As Range
Dim ModTwoRange As Range
' Set ModOneRange to the range passed into the sub
Set ModOneRange = rng
' Set ModTwoRange to be the range directly below ModOneRange
Set ModTwoRange = ModOneRange.Offset(1, 0)
' Unhide all rows in the range passed into the sub
rng.EntireRow.Hidden = False
' Loop through the cells in the first mod (ModOneRange)
For Each ModOne1 In ModOneRange.Cells
' If the value of the cell is "n", we need to hide the next set of rows
If ModOne1.Value = "n" Then
' Loop through the cells in the range starting from the cell two rows below ModOne1
' and going until the end of ModOneRange (the last row in the range passed into the sub)
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells(ModOneRange.Rows.Count, 1)).Cells
' If the value of the cell is "n", hide the entire row
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
' Loop through the cells in the second mod (ModTwoRange)
For Each ModTwo1 In ModTwoRange.Cells
' If the value of the cell is "n", we need to hide the next set of rows
If ModTwo1.Value = "n" Then
' Loop through the cells in the range starting from the cell two rows below ModTwo1
' and going until the last row in ModTwoRange
For Each ModTwo2 In Range(ModTwo1.Offset(2, 0), ModTwoRange.Cells(ModTwoRange.Rows.Count, 1)).Cells
' If the value of the cell is "n", hide the entire row
If ModTwo2.Value = "n" Then ModTwo2.EntireRow.Hidden = True
Next ModTwo2
End If
Next ModTwo1
End Sub
Sub Hide_And_Reveal_Courses_3_Mods(rng As Range)
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModTwo1 As Range
Dim ModTwo2 As Range
Dim ModThree1 As Range
Dim ModThree2 As Range
Dim ModOneRange As Range
Dim ModTwoRange As Range
Dim ModThreeRange As Range
Set ModOneRange = rng
Set ModTwoRange = ModOneRange.Offset(1, 0)
Set ModThreeRange = ModOneRange.Offset(2, 0)
rng.EntireRow.Hidden = False
'Loop through the cells in the first module
For Each ModOne1 In ModOneRange.Cells
'If the cell is "n", hide the next set of the course
If ModOne1.Value = "n" Then
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells.Count).Cells
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
'Loop through the cells in the second module
For Each ModTwo1 In ModTwoRange.Cells
'If the cell is "n", hide the next set of the course
If ModTwo1.Value = "n" Then
For Each ModTwo2 In Range(ModTwo1.Offset(2, 0), ModTwoRange.Cells.Count).Cells
If ModTwo2.Value = "n" Then ModTwo2.EntireRow.Hidden = True
Next ModTwo2
End If
Next ModTwo1
'Loop through the cells in the third module
For Each ModThree1 In ModThreeRange.Cells
'If the cell is "n", hide the next set of the course
If ModThree1.Value = "n" Then
For Each ModThree2 In Range(ModThree1.Offset(2, 0), ModThreeRange.Cells.Count).Cells
If ModThree2.Value = "n" Then ModThree2.EntireRow.Hidden = True
Next ModThree2
End If
Next ModThree1
End Sub
Sub Hide_And_Reveal_Courses_4_Mods(rng As Range)
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModTwo1 As Range
Dim ModTwo2 As Range
Dim ModThree1 As Range
Dim ModThree2 As Range
Dim ModFour1 As Range
Dim ModFour2 As Range
Dim ModOneRange As Range
Dim ModTwoRange As Range
Dim ModThreeRange As Range
Dim ModFourRange As Range
Set ModOneRange = rng
Set ModTwoRange = ModOneRange.Offset(1, 0)
Set ModThreeRange = ModOneRange.Offset(2, 0)
Set ModFourRange = ModOneRange.Offset(3, 0)
rng.EntireRow.Hidden = False
'Loop through the cells in the first mod
For Each ModOne1 In ModOneRange.Cells
'If the cell is "n", hide the next set of the course
If ModOne1.Value = "n" Then
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells(ModOne1.Cells.Count)).Cells
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
'Loop through the cells in the second mod
For Each ModTwo1 In ModTwoRange.Cells
'If the cell is "n", hide the next set of the course
If ModTwo1.Value = "n" Then
For Each ModTwo2 In Range(ModTwo1.Offset(2, 0), ModTwoRange.Cells(ModTwo1.Cells.Count)).Cells
If ModTwo2.Value = "n" Then ModTwo2.EntireRow.Hidden = True
Next ModTwo2
End If
Next ModTwo1
'Loop through the cells in the third mod
For Each ModThree1 In ModThreeRange.Cells
'If the cell is "n", hide the next set of the course
If ModThree1.Value = "n" Then
For Each ModThree2 In Range(ModThree1.Offset(2, 0), ModThreeRange.Cells(ModThree1.Cells.Count)).Cells
If ModThree2.Value = "n" Then ModThree2.EntireRow.Hidden = True
Next ModThree2
End If
Next ModThree1
'Loop through the cells in the fourth mod
For Each ModFour1 In ModFourRange.Cells
'If the cell is "n", hide the next set of the course
If ModFour1.Value = "n" Then
For Each ModFour2 In Range(ModFour1.Offset(2, 0), ModFourRange.Cells(ModFour1.Cells.Count)).Cells
If ModFour2.Value = "n" Then ModFour2.EntireRow.Hidden = True
Next ModFour2
End If
Next ModFour1
End Sub
Sub Hide_And_Reveal_Courses_5_Mods(rng As Range)
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModTwo1 As Range
Dim ModTwo2 As Range
Dim ModThree1 As Range
Dim ModThree2 As Range
Dim ModFour1 As Range
Dim ModFour2 As Range
Dim ModFive1 As Range
Dim ModFive2 As Range
Dim ModOneRange As Range
Dim ModTwoRange As Range
Dim ModThreeRange As Range
Dim ModFourRange As Range
Dim ModFiveRange As Range
Set ModOneRange = rng
Set ModTwoRange = ModOneRange.Offset(1, 0)
Set ModThreeRange = ModTwoRange.Offset(1, 0)
Set ModFourRange = ModThreeRange.Offset(1, 0)
Set ModFiveRange = ModFourRange.Offset(1, 0)
rng.EntireRow.Hidden = False
'Loop through the cells in the first mod
For Each ModOne1 In ModOneRange.Cells
'If the cell is "n", hide the next set of the course
If ModOne1.Value = "n" Then
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells(ModOne1.Cells.Count)).Cells
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
'Loop through the cells in the second mod
For Each ModTwo1 In ModTwoRange.Cells
'If the cell is "n", hide the next set of the course
If ModTwo1.Value = "n" Then
For Each ModTwo2 In Range(ModTwo1.Offset(2, 0), ModTwoRange.Cells(ModTwo1.Cells.Count)).Cells
If ModTwo2.Value = "n" Then ModTwo2.EntireRow.Hidden = True
Next ModTwo2
End If
Next ModTwo1
'Loop through the cells in the third mod
For Each ModThree1 In ModThreeRange.Cells
'If the cell is "n", hide the next set of the course
If ModThree1.Value = "n" Then
For Each ModThree2 In Range(ModThree1.Offset(2, 0), ModThreeRange.Cells(ModThree1.Cells.Count)).Cells
If ModThree2.Value = "n" Then ModThree2.EntireRow.Hidden = True
Next ModThree2
End If
Next ModThree1
'Loop through the cells in the fourth mod
For Each ModFour1 In ModFourRange.Cells
'If the cell is "n", hide the next set of the course
If ModFour1.Value = "n" Then
For Each ModFour2 In Range(ModFour1.Offset(2, 0), ModFourRange.Cells(ModFour1.Cells.Count)).Cells
If ModFour2.Value = "n" Then ModFour2.EntireRow.Hidden = True
Next ModFour2
End If
Next ModFour1
'Loop through the cells in the fifth mod
For Each ModFive1 In ModFiveRange.Cells
'If the cell is "n", hide the next set of the course
If ModFive1.Value = "n" Then
For Each ModFive2 In Range(ModFive1.Offset(2, 0), ModFiveRange.Cells(ModFive1.Cells.Count)).Cells
If ModFive2.Value = "n" Then ModFive2.EntireRow.Hidden = True
Next ModFive2
End If
Next ModFive1
End Sub
My buttons called crse1, crse2, crse3, crse4 and crse5 are supposed to call their respective cases in the CallHideAndRevealCells() sub. In each of the cases, I set the value of the Range and pass that range into their respective functions depending on the amount of mods it is has. I tried asking chatGPT but its not working.
My intent is for the cells to reveal themselves everytime I click the button and immediately go through the loop and hide all but the first line with an "n" in the cells.
Please help.
Cheers,
ArmySanta
Sub CallHideAndRevealCells() ' Get the name of the button that was clicked
Dim buttonName As String
Dim rng As Range
buttonName = Application.Caller
' Call the appropriate function based on the button name
Select Case buttonName
Case "crse1"
Set rng = Range("C2:C21")
Hide_And_Reveal_Courses_1_Mod rng
Case "crse2"
Set rng = Range("C49:C88")
Hide_And_Reveal_Courses_2_Mods rng
Case "crse3"
Set rng = Range("C194:C253")
Hide_And_Reveal_Courses_3_Mods rng
Case "crse4"
Set rng = Range("C666:C745")
Hide_And_Reveal_Courses_4_Mods rng
Case "crse5"
Set rng = Range("C768:C867")
Hide_And_Reveal_Courses_5_Mods rng
End Select
End Sub
Sub Hide_And_Reveal_Courses_1_Mod(rng As Range)
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModOneRange As Range
Set ModOneRange = rng
rng.EntireRow.Hidden = False
'Loop through the cells in the module
For Each ModOne1 In ModOneRange.Cells
'If the cell is "n", hide the next set of the course
If ModOne1.Value = "n" Then
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells(ModOneRange.Cells.Count)).Cells
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
End Sub
Sub Hide_And_Reveal_Courses_2_Mods(rng As Range)
' Declare variables to hold the different ranges we'll be working with
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModTwo1 As Range
Dim ModTwo2 As Range
Dim ModOneRange As Range
Dim ModTwoRange As Range
' Set ModOneRange to the range passed into the sub
Set ModOneRange = rng
' Set ModTwoRange to be the range directly below ModOneRange
Set ModTwoRange = ModOneRange.Offset(1, 0)
' Unhide all rows in the range passed into the sub
rng.EntireRow.Hidden = False
' Loop through the cells in the first mod (ModOneRange)
For Each ModOne1 In ModOneRange.Cells
' If the value of the cell is "n", we need to hide the next set of rows
If ModOne1.Value = "n" Then
' Loop through the cells in the range starting from the cell two rows below ModOne1
' and going until the end of ModOneRange (the last row in the range passed into the sub)
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells(ModOneRange.Rows.Count, 1)).Cells
' If the value of the cell is "n", hide the entire row
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
' Loop through the cells in the second mod (ModTwoRange)
For Each ModTwo1 In ModTwoRange.Cells
' If the value of the cell is "n", we need to hide the next set of rows
If ModTwo1.Value = "n" Then
' Loop through the cells in the range starting from the cell two rows below ModTwo1
' and going until the last row in ModTwoRange
For Each ModTwo2 In Range(ModTwo1.Offset(2, 0), ModTwoRange.Cells(ModTwoRange.Rows.Count, 1)).Cells
' If the value of the cell is "n", hide the entire row
If ModTwo2.Value = "n" Then ModTwo2.EntireRow.Hidden = True
Next ModTwo2
End If
Next ModTwo1
End Sub
Sub Hide_And_Reveal_Courses_3_Mods(rng As Range)
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModTwo1 As Range
Dim ModTwo2 As Range
Dim ModThree1 As Range
Dim ModThree2 As Range
Dim ModOneRange As Range
Dim ModTwoRange As Range
Dim ModThreeRange As Range
Set ModOneRange = rng
Set ModTwoRange = ModOneRange.Offset(1, 0)
Set ModThreeRange = ModOneRange.Offset(2, 0)
rng.EntireRow.Hidden = False
'Loop through the cells in the first module
For Each ModOne1 In ModOneRange.Cells
'If the cell is "n", hide the next set of the course
If ModOne1.Value = "n" Then
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells.Count).Cells
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
'Loop through the cells in the second module
For Each ModTwo1 In ModTwoRange.Cells
'If the cell is "n", hide the next set of the course
If ModTwo1.Value = "n" Then
For Each ModTwo2 In Range(ModTwo1.Offset(2, 0), ModTwoRange.Cells.Count).Cells
If ModTwo2.Value = "n" Then ModTwo2.EntireRow.Hidden = True
Next ModTwo2
End If
Next ModTwo1
'Loop through the cells in the third module
For Each ModThree1 In ModThreeRange.Cells
'If the cell is "n", hide the next set of the course
If ModThree1.Value = "n" Then
For Each ModThree2 In Range(ModThree1.Offset(2, 0), ModThreeRange.Cells.Count).Cells
If ModThree2.Value = "n" Then ModThree2.EntireRow.Hidden = True
Next ModThree2
End If
Next ModThree1
End Sub
Sub Hide_And_Reveal_Courses_4_Mods(rng As Range)
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModTwo1 As Range
Dim ModTwo2 As Range
Dim ModThree1 As Range
Dim ModThree2 As Range
Dim ModFour1 As Range
Dim ModFour2 As Range
Dim ModOneRange As Range
Dim ModTwoRange As Range
Dim ModThreeRange As Range
Dim ModFourRange As Range
Set ModOneRange = rng
Set ModTwoRange = ModOneRange.Offset(1, 0)
Set ModThreeRange = ModOneRange.Offset(2, 0)
Set ModFourRange = ModOneRange.Offset(3, 0)
rng.EntireRow.Hidden = False
'Loop through the cells in the first mod
For Each ModOne1 In ModOneRange.Cells
'If the cell is "n", hide the next set of the course
If ModOne1.Value = "n" Then
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells(ModOne1.Cells.Count)).Cells
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
'Loop through the cells in the second mod
For Each ModTwo1 In ModTwoRange.Cells
'If the cell is "n", hide the next set of the course
If ModTwo1.Value = "n" Then
For Each ModTwo2 In Range(ModTwo1.Offset(2, 0), ModTwoRange.Cells(ModTwo1.Cells.Count)).Cells
If ModTwo2.Value = "n" Then ModTwo2.EntireRow.Hidden = True
Next ModTwo2
End If
Next ModTwo1
'Loop through the cells in the third mod
For Each ModThree1 In ModThreeRange.Cells
'If the cell is "n", hide the next set of the course
If ModThree1.Value = "n" Then
For Each ModThree2 In Range(ModThree1.Offset(2, 0), ModThreeRange.Cells(ModThree1.Cells.Count)).Cells
If ModThree2.Value = "n" Then ModThree2.EntireRow.Hidden = True
Next ModThree2
End If
Next ModThree1
'Loop through the cells in the fourth mod
For Each ModFour1 In ModFourRange.Cells
'If the cell is "n", hide the next set of the course
If ModFour1.Value = "n" Then
For Each ModFour2 In Range(ModFour1.Offset(2, 0), ModFourRange.Cells(ModFour1.Cells.Count)).Cells
If ModFour2.Value = "n" Then ModFour2.EntireRow.Hidden = True
Next ModFour2
End If
Next ModFour1
End Sub
Sub Hide_And_Reveal_Courses_5_Mods(rng As Range)
Dim ModOne1 As Range
Dim ModOne2 As Range
Dim ModTwo1 As Range
Dim ModTwo2 As Range
Dim ModThree1 As Range
Dim ModThree2 As Range
Dim ModFour1 As Range
Dim ModFour2 As Range
Dim ModFive1 As Range
Dim ModFive2 As Range
Dim ModOneRange As Range
Dim ModTwoRange As Range
Dim ModThreeRange As Range
Dim ModFourRange As Range
Dim ModFiveRange As Range
Set ModOneRange = rng
Set ModTwoRange = ModOneRange.Offset(1, 0)
Set ModThreeRange = ModTwoRange.Offset(1, 0)
Set ModFourRange = ModThreeRange.Offset(1, 0)
Set ModFiveRange = ModFourRange.Offset(1, 0)
rng.EntireRow.Hidden = False
'Loop through the cells in the first mod
For Each ModOne1 In ModOneRange.Cells
'If the cell is "n", hide the next set of the course
If ModOne1.Value = "n" Then
For Each ModOne2 In Range(ModOne1.Offset(2, 0), ModOneRange.Cells(ModOne1.Cells.Count)).Cells
If ModOne2.Value = "n" Then ModOne2.EntireRow.Hidden = True
Next ModOne2
End If
Next ModOne1
'Loop through the cells in the second mod
For Each ModTwo1 In ModTwoRange.Cells
'If the cell is "n", hide the next set of the course
If ModTwo1.Value = "n" Then
For Each ModTwo2 In Range(ModTwo1.Offset(2, 0), ModTwoRange.Cells(ModTwo1.Cells.Count)).Cells
If ModTwo2.Value = "n" Then ModTwo2.EntireRow.Hidden = True
Next ModTwo2
End If
Next ModTwo1
'Loop through the cells in the third mod
For Each ModThree1 In ModThreeRange.Cells
'If the cell is "n", hide the next set of the course
If ModThree1.Value = "n" Then
For Each ModThree2 In Range(ModThree1.Offset(2, 0), ModThreeRange.Cells(ModThree1.Cells.Count)).Cells
If ModThree2.Value = "n" Then ModThree2.EntireRow.Hidden = True
Next ModThree2
End If
Next ModThree1
'Loop through the cells in the fourth mod
For Each ModFour1 In ModFourRange.Cells
'If the cell is "n", hide the next set of the course
If ModFour1.Value = "n" Then
For Each ModFour2 In Range(ModFour1.Offset(2, 0), ModFourRange.Cells(ModFour1.Cells.Count)).Cells
If ModFour2.Value = "n" Then ModFour2.EntireRow.Hidden = True
Next ModFour2
End If
Next ModFour1
'Loop through the cells in the fifth mod
For Each ModFive1 In ModFiveRange.Cells
'If the cell is "n", hide the next set of the course
If ModFive1.Value = "n" Then
For Each ModFive2 In Range(ModFive1.Offset(2, 0), ModFiveRange.Cells(ModFive1.Cells.Count)).Cells
If ModFive2.Value = "n" Then ModFive2.EntireRow.Hidden = True
Next ModFive2
End If
Next ModFive1
End Sub