+ Reply to Thread
Results 1 to 11 of 11

Correcting Compile error: duplicate declaration...

Hybrid View

JPDutch Correcting Compile error:... 01-14-2017, 02:07 AM
Trebor76 Re: Correcting Compile error:... 01-14-2017, 02:28 AM
JPDutch Re: Correcting Compile error:... 01-14-2017, 01:02 PM
stimulus Re: Correcting Compile error:... 01-14-2017, 08:24 AM
JPDutch Re: Correcting Compile error:... 01-14-2017, 01:13 PM
jaslake Re: Correcting Compile error:... 01-14-2017, 05:06 PM
JPDutch Re: Correcting Compile error:... 01-14-2017, 07:19 PM
  1. #1
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Correcting Compile error: duplicate declaration...

    I am trying to combine two sub macros with a Sub Main() macro, but I get a “Compile error: duplicate declaration in current scope.” I know I have two occurrences of “Dim i” (I think that’s the problem) but I don’t know a work-around. Could anyone offer of a suggestion? The code is as follows:

    Sub Main() 'CreateWorkbooks1()
    '   This one creates new workbooks
    '   Created by JieJenn & jpdutch 01-11-12
    '   Switch "219" with "20" below to test as needed...and vise versa
    '
    '   Const NumCopies As Long = 219
        Const NumCopies As Long = 20
        Dim FilePath As String
        Dim i As Long
        Application.DisplayAlerts = False
    
    '   Be sure to select the correct path in which to create the workbooks (01/27/16: I changed the dates below to 2016)
    
         FilePath = "C:\Users\john.johnson\Desktop\2016 Test Macros\Final Logs-2"
        For i = 1 To NumCopies
    
        ActiveWorkbook.SaveAs Filename:=FilePath & "\" & i & Range("D" & i + 40).Text & "_OSHA LOG_2016 " & ".xlsm"
        Next i
        Application.DisplayAlerts = True
    
            ' End Sub
    
        '   Sub InserYearBranchNumberVLkup2()
    ' Macro recorded 01/12/2012 by jpdutch
    ' This starts things rolling 'Use this one if needed to format & clean-up worksheets
    '   Dim fs As FileSearch
        Dim i As Integer
        Dim Wbk As Workbook
        Dim sFile As String
        
        sFile = Dir(ThisWorkbook.Path & "\*.xlsm")
        Do While sFile <> ""
        Set Wbk = Workbooks.Open(ThisWorkbook.Path & "\" & sFile)
            Sheets("Instruction Sheet").Visible = True
            Sheets("Data").Visible = True
            Sheets("Data").Select
            ActiveSheet.Unprotect
    '   This will set the correct year. You'll need to change this each year. Then it does the Vlookup Function.
            Range("A1").Select
            ActiveCell.FormulaR1C1 = "2016"
            Range("A1").Select
    '   This Inserts the Branch number into "A4"
            Range("A4").Select
            ActiveCell.FormulaR1C1 = _
            "=LEFT(MID(CELL(""filename""),FIND(""["",CELL(""filename""),1)+1,255),SEARCH(""_OSHA"",MID(CELL(""filename""),FIND(""["",CELL(""filename""),1)+1,255),1)-1)*1"
            Range("A5").Select
    '   This will format the range for general so the VLookUp will work
            Columns("B:B").ColumnWidth = 50
            Range("B4:D25").Select
            Selection.NumberFormat = "General"
    '   This will do the VLookUps
            Range("B4:D25").Select
            Selection.ClearContents
            Range("B4").Select
            ActiveCell.FormulaR1C1 = _
                "=IF(ISERROR(VLOOKUP(RC[-1],'Hours & EE Count'!R3C4:R65536C7,2,FALSE)),""DO NOT PRINT"",(VLOOKUP(RC[-1],'Hours & EE Count'!R3C4:R65536C7,2,FALSE)))"
            Range("C4").Select
            ActiveCell.FormulaR1C1 = _
                "=IF(ISERROR(VLOOKUP(RC[-2],'Hours & EE Count'!R3C4:R65536C7,3,FALSE)),"""",(VLOOKUP(RC[-2],'Hours & EE Count'!R3C4:R65536C7,3,FALSE)))"
            Range("D4").Select
            ActiveCell.FormulaR1C1 = _
                "=IF(ISERROR(VLOOKUP(RC[-3],'Hours & EE Count'!R3C4:R65536C7,4,FALSE)),"""",(VLOOKUP(RC[-3],'Hours & EE Count'!R3C4:R65536C7,4,FALSE)))"
            Range("B4:D4").Select
            Selection.AutoFill Destination:=Range("B4:D25"), Type:=xlFillDefault
            Range("A1").Select
    '   This will copy the values and paste them as values only.
            Range("A4:D25").Select
            Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
                xlNone, SkipBlanks:=False, Transpose:=False
            Sheets("Data").Select
            Range("A1").Select
    '   This closes the sheets
            Sheets("Instruction Sheet").Visible = False
            Sheets("Data").Visible = False
            Sheets("Hours & EE Count").Visible = False
    '        Sheets("Staff Directory").Visible = False
            Sheets("Branch Directory").Visible = False
            Sheets("EE Injuries").Visible = False
            Wbk.Close SaveChanges:=True
         sFile = Dir
       Loop
    
    End Sub
    Thank you in advance!
    JPDutch

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Correcting Compile error: duplicate declaration...

    You're correct - you can't define two dimensions (Dim) in the same macro with the same name. The second i dimension isn't being used at all - delete or comment it out.
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Re: Correcting Compile error: duplicate declaration...

    Quote Originally Posted by Trebor76 View Post
    You're correct - you can't define two dimensions (Dim) in the same macro with the same name. The second i dimension isn't being used at all - delete or comment it out.
    Thanks Treor76, I tried commenting it out, but the "program" didn't function the way it does when they run separately. My problem is that I don't know the options to place into the code. For example, if I comment that second i dimension out, it no longer places the correct numbering for all the OSHA logs I'm trying to generate, so when I take out the "dim i as integer" what would I put in its place to keep the correct numbering in place...which is used by the vLookup? I'm sure the answer is staring me in the face, but I'm just not seeing it. Thanks again for your help.

  4. #4
    Registered User
    Join Date
    01-22-2013
    Location
    Lowestoft, England
    MS-Off Ver
    2016
    Posts
    17

    Re: Correcting Compile error: duplicate declaration...

    You also have two constants called NumCopies.

  5. #5
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Re: Correcting Compile error: duplicate declaration...

    Thank you... All the code that I posted was developed over the years by members of this forum helping me out. Some of the items make sense, and I'm trying to learn VBA, but there is so much that still doesn't make sense to me. For example, I'm still struggling with the work-around for commenting out the "dim i As long" or "dim i As interger." If I remove either one, the program doesn't function properly. Thanks again for your help...

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Correcting Compile error: duplicate declaration...

    Hi JPDutch

    The Code will not Compile with the Duplicate Declarations and, accordingly, will not run. As Trebor pointed out, Dim i as Integer is not used in the Code.

    You indicate Dim i as Integer is used in the Sub InserYearBranchNumberVLkup2() part of the Code...I don't see where.

    Bottom line, removing Dim i as Integer appears to have no relationship to this...
    I tried commenting it out, but the "program" didn't function the way it does when they run separately.
    or this...
    it no longer places the correct numbering for all the OSHA logs I'm trying to generate,
    There's something else going on. If you wish help troubleshooting you'll need to attach your File(s).
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  7. #7
    Registered User
    Join Date
    01-15-2011
    Location
    Clearfield, UT
    MS-Off Ver
    Excel 2016
    Posts
    58

    Re: Correcting Compile error: duplicate declaration...

    Well, the file has confidential information, so I cannot send it. Allow me to put in the original two macros so you can see it as compared to what I'm hoping to accomplish. I'm trying to create 219 OSHA Logs and Summary pages. The first macro creates the 219 OSHA Log and Summary workbooks (now set to 8 workbooks) and names and saves the file...and it works well. The second macro puts the year in cell A1 and then the first branch number in A4; after that various vlookups pull information into the spreadsheet based on the branch number like injuries, branch addresses, phone numbers etc. to then populate the Log and Summary; this second macro also works well. The problem comes when I try to merge the two into one macro.

    Sub CreateWorkbooks1()
    '   This one creates new workbooks
    '   Created by JieJenn & jpdutch 01-11-12
    '   Switch 915 with "10" below as needed...and vis
    '
    '    Const NumCopies As Long = 219
        Const NumCopies As Long = 8
        Dim FilePath As String
        Dim i As Long
        Application.DisplayAlerts = False
    
    '   Be sure to select the correct path in which to create the workbooks (01/27/16: I changed the dates below to 2015)
    '   Was FilePath = "C:\Users\johnsoj\Desktop\2016 Test Macros\Final Logs"
         FilePath = "C:\Users\john.johnson\Desktop\2016 Test Macros\FINAL Logs"
        For i = 1 To NumCopies
    '    ActiveWorkbook.SaveAs Filename:=FilePath & Corporate & "\" & i & "_OSHA LOG_2015 " & ".xlsm" Does this need to be modified?
    '    Range("D" & i + 40).Text &
        ActiveWorkbook.SaveAs Filename:=FilePath & "\" & i & "_OSHA LOG_2016 " & " .xlsm"
        Next i
        Application.DisplayAlerts = True
    
    End Sub
    Here's the second macro I use to populate the OSHA Logs and Summary Pages:

    Sub InserYearBranchNumberVLkup2()
    ' Macro recorded 01/12/2012 by jpdutch
    ' This starts things rolling 'Use this one if needed to format & clean-up worksheets
    '    Dim fs As FileSearch
        Dim i As Integer
        Dim Wbk As Workbook
        Dim sFile As String
        
    '    Set fs = Application.FileSearch
        sFile = Dir(ThisWorkbook.Path & "\* .xlsm")
        Do While sFile <> ""
    '    With fs
    '        .LookIn = ThisWorkbook.Path
    '        .Filename = "*.xls"
    '    For i = 1 To .Execute()
    '        Set wbk = Workbooks.Open(.FoundFiles(i))
        Set Wbk = Workbooks.Open(ThisWorkbook.Path & "\" & sFile)
            Sheets("Instruction Sheet").Visible = True
            Sheets("Data").Visible = True
            Sheets("Data").Select
            ActiveSheet.Unprotect
    '   This will set the correct year. You'll need to change this each year. Then it does the Vlookup Function.
            Range("A1").Select
            ActiveCell.FormulaR1C1 = "2016"
            Range("A1").Select
    ' This Inserts the Branch number into "A4"
            Range("A4").Select
            ActiveCell.FormulaR1C1 = _
            "=LEFT(MID(CELL(""filename""),FIND(""["",CELL(""filename""),1)+1,255),SEARCH(""_OSHA"",MID(CELL(""filename""),FIND(""["",CELL(""filename""),1)+1,255),1)-1)*1"
            Range("A5").Select
    '   This will format the range for general so the VLookUp will work
            Columns("B:B").ColumnWidth = 50
            Range("B4:D25").Select
            Selection.NumberFormat = "General"
    '   This will do the VLookUps
            Range("B4:D25").Select
            Selection.ClearContents
            Range("B4").Select
            ActiveCell.FormulaR1C1 = _
                "=IF(ISERROR(VLOOKUP(RC[-1],'Hours & EE Count'!R3C4:R65536C7,2,FALSE)),""DO NOT PRINT"",(VLOOKUP(RC[-1],'Hours & EE Count'!R3C4:R65536C7,2,FALSE)))"
            Range("C4").Select
            ActiveCell.FormulaR1C1 = _
                "=IF(ISERROR(VLOOKUP(RC[-2],'Hours & EE Count'!R3C4:R65536C7,3,FALSE)),"""",(VLOOKUP(RC[-2],'Hours & EE Count'!R3C4:R65536C7,3,FALSE)))"
            Range("D4").Select
            ActiveCell.FormulaR1C1 = _
                "=IF(ISERROR(VLOOKUP(RC[-3],'Hours & EE Count'!R3C4:R65536C7,4,FALSE)),"""",(VLOOKUP(RC[-3],'Hours & EE Count'!R3C4:R65536C7,4,FALSE)))"
            Range("B4:D4").Select
            Selection.AutoFill Destination:=Range("B4:D25"), Type:=xlFillDefault
            Range("A1").Select
    '        Range("B4:D25").Select
    '   This will copy the values and paste them as values only.
            Range("A4:D25").Select
            Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
                xlNone, SkipBlanks:=False, Transpose:=False
            Sheets("Data").Select
            Range("A1").Select
    '        Range("A1").Select
    '        Sheets("Data").Visible = False
    '        Sheets("Data").Visible = True
    '   This closes the sheets
            Sheets("Instruction Sheet").Visible = False
            Sheets("Data").Visible = False
            Sheets("Hours & EE Count").Visible = False
    '        Sheets("Staff Directory").Visible = False
            Sheets("Branch Directory").Visible = False
            Sheets("EE Injuries").Visible = False
    '        Sheets("Data").Select
    '        Range("A1").Select
            Wbk.Close SaveChanges:=True
    '        wbk.Close SaveChanges:=False
    '    Next i
         sFile = Dir
       Loop
    'End With
    End Sub
    Combining these two is where the problem comes in, so I'm trying to modify the code to allow everything to run smoothly. Please let me know if you still need the file; if so, I'll remove the confidential information and then send it to you. Thanks to all who are helping me with this. JPDutch.

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Correcting Compile error: duplicate declaration...

    Hi JPDutch

    Well, if what you have truly works for you, don't combine the Code. Simply ADD the suggested Line of Code to Sub CreateWorkbooks1()

    Sub CreateWorkbooks1()
    '   This one creates new workbooks
    '   Created by JieJenn & jpdutch 01-11-12
    '   Switch 915 with "10" below as needed...and vis
    '
    '    Const NumCopies As Long = 219
        Const NumCopies As Long = 8
        Dim FilePath As String
        Dim i As Long
        Application.DisplayAlerts = False
    
    '   Be sure to select the correct path in which to create the workbooks (01/27/16: I changed the dates below to 2015)
    '   Was FilePath = "C:\Users\johnsoj\Desktop\2016 Test Macros\Final Logs"
         FilePath = "C:\Users\john.johnson\Desktop\2016 Test Macros\FINAL Logs"
        For i = 1 To NumCopies
    '    ActiveWorkbook.SaveAs Filename:=FilePath & Corporate & "\" & i & "_OSHA LOG_2015 " & ".xlsm" Does this need to be modified?
    '    Range("D" & i + 40).Text &
        ActiveWorkbook.SaveAs Filename:=FilePath & "\" & i & "_OSHA LOG_2016 " & " .xlsm"
        Next i
        Application.DisplayAlerts = True
    
        Call InserYearBranchNumberVLkup2  '<---Insert This Line of Code
    
    End Sub

+ 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. [SOLVED] Macro / Compile Error / Duplicate Declaration
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 PM
  2. Macro / Compile Error / Duplicate Declaration
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  3. Macro / Compile Error / Duplicate Declaration
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 07:05 AM
  4. [SOLVED] Macro / Compile Error / Duplicate Declaration
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 04:05 AM
  5. [SOLVED] Macro / Compile Error / Duplicate Declaration
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  6. [SOLVED] Macro / Compile Error / Duplicate Declaration
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  7. Macro / Compile Error / Duplicate Declaration
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 01:05 AM
  8. [SOLVED] Macro / Compile Error / Duplicate Declaration
    By carl in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-29-2005, 03:55 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