+ Reply to Thread
Results 1 to 6 of 6

End if without Block If

Hybrid View

  1. #1
    Registered User
    Join Date
    08-17-2023
    Location
    Stevenage, England
    MS-Off Ver
    365
    Posts
    7

    End if without Block If

    Hi All,

    I'm really new to this and have been trying to code. I'm trying to select a data range and copy it into a file save both files, clear the contents of the original selection and save and close that workbook too.

    The below did work:
    Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    If Workbooks("Support Workload Tracker").Worksheets("Data Capture").Range("B2") <> "" Then
    Dim AnswerYes As String
    Dim AnswerNo As String
    AnswerYes = MsgBox("Are you finishing for the day?", vbQuestion + vbYesNo, "End Of Day")
    If AnswerYes = vbNo Then
    MsgBox "Action Cancelled"
    Cancel = True
    
    Else
    
    Workbooks("Support Workload Tracker").Activate
    With Worksheets("Data Capture")
        LastRow = .Cells(Rows.Count, "N").End(xlUp).Row
        .Range("A2:N" & LastRow).Copy
    End With
    Dim wb As Workbook
    Filespec = "xxxxxxx(webaddress)xxxxxxx"
    Set wb = Application.Workbooks.Open(Filename:=Filespec)
    Workbooks("MASTER Support Workload Tracker").Worksheets("Data").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    ActiveCell.PasteSpecial (xlPasteValuesAndNumberFormats)
    For Each wb In Workbooks
    wb.Save
    Next wb
    Workbooks("MASTER Support Workload Tracker").Close
    End If
    End If
    Application.ScreenUpdating = True
    End Sub
    But when I added this after the "Workbooks("MASTER Support Workload Tracker").Close"
    Worksheets("Historical").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    ActiveCell.PasteSpecial (xlPasteValuesAndNumberFormats)
    Workbooks("Support Workload Tracker").Activate
    With Worksheets("Data Capture")
        LastRow = .Cells(Rows.Count, "N").End(xlUp).Row
        .Range("A2:N" & LastRow).ClearContents
    It stopped working. Can anyone tell me why? And how to fix it.

    For full clarity this is how the code looks currently and isn't working
    Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    If Workbooks("Support Workload Tracker").Worksheets("Data Capture").Range("B2") <> "" Then
    Dim AnswerYes As String
    Dim AnswerNo As String
    AnswerYes = MsgBox("Are you finishing for the day?", vbQuestion + vbYesNo, "End Of Day")
    If AnswerYes = vbNo Then
    MsgBox "Action Cancelled"
    Cancel = True
    
    Else
    
    Workbooks("Support Workload Tracker").Activate
    With Worksheets("Data Capture")
        LastRow = .Cells(Rows.Count, "N").End(xlUp).Row
        .Range("A2:N" & LastRow).Copy
    End With
    Dim wb As Workbook
    Filespec = "xxxxxxxxxx(webaddress)xxxxxxxxx"
    Set wb = Application.Workbooks.Open(Filename:=Filespec)
    Workbooks("MASTER Support Workload Tracker").Worksheets("Data").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    ActiveCell.PasteSpecial (xlPasteValuesAndNumberFormats)
    For Each wb In Workbooks
    wb.Save
    Next wb
    Workbooks("MASTER Support Workload Tracker").Close
    Worksheets("Historical").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    ActiveCell.PasteSpecial (xlPasteValuesAndNumberFormats)
    Workbooks("Support Workload Tracker").Activate
    With Worksheets("Data Capture")
        LastRow = .Cells(Rows.Count, "N").End(xlUp).Row
        .Range("A2:N" & LastRow).ClearContents
    End If
    End If
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,047

    Re: End if without Block If

    Try

    '.....
    With Worksheets("Data Capture")
        LastRow = .Cells(Rows.Count, "N").End(xlUp).Row
        .Range("A2:N" & LastRow).ClearContents
    End With
    End If
    End If
    Application.ScreenUpdating = True
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: End if without Block If

    You will find it MUCH easier to read and find these sorts of problems if you indent lines properly, such as this:
    Sub CommandButton1_Click()
        
        Application.ScreenUpdating = False
        
        If Workbooks("Support Workload Tracker").Worksheets("Data Capture").Range("B2") <> "" Then
        
            Dim AnswerYes As String
            Dim AnswerNo As String
            AnswerYes = MsgBox("Are you finishing for the day?", vbQuestion + vbYesNo, "End Of Day")
            If AnswerYes = vbNo Then
                MsgBox "Action Cancelled"
                Cancel = True
            Else
                Workbooks("Support Workload Tracker").Activate
                With Worksheets("Data Capture")
                    LastRow = .Cells(Rows.Count, "N").End(xlUp).Row
                    .Range("A2:N" & LastRow).Copy
                End With
                
                Dim wb As Workbook
                Filespec = "xxxxxxxxxx(webaddress)xxxxxxxxx"
                Set wb = Application.Workbooks.Open(Filename:=Filespec)
                Workbooks("MASTER Support Workload Tracker").Worksheets("Data").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
                ActiveCell.PasteSpecial (xlPasteValuesAndNumberFormats)
                For Each wb In Workbooks
                    wb.Save
                Next wb
                
                Workbooks("MASTER Support Workload Tracker").Close
                Worksheets("Historical").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
                ActiveCell.PasteSpecial (xlPasteValuesAndNumberFormats)
                
                Workbooks("Support Workload Tracker").Activate
                With Worksheets("Data Capture")
                    LastRow = .Cells(Rows.Count, "N").End(xlUp).Row
                    .Range("A2:N" & LastRow).ClearContents
                End With    'this is the one you missed.
            End If
        End If
        
        Application.ScreenUpdating = True
        
    End Sub
    I would also suggest you introduce some naming discipline to help read through your code by using 3-letter prefixes to identify the data type, such as
    Dim strAnswerNo as String, strAnswerYes as String
    Dim varExample as Variant
    Dim dblAmount as Double
    Dim rngData as Range
    ... and so on.

    There are exceptions such as ws and wb (or wks and wkb) and rng and cel (e.g. For each cel in rng) as these are commonly used.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  4. #4
    Registered User
    Join Date
    08-17-2023
    Location
    Stevenage, England
    MS-Off Ver
    365
    Posts
    7

    Re: End if without Block If

    Amazing, thank you so much for your help.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    81,640

    Re: End if without Block If

    If that takes care of your original question, please choose Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, if you have not already done so, you may not be aware that you can thank anyone who offered you help towards a solution for your issue by clicking the small star icon (* Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who offered help.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  6. #6
    Registered User
    Join Date
    08-17-2023
    Location
    Stevenage, England
    MS-Off Ver
    365
    Posts
    7

    Re: End if without Block If

    So this has worked for the most part but now I am receiving an object defined error. Can anyone help?

    Application.ScreenUpdating = False
    If Workbooks("Support Workload Tracker").Worksheets("Data Capture").Range("B2") <> "" Then
        Dim AnswerYes As String
        Dim AnswerNo As String
        AnswerYes = MsgBox("Are you finishing for the day?", vbQuestion + vbYesNo, "End Of Day")
        If AnswerYes = vbNo Then
            MsgBox "Action Cancelled"
            Cancel = True
        Else
            Workbooks("Support Workload Tracker").Activate
            With Worksheets("Data Capture")
                LastRow = .Cells(Rows.Count, "N").End(xlUp).Row
                .Range("A2:N" & LastRow).Copy
            End With
            
            Dim wb As Workbook
            Filespec = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            Set wb = Application.Workbooks.Open(Filename:=Filespec)
            Workbooks("MASTER Support Workload Tracker").Worksheets("Data").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            ActiveCell.PasteSpecial (xlPasteValuesAndNumberFormats)
            For Each wb In Workbooks
                wb.Save
            Next wb
            Workbooks("MASTER Support Workload Tracker").Close
            Worksheets("Historical").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            ActiveCell.PasteSpecial (xlPasteValuesAndNumberFormats)
            
            Workbooks("Support Workload Tracker").Activate
            With Worksheets("Data Capture")
                LastRow = .Cells(Rows.Count, "N").End(xlUp).Row
                .Range("A2:N" & LastRow).ClearContents
            End With
        End If
    End If
    Application.ScreenUpdating = True

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Conditional formatting of cells of data block based on cell value in data block?
    By AleXSR700 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-25-2020, 02:03 AM
  2. [SOLVED] Macro needed for AutoScroll Down - Block up Block (TV Display team by team) - Help Plz
    By NametobeRenamed in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-01-2019, 12:03 AM
  3. Help with VBA. Block If without End If
    By Lasternz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-11-2016, 09:51 PM
  4. New kid on the block
    By troyn in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 09-18-2015, 10:26 AM
  5. New boy on the block
    By Firepoint in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 06-04-2014, 12:51 PM
  6. Replies: 5
    Last Post: 10-09-2012, 10:44 AM
  7. Replies: 2
    Last Post: 06-22-2010, 07:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1