+ Reply to Thread
Results 1 to 11 of 11

Excel 2003 vs. 2007 VBA Issue

Hybrid View

  1. #1
    Registered User
    Join Date
    11-18-2009
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Unhappy Excel 2003 vs. 2007 VBA Issue

    I have written the following code to take lines of data off a "Master" tab and create separate "forms" that are prepopulated from the original data. It should only create/pre-fill as many forms as there are lines of data in the Master tab. This code works beautifully on my Excel 2007 at home, but I need it to work on Excel 2003 at the office. No luck.

    Please correct the code so that it'll work on both!

    Thanks so much!!

    Sub Copier2()
        Dim x As Range
        Dim y As Integer
        Dim z As Integer
        Dim SheetName As String
            
        Dim Title As String
        Dim Observation As String
        Dim Risk As String
        Dim Department As String
        Dim Description As String
        Dim CorrectiveAction As String
           
        Set x = Workbooks.Item(1).Worksheets.Item("Master").Range("F2")
        y = 3
        z = 2
    
        For numtimes = 1 To x
            'Loop by using x as the index number to make x number copies.
            'Replace "Sheet4" with the name of the sheet to be copied.
                
            ActiveWorkbook.Sheets("Report").Copy _
            After:=ActiveWorkbook.Sheets("Report")
            
            Title = "A" & y
            Range("Title").Select
            ActiveCell.FormulaR1C1 = Workbooks.Item(1).Worksheets.Item("Master").Range(Title)
            
            Observation = "F" & y
            Range("Observation").Select
            ActiveCell.FormulaR1C1 = Workbooks.Item(1).Worksheets.Item("Master").Range(Observation)
            
            Risk = "K" & y
            Range("Risk").Select
            ActiveCell.FormulaR1C1 = Workbooks.Item(1).Worksheets.Item("Master").Range(Risk)
            
            Department = "D" & y
            Range("Department").Select
            ActiveCell.FormulaR1C1 = Workbooks.Item(1).Worksheets.Item("Master").Range(Department)
            
            Description = "H" & y
            Range("FindingDescription").Select
            ActiveCell.FormulaR1C1 = Workbooks.Item(1).Worksheets.Item("Master").Range(Description)
            
            CorrectiveAction = "M" & y
            Range("CorrectiveAction").Select
            ActiveCell.FormulaR1C1 = Workbooks.Item(1).Worksheets.Item("Master").Range(CorrectiveAction)
            
            y = y + 1
            z = z + 1
                    
        Next
        End Sub
    Last edited by astronautika; 11-18-2009 at 04:02 PM.

  2. #2
    Registered User
    Join Date
    11-18-2009
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Excel 2003 vs. 2007 VBA Issue

    Sorry forgot to post the error...Runtime Error 9: Subscript out of Range at the following point:
    Set x = Workbooks.Item(1).Worksheets.Item("Master").Range("F2")
    ...and, again, works fine on Excel 2007. No errors!
    Last edited by Paul; 11-18-2009 at 04:09 PM. Reason: Added code tags

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Excel 2003 vs. 2007 VBA Issue

    I don't have 2003 any longer to test, but try changing:
    Set x = Workbooks.Item(1).Worksheets.Item("Master").Range("F2")
    to
    Set x = Workbooks(1).Worksheets("Master").Range("F2")
    Be sure to change the rest of the rows that use that format. Not sure why you would need the "Item" section in either version.

  4. #4
    Registered User
    Join Date
    11-18-2009
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Excel 2003 vs. 2007 VBA Issue

    No luck, unfortunately...same error. Thanks though!

  5. #5
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Excel 2003 vs. 2007 VBA Issue

    You DIM'd variable x as Range, but you're trying to use it in a loop. I'm guessing you're trying to set the upper limit of the loop to the value in cell F2? If so, just DIM variable x as Long. (y and z, too. Long is usually safer than Integer, since it has a much greater range.)

  6. #6
    Forum Contributor
    Join Date
    04-01-2009
    Location
    Irvine, CA
    MS-Off Ver
    Excel 2010
    Posts
    280

    Re: Excel 2003 vs. 2007 VBA Issue

    I tried this in 2003 and 2007 and in both cases the Workbooks.Item(1). reference caused the error you described. If I removed that part the code would continue.

    Also, the code failed on the select statements (like):

            Title = "A" & y
            Range("Title").Select
    If you remove the quotes it works:
            Title = "A" & y
            Range(Title).Select
    Or you could just select the range without the string variable:
            Range("A" & y).Select

  7. #7
    Registered User
    Join Date
    11-18-2009
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Excel 2003 vs. 2007 VBA Issue

    Got a new error, but it feels like progress! I apologize...I'm very new to VBA code, but trying to plug my way through!

    As I was getting an "Object Required" error, I did some reading and got rid my "Set" here
    Set x = Worksheets("Master").Range("F2")
    I also changed the code to remove the quotes as Chance suggested.

    Now, I've got this code:
        Sub Copier2()
        Dim x As Long
        Dim y As Long
        Dim z As Long
        Dim SheetName As String
            
        Dim Title As String
        Dim Observation As String
        Dim Risk As String
        Dim Department As String
        Dim Description As String
        Dim CorrectiveAction As String
        
        
        
        x = Worksheets("Master").Range("F2")
        y = 3
        z = 2
    
        For numtimes = 1 To x
            'Loop by using x as the index number to make x number copies.
            'Replace "Sheet4" with the name of the sheet to be copied.
            
            
            ActiveWorkbook.Sheets("Report").Copy _
            After:=ActiveWorkbook.Sheets("Report")
            
            Title = "A" & y
            Range(Title).Select
            ActiveCell.FormulaR1C1 = Worksheets("Master").Range(Title)
            
            Observation = "F" & y
            Range(Observation).Select
            ActiveCell.FormulaR1C1 = Worksheets("Master").Range(Observation)
            
            Risk = "K" & y
            Range(Risk).Select
            ActiveCell.FormulaR1C1 = Worksheets("Master").Range(Risk)
            
            Department = "D" & y
            Range(Department).Select
            ActiveCell.FormulaR1C1 = Worksheets("Master").Range(Department)
            
            Description = "H" & y
            Range(Description).Select
            ActiveCell.FormulaR1C1 = Worksheets("Master").Range(Description)
            
            CorrectiveAction = "M" & y
            Range(CorrectiveAction).Select
            ActiveCell.FormulaR1C1 = Worksheets("Master").Range(CorrectiveAction)
            
            y = y + 1
    And, I get this error "Runtime Error 1004: Application-defined or object-defined error" at:
    ActiveCell.FormulaR1C1 = Worksheets("Master").Range(CorrectiveAction)

  8. #8
    Forum Contributor
    Join Date
    04-01-2009
    Location
    Irvine, CA
    MS-Off Ver
    Excel 2010
    Posts
    280

    Re: Excel 2003 vs. 2007 VBA Issue

    I agree with shg, the code works for me.
    Can you post a dummy file so we can see if your data is causing the error?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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