+ Reply to Thread
Results 1 to 5 of 5

Conversion from Windows XP to Windows 2007 has killed my macro - Run-time Error 1004

Hybrid View

  1. #1
    Registered User
    Join Date
    01-22-2013
    Location
    Winnipeg, Manitoba
    MS-Off Ver
    Excel 2007
    Posts
    15

    Conversion from Windows XP to Windows 2007 has killed my macro - Run-time Error 1004

    Hi,

    I had the following code which was working when I was under Windows XP. After converting to Windows 2007, I tried running the same macro to no avail. Run-time error 1004 would pop up. The debugger would highlight the Copy Before action. Any advice is greatly appreciated!

    Sub RegionSplit()
        Dim nodupes As New Collection
        Dim OutWB As Workbook
        Dim strRONum As String
      
        'determine a list of unique regions
        With Sheets("Report")
            On Error Resume Next
            For Each ce In .Range(.Range("F9"), .Cells(Rows.Count, "F").End(xlUp))
                nodupes.Add Item:=ce.Value, Key:=ce.Value
            Next ce
            On Error GoTo 0
        End With
      
        lstdatarow = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row
        lstcsltrow = Sheets("Cslt_Detail").Cells(Rows.Count, 1).End(xlUp).Row
      
        For i = 1 To nodupes.Count
            Workbooks.Add
    
            Set OutWB = ActiveWorkbook
            ThisWorkbook.Activate
            Sheets("Reference").Copy Before:=OutWB.Sheets(1)        
            ThisWorkbook.Activate
            Sheets("Report").Copy after:=OutWB.Sheets(1)
            ThisWorkbook.Activate
            Sheets("Cslt_Detail").Copy after:=OutWB.Sheets(2)
            ThisWorkbook.Activate
            OutWB.Sheets("Report").Rows("9:" & lstdatarow).ClearContents
            OutWB.Sheets("Cslt_Detail").Rows("3:" & lstcsltrow).ClearContents
          
            With Sheets("Report")
                .Range("H1").Value = Sheets("Report").Range("F8").Value
                .Range("H2").Value = nodupes(i)
                .Range(.Range("A8"), .Range("AO" & .Cells(Rows.Count, 1).End(xlUp).Row)).AdvancedFilter , Action:=xlFilterCopy, criteriarange:=.Range("H1:H2"), copytorange:=OutWB.Sheets("Report").Range("A8:AO8")
            End With
        
            lstdatarowreg = OutWB.Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row
            OutWB.Sheets("Report").Range("A" & lstdatarowreg + 1 & ":AO" & lstdatarowreg + 1).ClearFormats
            OutWB.Sheets("Report").Range("A" & lstdatarowreg + 1 & ":AO" & lstdatarowreg + 1).Interior.ColorIndex = 2
            OutWB.Sheets("Report").Range("A" & lstdatarowreg + 2 & ":A65000").EntireRow.Delete
            strRONum = "RO " & Format(OutWB.Sheets("Report").Range("A9"), "000")
        
            With Sheets("Cslt_Detail")
                .Range("AS1").Value = Sheets("Cslt_Detail").Range("B2").Value
                .Range("AS2").Value = nodupes(i)
                .Range(.Range("A2"), .Range("AR" & .Cells(Rows.Count, 1).End(xlUp).Row)).AdvancedFilter , Action:=xlFilterCopy, criteriarange:=.Range("AS1:AS2"), copytorange:=OutWB.Sheets("Cslt_Detail").Range("A2:AR2")
            End With
        
            lstcsltrowreg = OutWB.Sheets("Cslt_Detail").Cells(Rows.Count, 1).End(xlUp).Row
            OutWB.Sheets("Cslt_Detail").Range("A" & lstcsltrowreg + 1 & ":A65000").EntireRow.Delete
              
            OutWB.Activate
        
            Application.DisplayAlerts = False
        
            For j = Sheets.Count To 4 Step -1
                Sheets(j).Delete
            Next j
        
            Application.DisplayAlerts = True
        
            Sheets("Report").Range("H1:H2").ClearContents
            ActiveWorkbook.SaveAs Filename:="C:\Temp\" & Left(ThisWorkbook.Name, _
                Len(ThisWorkbook.Name) - 20) & strRONum, FileFormat:=51
            Sheets("cslt_Detail").Range("AS1:AS2").ClearContents
            Sheets("Reference").Activate
            Range("A1").Select
            OutWB.Close savechanges:=True
        
        Next i
        
        Sheets("Report").Range("H1:H2").ClearContents
        Sheets("cslt_Detail").Range("AS1:AS2").ClearContents
    
    End Sub
    Thank you!

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Conversion from Windows XP to Windows 2007 has killed my macro - Run-time Error 1004

    Is this macro using more than one workbook? It doesn't seem to be. So why are you setting and activating this workbook?
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Conversion from Windows XP to Windows 2007 has killed my macro - Run-time Error 1004

    Maybe try this:

    Sub RegionSplit()
        Dim nodupes As New Collection
        Dim strRONum As String
      
        'determine a list of unique regions
        With Sheets("Report")
            On Error Resume Next
            For Each ce In .Range(.Range("F9"), .Cells(Rows.Count, "F").End(xlUp))
                nodupes.Add Item:=ce.Value, Key:=ce.Value
            Next ce
            On Error GoTo 0
        End With
      
        lstdatarow = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row
        lstcsltrow = Sheets("Cslt_Detail").Cells(Rows.Count, 1).End(xlUp).Row
      
        For i = 1 To nodupes.Count
            Workbooks.Add
    
            Sheets("Reference").Copy Before:=Sheets(1)
            Sheets("Report").Copy after:=Sheets(1)
            Sheets("Cslt_Detail").Copy after:=Sheets(2)
            Sheets("Report").Rows("9:" & lstdatarow).ClearContents
            Sheets("Cslt_Detail").Rows("3:" & lstcsltrow).ClearContents
          
            With Sheets("Report")
                .Range("H1").Value = Sheets("Report").Range("F8").Value
                .Range("H2").Value = nodupes(i)
                .Range(.Range("A8"), .Range("AO" & .Cells(Rows.Count, 1).End(xlUp).Row)).AdvancedFilter , Action:=xlFilterCopy, criteriarange:=.Range("H1:H2"), copytorange:=Sheets("Report").Range("A8:AO8")
            End With
        
            lstdatarowreg = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row
            Sheets("Report").Range("A" & lstdatarowreg + 1 & ":AO" & lstdatarowreg + 1).ClearFormats
            Sheets("Report").Range("A" & lstdatarowreg + 1 & ":AO" & lstdatarowreg + 1).Interior.ColorIndex = 2
            Sheets("Report").Range("A" & lstdatarowreg + 2 & ":A65000").EntireRow.Delete
            strRONum = "RO " & Format(Sheets("Report").Range("A9"), "000")
        
            With Sheets("Cslt_Detail")
                .Range("AS1").Value = Sheets("Cslt_Detail").Range("B2").Value
                .Range("AS2").Value = nodupes(i)
                .Range(.Range("A2"), .Range("AR" & .Cells(Rows.Count, 1).End(xlUp).Row)).AdvancedFilter , Action:=xlFilterCopy, criteriarange:=.Range("AS1:AS2"), copytorange:=Sheets("Cslt_Detail").Range("A2:AR2")
            End With
        
            lstcsltrowreg = Sheets("Cslt_Detail").Cells(Rows.Count, 1).End(xlUp).Row
            Sheets("Cslt_Detail").Range("A" & lstcsltrowreg + 1 & ":A65000").EntireRow.Delete
        
            Application.DisplayAlerts = False
        
            For j = Sheets.Count To 4 Step -1
                Sheets(j).Delete
            Next
        
            Application.DisplayAlerts = True
        
            Sheets("Report").Range("H1:H2").ClearContents
            ActiveWorkbook.SaveAs Filename:="C:\Temp\" & Left(ThisWorkbook.Name, _
                Len(ThisWorkbook.Name) - 20) & strRONum, FileFormat:=51
            Sheets("cslt_Detail").Range("AS1:AS2").ClearContents
            Sheets("Reference").Activate
            Range("A1").Select
            Thisworkbook.Close savechanges:=True
        
        Next
        
        Sheets("Report").Range("H1:H2").ClearContents
        Sheets("cslt_Detail").Range("AS1:AS2").ClearContents
    
    End Sub
    abousetta

  4. #4
    Registered User
    Join Date
    01-22-2013
    Location
    Winnipeg, Manitoba
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Conversion from Windows XP to Windows 2007 has killed my macro - Run-time Error 1004

    Hi abousetta,

    Thank you for the quick reply, I will try this tomorrow when I am back at work. The code is basically trying to copy from the main workbook to generate multiple workbooks (one per region based on a region column identifier). I am not well-versed with VB and I'm sure there's a much cleaner way to get the result (your suggestion definitely looks neater). At the time when I was in Windows XP, the original code did work, but for some reason after converting to Windows 7, it went wonky.

    Much appreciated! I will follow-up tomorrow.

  5. #5
    Registered User
    Join Date
    01-22-2013
    Location
    Winnipeg, Manitoba
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Conversion from Windows XP to Windows 2007 has killed my macro - Run-time Error 1004

    Good morning,

    Inputting the above code gets me a new error - Run-time error 9 (Subscript out of range). Debugging highlights the following:

    Sheets("Reference").Copy Before:=Sheets(1)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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