+ Reply to Thread
Results 1 to 17 of 17

apply error handeling if user selects wrong file based on pre-defined sheets

  1. #1
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    apply error handeling if user selects wrong file based on pre-defined sheets

    I have VBA code that prompts a user to select the workbook from a location. I want to add error handling so that if they choose an incorrect file based on the worksheets then apply a message box "wrong file" and re-enable the prompt to choose a new file.

    As you can see below I've already set what the existing names of the worksheets should be if they select the correct workbook.


    Sub Button4_Click()

    Application.ScreenUpdating = False

    Application.EnableEvents = False

    Dim strFileName As String
    Dim wb1 As Workbook
    Dim ws1 As Worksheet
    Dim ws1A As Worksheet
    Dim wb2 As Workbook
    Dim ws2 As Worksheet
    Dim cell As Range
    Dim rng As Range
    Dim rng2 As Range
    Dim RangeName As String
    Dim CellName As String
    Dim ValueToFind
    Dim dstRng As Range
    Dim NewFile As Variant


    'prompt folder location
    NewFile = Application.GetOpenFilename("microsoft excel files (*.xlsm*), *.xlsm*")

    If NewFile <> False Then
    Set wb1 = Workbooks.Open(NewFile)
    'apply error handeling if user does not select a file
    Else
    MsgBox "No/Wrong File Selected. Program Will Exit."
    Application.DisplayAlerts = False
    Exit Sub
    End If


    Set wb2 = ThisWorkbook
    Set ws2 = wb2.Sheets("Output")
    Set ws1 = wb1.Sheets("RVP Local GAAP")
    Set rng = Range("CurrentTaxPerLocalGAAPProvision")
    Set rng2 = Range("CurrentTaxPerGroupGAAPProvision")
    Set ws1A = wb1.Sheets("RVP Group GAAP")


    'Loop through all the values in NamedRange
    For Each rng In ws2.Range("NamedRange")
    Set dstRng = Nothing
    On Error Resume Next
    Set dstRng = ws1.Range(rng.Value)
    On Error GoTo 0
    'Check that the range exists in destination sheet
    If Not dstRng Is Nothing Then
    'Check that the range exists in the appropriate area
    If Not Intersect(dstRng, ws1.Range("CurrentTaxPerLocalGAAPProvision")) Is Nothing Then
    'Transfer the value from the next column to the appropriate range in the
    'destination sheet
    dstRng.Value = rng.Offset(0, 1).Value
    Else
    End If
    End If
    Next
    For Each rng2 In ws2.Range("NamedRange")
    Set dstRng = Nothing
    On Error Resume Next
    Set dstRng = ws1A.Range(rng2.Value)
    On Error GoTo 0
    'Check that the range exists in destination sheet
    If Not dstRng Is Nothing Then
    'Check that the range exists in the appropriate area
    If Not Intersect(dstRng, ws1A.Range("CurrentTaxPerGroupGAAPProvision")) Is Nothing Then
    ''MsgBox "succesful"
    ''found = False
    'Transfer the value from the next column to the appropriate range in the
    'destination sheet
    dstRng.Value = rng2.Offset(0, 1).Value
    Else
    End If
    End If
    Next
    MsgBox "Values have copied over sucessfully"

    ActiveWindow.FreezePanes = False
    Application.ScreenUpdating = True
    Application.EnableEvents = True

    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    thanks much.. worked perfectly...can I ask another question related to error handeling in the same code above...
    I'm having trouble with this line of code --> ElseIf rng2.Value <> dstRng Then MsgBox rng2.Value & " not in RVP Group GAAP sheet" is not being recognized and I made sure to set values not equal to each other. Basically I am trying to apply error handling to my if statement below. It works perfectly when the two ranges match just not when values do not match...

    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    You're welcome.

    Not sure I follow what's going on. It looks like rng2.Value is the "Name" for a range that is set to the variable dstRng. If that's correct, then this; ElseIf rng2.Value <> dstRng Then, is comparing the Range Name to the Range Value. I think you just want an Else statement (not ElseIf) as you've previously tested if the dstRng is in the RVP group. You may want that that Else statement as part of the first IF; If Not dstRng Is Nothing Then.
    Last edited by AlphaFrog; 01-17-2018 at 01:11 PM.

  5. #5
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    Got it okay so I added the else statement --> Else: MsgBox rng.Value & " not in RVP Local GAAP sheet"
    and it did work and returned a message box for the value that was missing in the destination sheet, however it didn't copy over the successful values? and then it gives me an error message "Invalid procedure call or argument" on this line --> If Not Intersect(dstRng, checkRange) Is Nothing Then

    Please Login or Register  to view this content.
    Next

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 01-17-2018 at 02:13 PM.

  7. #7
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    For some reason the above just repetitively shows both message boxes for all values even the successful ones and then I have to force quit to stop the message boxes... heres my code:

    Please Login or Register  to view this content.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  9. #9
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    great thanks so much - the template file is the one to choose when you get the prompt while the code is in workbook 2.
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    Your named range ws2.Range("NamedRange") is defined as all cells in column D. So your code was looping through the whole column including all the empty cells. That's why there was a continuous loop of no matches.

    The code below defines the named range from D4 to the last used cell in column D.

    There are only four named ranges on each sheet in the template. So you will still have several messages that there is no named range match. I don't know if that's what you want or not.


    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    thanks for pointing this out, it does help. That is fine that there is only 4 named ranges on each sheet with a match... however the message box still appears for even those named ranges that do have a match and it titles it "no named range." Also right now it doesn't even copy over the successful values, it just shows me the message box.

  12. #12
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    Using your example workbooks, it matched one range on each sheet and copied that value over. If you think there should be more matches, check that the values in column D are the exact names of the Named ranges in template workbook (case sensitive)

    I don't understand what you want the message box to do or not do.

  13. #13
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    I only want the message box to show when there is no match between the namedrange column and the destination sheet. if there is a no match then just let the user know that there is no match via a message box. For some reason the no match is showing for every single named range i have in the namedrange column.

  14. #14
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    I only want the message box to show when there is no match between the namedrange column and the destination sheet. if there is a no match then just let the user know that there is no match via a message box.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    ah yes looks like that works.. thanks for all your help and effort on this!!! It is much appreciated

  16. #16
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    per your suggestion writing this in the main thread- using the same workbooks

    Basically right now it prompts the user to select the template and performs the code perfectly, instead I want it to prompt the user to choose a folder the folder will be called “templates” and it will contain all the templates that I want it to perform the code on and then save them into an output folder called “output”

    The code works by looking to see if column (“NamedRange”) exists as a named range in the template and then returns the report balance if it does exist.
    However I want to add logic where for each template in the folder it will check the “Ted ID” in a table in ws "output - flat" and only bring back the values associated with that Ted ID.
    So in the table there are two different Ted ID’s so there will be two templates outputs

    The table is in “output - flat” tab in “workboopk2” along with a “select all” checkbox. I want the code to only loop through all the workbooks and perform the above when the user clicks “ select all” and then “Send values” button.

    Let me know if this makes sense and thanks so much...

  17. #17
    Registered User
    Join Date
    03-07-2016
    Location
    USA
    MS-Off Ver
    ms office 2013
    Posts
    25

    Re: apply error handeling if user selects wrong file based on pre-defined sheets

    The code is in Module 1 of “workbook2” and the template to choose from the folder is attached. both are attached.

    Basically right now it prompts the user to select the template and performs the code perfectly, instead I want it to prompt the user to choose a folder the folder will be called “templates” and it will contain all the templates then I want it to perform the code on and then save them into an output folder called “output”

    The code works by looking to see if column (“NamedRange”) exists as a named range in the template and then returns the report balance if it does exist.
    However I want to add logic where for each template in the folder it will check the “Ted ID” in a table in ws "output - flat" and only bring back the values associated with that Ted ID.
    So in the table there are two different Ted ID’s so there will be two templates outputs

    The table is in “output - flat” tab in “workboopk2” along with a “select all” checkbox. I want the code to only loop through all the workbooks and perform the above when the user clicks “ select all” and then “Send values” button.

+ 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. If user selects a range using two comboboxes, apply value across range
    By Groovicles in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-24-2016, 02:08 PM
  2. Copy, paste, replace- user defined range in all the defined sheets
    By aganesan99 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2014, 12:28 PM
  3. Date-based VBA macro selects wrong cell.
    By Dralky in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-12-2013, 06:38 PM
  4. Replies: 2
    Last Post: 09-12-2012, 01:01 PM
  5. [SOLVED] User Selects File- That file is copied to a sheet
    By FancyCorndog in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-09-2012, 05:31 PM
  6. Excel 2007 error when adding custom help file to user defined function
    By sabotuer99 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-04-2009, 01:10 AM
  7. [SOLVED] User Defined Functions: Can anyone tell me where I'm going wrong?
    By Mike McLellan in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-04-2006, 06:00 AM

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