+ Reply to Thread
Results 1 to 20 of 20

Caputre WB name on error and continue

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Caputre WB name on error and continue

    Below is the far too complex code I haven't whittled down yet. I have a routine that performs a copy-past from one workbook to a compiled workbook. Sometimes the source data is bad (ie, the source workbook thinks that 9 contiguous cells are non=contiguous BS error) When an error like this occurs, I want to be able to capture the WB name that popped the error then skip that workbook and continue. At the end I would like a Pop up of errored out WBs. Can this be done give what I have?

    Due to the sensitivity of the data, I cannot accurately recreate of share the files. The repetition for Risk A/B/C is due to the risk groups being contiguous in rows, but the same 5 categories...its a mess and i a trying to untagle it.

    Private Sub DoSomething(ByRef wb As Workbook, ByVal CurrentRow As Long)
       
        Dim copyRiskAFrom As Range
        Dim copyRiskATo As Range
        Dim copyRiskBFrom As Range
        Dim copyRiskBTo As Range
        Dim copyRiskCFrom As Range
        Dim copyRiskCTo As Range
        Dim copyBaseDataFrom As Range
        Dim copyBaseDataTo As Range
        Dim wb2 As Workbook
        Dim ancHosp As Range
        Dim lrow As Long
        Dim lcell1 As Long
        Dim lcell2 As Long
        Dim riskA As Range
        Dim lcellA As Long
        Dim lcellA2 As Long
        Dim lrowA As Long
        Dim riskB As Range
        Dim lcellB As Long
        Dim lcellB2 As Long
        Dim lrowB As Long
        Dim riskC As Range
        Dim lcellC As Long
        Dim lcellC2 As Long
        Dim lrowC As Long
        Dim TestWorkbook As Workbook
        Dim wsCheck As Worksheet
        Dim i As Integer
    
    'test to see if the destination workbook is already open
    '    Set TestWorkbook = Nothing
    '    On Error Resume Next
    '    Set TestWorkbook = Workbooks("Targets09.30.14V1 - Copy.xlsx")
    '    On Error GoTo 0
    '
    '    If TestWorkbook Is Nothing Then
    '        MsgBox "Please open the destination file, 'Targets09.30.14V1 - Copy.xlsx'."
    '        Exit Sub
    '    End If
    Workbooks.Open ("H:\Shared\DataGen\DG Group\Bundled Payments\Temporary John Folder\New folder\Targets09.30.14V1 - Copy.xlsx")
    
        Set wb2 = Workbooks("Targets09.30.14V1 - Copy.xlsx")
        Set wsCheck = wb.Worksheets(2)
        i = 2
    
    step1:
    '******************************Risk A***********************************
     '***********************Copy Base Data columns (Clinical Episode, MSDRG and N)
     'set the copy range
        With wsCheck
             Set copyBaseDataFrom = .Range(.Range("A5"), .Range("C5").End(xlDown))
        End With
    
        'set where we are copying to
        Set copyBaseDataTo = wb2.Worksheets(2).Range("C" & Rows.count).End(xlUp).Offset(1, 0)
    
       
        'perform the copy/paste without the borders
        copyBaseDataFrom.Copy
        copyBaseDataTo.PasteSpecial Paste:=xlPasteAllExceptBorders
        Application.CutCopyMode = False
        
    '************************** set copy/Paste Range for Risk A
        'set the copy range
        With wsCheck
             Set copyRiskAFrom = .Range(.Range("D5"), .Range("H5").End(xlDown))
        End With
        
        'set where we are copying to
        Set copyRiskATo = wb2.Worksheets(2).Range("F" & Rows.count).End(xlUp).Offset(1, 0)
         
        'perform the copy/paste without the borders
        copyRiskAFrom.Copy
        copyRiskATo.PasteSpecial Paste:=xlPasteAllExceptBorders
        Application.CutCopyMode = False
        
    '************************************** Risk A Definition
         'capture the Risk Definition
        With wsCheck
            Set riskA = .Range("D3")
        End With
        
        'determine the last full cell in Column K and start in the next one down
        With wb2.Worksheets(2)
            lcellA = .Range("K" & Rows.count).End(xlUp).Offset(1).row
        End With
        
        'copy the Risk Definition name to the first empty cell in column K
        riskA.Copy Destination:=wb2.Worksheets(2).Range("K" & lcellA)
        
        'autofill the Risk definition from the first cell its in, down the the last row with data in column C
        With wb2.Worksheets(2)
            lcellA2 = .Range("K" & Rows.count).End(xlUp).row
            lrowA = .Range("C" & Rows.count).End(xlUp).row
            .Range("K" & lcellA2 & ":K" & lrowA).FillDown
        End With
        
    '****************************Risk B*********************************************
    '***********************Copy Base Data columns (Clinical Episode, MSDRG and N)
     'set the copy range
        With wsCheck
             Set copyBaseDataFrom = .Range(.Range("A5"), .Range("C5").End(xlDown))
        End With
    
        'set where we are copying to
        Set copyBaseDataTo = wb2.Worksheets(2).Range("C" & Rows.count).End(xlUp).Offset(1, 0)
    
        'perform the copy/paste without the borders
        copyBaseDataFrom.Copy
        copyBaseDataTo.PasteSpecial Paste:=xlPasteAllExceptBorders
        Application.CutCopyMode = False
      
    '************************** set copy/Paste Range for Risk B
        'set the copy range
        With wsCheck
             Set copyRiskBFrom = .Range(.Range("I5"), .Range("M5").End(xlDown))
        End With
        
        'set where we are copying to
        Set copyRiskBTo = wb2.Worksheets(2).Range("F" & Rows.count).End(xlUp).Offset(1, 0)
         
        'perform the copy/paste without the borders
        copyRiskBFrom.Copy
        copyRiskBTo.PasteSpecial Paste:=xlPasteAllExceptBorders
        Application.CutCopyMode = False
        
    '************************************** Risk B Definition
         'capture the Risk Definition
        With wsCheck
            Set riskB = .Range("I3")
        End With
        
        'determine the last full cell in Column K and start in the next one down
        With wb2.Worksheets(2)
            lcellB = .Range("K" & Rows.count).End(xlUp).Offset(1).row
        End With
        
        'copy the Risk Definition name to the first empty cell in column K
        riskB.Copy Destination:=wb2.Worksheets(2).Range("K" & lcellB)
        
        'autofill the Risk definition from the first cell its in, down the the last row with data in column C
        With wb2.Worksheets(2)
            lcellB2 = .Range("K" & Rows.count).End(xlUp).row
            lrowB = .Range("C" & Rows.count).End(xlUp).row
            .Range("K" & lcellB2 & ":K" & lrowB).FillDown
        End With
       
    '****************************Risk C*********************************************
    '***********************Copy Base Data columns (Clinical Episode, MSDRG and N)
     'set the copy range
        With wsCheck
             Set copyBaseDataFrom = .Range(.Range("A5"), .Range("C5").End(xlDown))
        End With
    
        'set where we are copying to
        Set copyBaseDataTo = wb2.Worksheets(2).Range("C" & Rows.count).End(xlUp).Offset(1, 0)
    
        'perform the copy/paste without the borders
        copyBaseDataFrom.Copy
        copyBaseDataTo.PasteSpecial Paste:=xlPasteAllExceptBorders
        Application.CutCopyMode = False
      
    '************************** set copy/Paste Range for Risk B
        'set the copy range
        With wsCheck
             Set copyRiskCFrom = .Range(.Range("N5"), .Range("R5").End(xlDown))
        End With
        
        'set where we are copying to
        Set copyRiskCTo = wb2.Worksheets(2).Range("F" & Rows.count).End(xlUp).Offset(1, 0)
         
        'perform the copy/paste without the borders
        copyRiskCFrom.Copy
        copyRiskCTo.PasteSpecial Paste:=xlPasteAllExceptBorders
        Application.CutCopyMode = False
        
    '************************************** Risk B Definition
         'capture the Risk Definition
        With wsCheck
            Set riskC = .Range("N3")
        End With
        
        'determine the last full cell in Column K and start in the next one down
        With wb2.Worksheets(2)
            lcellC = .Range("K" & Rows.count).End(xlUp).Offset(1).row
        End With
        
        'copy the Risk Definition name to the first empty cell in column K
        riskC.Copy Destination:=wb2.Worksheets(2).Range("K" & lcellC)
        
        'autofill the Risk definition from the first cell its in, down the the last row with data in column C
        With wb2.Worksheets(2)
            lcellC2 = .Range("K" & Rows.count).End(xlUp).row
            lrowC = .Range("C" & Rows.count).End(xlUp).row
            .Range("K" & lcellC2 & ":K" & lrowC).FillDown
        End With
       
    '***********************************************************************************
    
    '******************************** Anchor Hosp Name
        'capture the Anchor Hopsital's name
        With wb.Worksheets(1)
            Set ancHosp = .Range("A3")
        End With
        
        'determine the last full cell in Column A and start in the next one down
        With wb2.Worksheets(2)
            lcell1 = .Range("A" & Rows.count).End(xlUp).Offset(1).row
        End With
        
        'copy the Anchor hospital name to the first empty cell in column A
        ancHosp.Copy Destination:=wb2.Worksheets(2).Range("A" & lcell1)
        
        'autofill the Anchor hospital name from the first cell its in, down the the last row with data in column C
        With wb2.Worksheets(2)
            lcell2 = .Range("A" & Rows.count).End(xlUp).row
            lrow = .Range("C" & Rows.count).End(xlUp).row
            .Range("A" & lcell2 & ":A" & lrow).FillDown
        End With
        
        If i = wb.Worksheets.count Then
            MsgBox "Workbook Complete."
            Exit Sub
                ElseIf i <> wb.Worksheets.count Then
                    i = i + 2
                    Set wsCheck = wb.Worksheets(i)
                    GoTo step1
    
           
        
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Caputre WB name on error and continue

    Where do you get the error?

    Are you checking for the contigous cells?

    PS Couldn't you use a For/Next loop for the worksheets, perhaps with a step of 2?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    I get the error on this line from VBA.

    copyBaseDataTo.PasteSpecial Paste:=xlPasteAllExceptBorders
    I did not know I could check for contiguous cells. But it seems to be something with just certain workbooks...no merged cells or anything like that, It just thinks they aren't contiguous. I can post the exact error if needed. Would this be overkill if it is a tiny portion of the workbooks?

    PS , I could, still working out other bugs before I consolidate...I inherited this mess.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Caputre WB name on error and continue

    What exactly do you mean the workbook/code thinks the cells aren't contiguous?

    Does that line of code cause an error?

    Could the problem actually be with the copied range, copyBaseDataFrom?

    To check for contiguous cells in a range variable you could use the range's Areas property, if all the cells in the range were contiguous that will return 1.

  5. #5
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    My company DL's a number of workbooks that are parsed out from a large database on a private site. We do not control the source workbooks until they are in our servers. That being said they are all the same layout and format but on some the range for copyDataBaseFrom (which will work on one WB won't work on another due to this contiguous cell issue.

    If it helps I have attached screen shots of the error box and the highlighted line it throws from.

    Like I said it works for 90% of the WBs for some reason it thinks on certain ones that the data is effed.

    Doc1.docx

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Caputre WB name on error and continue

    What ranges do the 2 variables copyBaseDataFrom and copyBaseDataTo refer to when you get the error?

  7. #7
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    the EXACT same ranges they do in other workbooks that work fine "A5" to "C5" and down to the last row with data in it.

    set the copy range
        With wsCheck
             Set copyBaseDataFrom = .Range(.Range("A5"), .Range("C5").End(xlDown))
        End With
    
        'set where we are copying to
        Set copyBaseDataTo = wb2.Worksheets(2).Range("C" & Rows.count).End(xlUp).Offset(1, 0)

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Caputre WB name on error and continue

    This should skip the pastespecial if copyBaseData is not a contiguous range and print the name of the workbook the problem was with to the debug window.
    If copyBaseData.Areas= 1 Then
        copyBaseDataTo.PasteSpecial Paste:=xlPasteAllExceptBorders
    Else
        Debug.Print wb.Name & " - not processed."
    End If

  9. #9
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    I tried that syntax using both CopyBaseDataFrom and copyBaseDataTo and I get and error that says argument is not optional

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Caputre WB name on error and continue

    There was anothe typo/omission,
    If copyBaseDataTo.Areas.Count= 1 Then

  11. #11
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    I appreciate the help, still throws the exact same error as before. Meh....i don't want to waste your time

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Caputre WB name on error and continue

    I've got a feeling that error isn't caused by a non-contiguous range though obviously that's what the message says the problem is.

    Can you print the address of the 2 ranges to the debug window?

    To do that add this just before the PasteSpecial
    Debug.Print "From range: " & CopyBaseDataFrom.Address & ", To range: " & copyBaseDataTo.Address

  13. #13
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    kersmack. There is the issue...now to fix it since it works all the other damn times LOL

    From Range: $A$5:$C$9 to range $C$2

    No idea why this happens only with certsin WBs

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Caputre WB name on error and continue

    Copying/pasting between those 2 ranges shouldn't be a problem.

    What happens if you add this just before the copy?

  15. #15
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    I love a good mystery...what happens if I put What before the copy?

  16. #16
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    I love a good mystery...what happens if I put What before the copy?

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Caputre WB name on error and continue

    Oops, sorry about that.

    I meant this.
    Application.CutCopyMode = False

  18. #18
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    Same error....tried it before both copyBaseData Variablese

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Caputre WB name on error and continue

    So it's still the same error as the one in the image you posted?

  20. #20
    Registered User
    Join Date
    08-27-2014
    Location
    Albany, NY
    MS-Off Ver
    Office 2010
    Posts
    91

    Re: Caputre WB name on error and continue

    Yes It is....Frustratingly, yes

+ 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. Macro code to continue iterating if error.
    By sid9221 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-23-2013, 03:39 PM
  2. Continue to get Sub not defined error? Can anyone help?
    By cody4334 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-18-2012, 05:00 PM
  3. Replies: 4
    Last Post: 04-01-2011, 12:02 PM
  4. Code error, highlight error, continue to RUN
    By JohnSeito in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-07-2008, 01:29 AM
  5. VB Formula error in macro, How to Continue?
    By Mikeco in forum Excel General
    Replies: 7
    Last Post: 04-30-2008, 07:29 PM

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