Consulting

Results 1 to 10 of 10

Thread: Runtime Error 1004 on 2nd run

  1. #1
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location

    Runtime Error 1004 on 2nd run

    Hello everybody,

    Im quite confused on my code below. The first time you run it it works fine the 2nd attempt always had the error. "pastespecial method of range class failed". Could you please enlighten me? TIA.

    Sub Rep1()
    
    Dim wbReport As Workbook
    Dim wbThis As Workbook
    Dim wbRep As String
        wbRep = Application.GetOpenFilename(FileFilter:="Excel workbook (*.xls),*.xls", Title:="Open Report")
        Set wbReport = Application.Workbooks.Open(wbRep)
        wbReport.Activate
    
    If ActiveSheet.Name = "Report1" Then
        Sheets("Report1").Select
        Cells.Select
        ActiveSheet.Range("$A$1:$AD$201").RemoveDuplicates Columns:=5, Header:=xlYes
        
        Range("D2:D500").Select
        Selection.Copy
        ThisWorkbook.Activate
     
        Worksheets("Input").Activate
        Sheets("Input").Range("C2:C500").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
    
        With Range("D2")
            .FormulaR1C1 = "=MID(RC[-1],LEN(RC[-1])-9,7)"
            .AutoFill Destination:=Range("D2:D100"), Type:=xlFillDefault
        End With
        
        wbReport.Close True
        Exit Sub
    Else
        MsgBox "Invalid Report. Please select the correct file.", vbCritical, "Error"
        wbReport.Close True
    
    End If
    
    End Sub

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    You can step through code with F8 to debug one line at a time. Try before your pastespecial:
    Debug.Print ActiveWorkbook.Name, ActiveSheet.Name, Selection.Address
    The results of Debug.Print after a run will be in the Immediate window.

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    perhaps...

    Sub Rep1()
         
        Dim wbReport As Workbook
        Dim wbRep As String
        
        wbRep = Application.GetOpenFilename(FileFilter:="Excel workbook (*.xls),*.xls", Title:="Open Report")
        Set wbReport = Application.Workbooks.Open(wbRep)
        
        With wbReport
            If .ActiveSheet.Name <> "Report1" Then
                MsgBox "Invalid Report. Please select the correct file.", vbCritical, "Error"
                .Close False
                Exit Sub
            End If
            .Worksheets("Report1").Cells(1).CurrentRegion.RemoveDuplicates Columns:=5, Header:=xlYes
        End With
        
        With ThisWorkbook.Worksheets("Input")
            .Range("C2:C500").Value = wbReport.Worksheets("Report1").Range("D2:D500").Value
            .Range("D2:D500").Formula = "=MID(C2,LEN(C2)-9,7)"
            .Range("D2:D500").Value = .Range("D2:D500").Value
        End With
        
        wbReport.Close True
         
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Whenever I need to have 2 WBs open, I found I make less (fewer?) mistakes by assigning to WB variables, and never relying on ActiveWorkbook or ThisWorkbook or ActiveSheet

     
    Option Explicit
    Sub Rep1()
         
        Dim wbReport As Workbook, wbThis As Workbook
        Dim wsReport As Worksheet, wsInput As Worksheet
        
        Dim wbRepName As String
        
        
        Set wbThis = ThisWorkbook
        Set wsInput = wbThis.Worksheets("Input")
        
        wbRepName = Application.GetOpenFilename(FileFilter:="Excel workbook (*.xls),*.xls", Title:="Open Report")
        If LCase(wbRepName) = "false" Then
            Exit Sub
        End If
        
        Set wbReport = Application.Workbooks.Open(wbRepName)
        
        Set wsReport = ActiveSheet
        
        If wsReport.Name <> "Report1" Then
            MsgBox "Invalid Report. Please select the correct file.", vbCritical, "Error"
            wbReport.Close True
            Exit Sub
        End If
            
        wsReport.Range("$A$1:$AD$201").RemoveDuplicates Columns:=5, Header:=xlYes
         
        wsReport.Range("D2:D500").Select
        Selection.Copy
        
        wbThis.Activate
        wsInput.Select
        wsInput.Range("C2:C500").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
         
        With wsInput.Range("D2")
            .FormulaR1C1 = "=MID(RC[-1],LEN(RC[-1])-9,7)"
            .AutoFill Destination:=Range("D2:D100"), Type:=xlFillDefault
        End With
         
        wbReport.Close True
         
        wbThis.Activate
        
    End Sub

    Paul

  5. #5
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    Hi all,

    Thanks so much for the speedy response.
    I will try to test and feedback on the results.

    Kind regards,
    deedii
    Last edited by deedii; 03-31-2014 at 07:04 AM.

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi deedii.

    since i did not use pastespecial method, it should throw different err msg. what err msg it throws and at which line is it?

    btw, uploading your workbook(s) with changed, representative data will help us.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    Hi mancubus,

    I edited my reply as I just adjusted some codes with the one provided. I will then look into it if the additional routine work. I will let you know the soonest. <hugs

  8. #8
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    Now I got it working using Paul modified code, so the answer really is to put the sheet/book into a variables rather than using THIS command. I tried your code mancubus it also makes the same result just changed the range of the cell. Thanks so much guys.
    Anyway I was just wondering how can I just display the number of result with the equal number on the item? I the below code it trims the mid data, copy the formula downward until it reaches D100.
    What I am thinking is that whether its possible to just display the result equal to the number of data in C2 column downward.


    With wsInput.Range("D2") 
            .FormulaR1C1 = "=MID(RC[-1],LEN(RC[-1])-9,7)" 
            .AutoFill Destination:=Range("D2:D100"), Type:=xlFillDefault 
    End With
    The original result im getting is

    000000169432003 0169432
    000000170050001 0170050
    000000113653002 0113653
    000000127814004 0127814
    000000156493001 0156493
    000000125982005 0125982
    000000141982001 0141982
    000000141985001 0141985
    000000093209001 0093209
    #VALUE!
    #VALUE!
    #VALUE!
    #VALUE!
    #VALUE!
    #VALUE!
    #VALUE!
    The #value keep on populating the column until D100, however the data in column C only until 000000093209001. I was wondering if possible to have the expected result like below.

    000000169432003 0169432
    000000170050001 0170050
    000000113653002 0113653
    000000127814004 0127814
    000000156493001 0156493
    000000125982005 0125982
    000000141982001 0141982
    000000141985001 0141985
    000000093209001 0093209

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb() 
        With Workbooks.Open(Application.GetOpenFilename(FileFilter:="Excel workbook (*.xls),*.xls", Title:="Open Report") ) 
             .Sheets("Report1").Range("$A$1:$AD$201").RemoveDuplicates Columns:=5, Header:=xlYes 
           
            ThisWorkbook.sheets("Input").Range("C2:C500")= .Sheets("Report1").Range("D2:D500").Value 
            .Close True 
        End With 
    
        ThisWorkbook.sheets("Input").Range("D2:D500")= [if(input!C2:C500="","",mid(input!C2:C500,len(input!C2:C200)-9,7))]         
    End Sub

  10. #10
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    Wow snb, I really admire you how yo write the code so concisely. I will try this later or tomorrow and give you guys a feedback.
    How about if I use this formula, how can I incorporate it to that code? "=RIGHT(C2,7)" Is this correct? Anyway what does "," means?

    ThisWorkbook.sheets("Input").Range("D2:D500")= [if(input!C2:C500="","",right(input!C2:C500, 7)]

Posting Permissions

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