If and ELseIF

imback2nite

Board Regular
Joined
Oct 30, 2004
Messages
211
Office Version
  1. 2003 or older
Platform
  1. Windows
I've written this and don't know what is going wrong.
Rich (BB code):
Private Sub CommandButton3_Click()
        If Range("3:2544").EntireRow.Hidden = False Then
        MsgBox ("Select Which Week To Update Names")
            Exit Sub
       ElseIf Range("4:6").EntireRow.Hidden = False Then
            Range("B8:B47").ClearContents
            Worksheets("Names").Range("B2:B41").Copy
            Range("B8").PasteSpecial Paste:=xlPasteValues
        ElseIf Range("51:54").EntireRow.Hidden = False Then
            Range("B56:B95").ClearContents
            Worksheets("Names").Range("B2:B41").Copy
            Range("B56").PasteSpecial Paste:=xlPasteValues
       ElseIf Range("99:102").EntireRow.Hidden = False Then
            Range("B104:B143").ClearContents
            Worksheets("Names").Range("B2:B41").Copy
            Range("B104").PasteSpecial Paste:=xlPasteValues
       ElseIf Range("147:150").EntireRow.Hidden = False Then
            Range("B152:B191").ClearContents
            Worksheets("Names").Range("B2:B41").Copy
            Range("B152").PasteSpecial Paste:=xlPasteValues
        End If
            Application.CutCopyMode = False
        Range("B1").Select
    Application.ScreenUpdating = True
End Sub
It all works except for the
Rich (BB code):
       ElseIf Range("4:6").EntireRow.Hidden = False Then
            Range("B8:B47").ClearContents
            Worksheets("Names").Range("B2:B41").Copy
            Range("B8").PasteSpecial Paste:=xlPasteValues
The rest of the ElseIf's work fine. I tried deleting the code below and it works fine but I do need it. Any help wound be appreciated.
Rich (BB code):
 If Range("3:2544").EntireRow.Hidden = False Then
        MsgBox ("Select Which Week To Update Names")
            Exit Sub
[/CODE] Because the code works fine without it.
 
What hides or unhides each section ? Show us the code and give a short description of the logic.

What are you trying to do with the CommandButton3 code ? Are you just trying to find the unhidden section ?

This should do the same thing as what you are trying to do with your code:

VBA Code:
Private Sub CommandButton3_Click()
    Dim ws As Worksheet
    Dim iHdgRows As Long, iBodyRows As Long
    Dim iStartRow As Long
    Dim LastRow As Long, iSectRows As Long
    Dim VisibleSectFound As Boolean
    Dim i As Long

    Set ws = Worksheets("TOKES")
    LastRow = Range("B" & Rows.Count).End(xlUp).Row + 3

    iStartRow = 3
    iHdgRows = 5
    iBodyRows = 40
    iGapRows = 3
    iSectRows = iHdgRows + iBodyRows + iGapRows
    
    VisibleSectFound = False
 
    For i = iStartRow To LastRow Step iSectRows
        With ws
            If .Cells(i, "B").EntireRow.Hidden = False Then
                .Cells(i, "B").Offset(iHdgRows).Resize(iBodyRows).ClearContents
                Worksheets("Names").Range("B2:B41").Copy
                .Cells(i, "B").Offset(iHdgRows).PasteSpecial Paste:=xlPasteValues
                
                VisibleSectFound = True
                Exit For

            End If
        End With
    Next i
    
    If VisibleSectFound = False Then
        MsgBox ("Select Which Week To Update Names")
        Exit Sub
    End If

End Sub
 
Last edited:
Upvote 0
Solution

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
What hides or unhides each section ? Show us the code and give a short description of the logic.

What are you trying to do with the CommandButton3 code ? Are you just trying to find the unhidden section ?

This should do the same thing as what you are trying to do with your code:

VBA Code:
Private Sub CommandButton3_Click()
    Dim ws As Worksheet
    Dim iHdgRows As Long, iBodyRows As Long
    Dim iStartRow As Long
    Dim LastRow As Long, iSectRows As Long
    Dim VisibleSectFound As Boolean
    Dim i As Long

    Set ws = Worksheets("TOKES")
    LastRow = Range("B" & Rows.Count).End(xlUp).Row + 3

    iStartRow = 3
    iHdgRows = 5
    iBodyRows = 40
    iGapRows = 3
    iSectRows = iHdgRows + iBodyRows + iGapRows
   
    VisibleSectFound = False
 
    For i = iStartRow To LastRow Step iSectRows
        With ws
            If .Cells(i, "B").EntireRow.Hidden = False Then
                .Cells(i, "B").Offset(iHdgRows).Resize(iBodyRows).ClearContents
                Worksheets("Names").Range("B2:B41").Copy
                .Cells(i, "B").Offset(iHdgRows).PasteSpecial Paste:=xlPasteValues
               
                VisibleSectFound = True
                Exit For

            End If
        End With
    Next i
   
    If VisibleSectFound = False Then
        MsgBox ("Select Which Week To Update Names")
        Exit Sub
    End If

End Sub
Wow! Now that is nice! Elegant. I use the 'Dates' button to bring up the 'Month(s)' userform. Then the dated buttons to select the appropriate week. I actually have 52 weeks populating the 'TOKES' page, vertically. The 'Dated' buttons unhide that week while hiding the rest. Then the user can input the pertinent information. Command Button 3 or 'Input Names' will see the 'open area and input the 'Names', which are shown as numbers for illustration. What I have is a 'Show All' button that will show all 52 weeks. But when all 52 weeks are shown I don't want the user to be able to populate the 'TOKES' page till a week is chosen. That is why I needed the MsgBox alerting then user.
Screenshot (21).jpg
Screenshot (20).jpg
 
Upvote 0
Thanks for elaborating. Assuming we have solved the topic of this thread I will leave it there on this one.
 
Upvote 1
Thanks for elaborating. Assuming we have solved the topic of this thread I will leave it there on this one.
Thank you! I really couldn't have done it without your help. I really need to learn the correct way of writing. My way is so juvenile. Thank you once again! :giggle:
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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