Consulting

Results 1 to 8 of 8

Thread: Solved: Compile Error of Duplicate Decleration in Current Scope

  1. #1
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location

    Solved: Compile Error of Duplicate Decleration in Current Scope

    I am running the same lines of code (the top code post) in about 10 different procedures so I wanted to create ONE module and just call it from there and do something like this (the bottom code post). Problem I have is that it highlights the RunThis line of code and says duplicate declaration in current scope, and I can't figure out what my problem is!!!
    [vba]
    Public Sub Enough()
    Dim wbName() As Variant
    Dim supName() As Variant
    Dim cPDF As String
    Di lCon As String
    Dim firstVal As String
    Dim matchVal As String
    Dim q

    wbName = Array("Alpha", "Omega", "Trillvil")
    supName = Array("Richard", "George", "Jose")

    For q = LBound(wbName) To UBound(wbName)
    Call DrillOne(CStr(wbName(q)), PM, CStr(supName(q)), cPDF, lCon, firstVal, matchVal)
    Call DrillTwo(CStr(wbName(q)), PM, CStr(supName(q)))
    Next q

    End Sub
    [/vba]


    [vba]
    Dim wbName() As Variant
    Dim supName() As Variant
    Dim cPDF As String
    Dim lCon As String
    Dim firstVal As String
    Dim matchVal As String
    Dim q

    wbName = Array("Alpha", "Omega", "Trillvil")
    supName = Array("Richard", "George", "Jose")

    Call RunThis(CStr(wbName)), CStr(supName)), cPDF As String, lCon As String, firstVal As String, matchVal As String, q)

    End Sub

    Public Sub RunThis(wbName, supName, cPDF, lCon, firstVal, matchVal, q)
    For q = LBound(wbName) To UBound(wbName)
    Call DrillOne(wbName, PM, supName, cPDF, lCon, firstVal, matchVal)
    Call DrillTwo(wbName, PM, supName)
    Next q
    End Sub
    [/vba]

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I don't see that error. Where is you first Sub? You have two End Subs.

  3. #3
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location
    Looks like I missed the top piece of the code when I copied...it should read like this (with a public sub at the very tip top)
    [vba]
    Public Sub ComesFirst()
    Dim wbName() As Variant
    Dim supName() As Variant
    Dim cPDF As String
    Dim lCon As String
    Dim firstVal As String
    Dim matchVal As String
    Dim q

    wbName = Array("Alpha", "Omega", "Trillvil")
    supName = Array("Richard", "George", "Jose")

    Call RunThis(CStr(wbName)), CStr(supName)), cPDF As String, lCon As String, firstVal As String, matchVal As String, q)

    End Sub

    Public Sub RunThis(wbName, supName, cPDF, lCon, firstVal, matchVal, q)
    For q = LBound(wbName) To UBound(wbName)
    Call DrillOne(wbName, PM, supName, cPDF, lCon, firstVal, matchVal)
    Call DrillTwo(wbName, PM, supName)
    Next q
    End Sub
    [/vba]

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    My understanding is that when you call another sub, you don't need to define the extras as in .

    Call RunThis
    Call DrillOne
    Call DrillTwo
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You can test the effect of the code with:


    [vba]Sub Enough()
    wbName = Array("Alpha", "Omega", "Trillvil")

    supName = Array("Richard", "George", "Jose")
    PM="PM-value"
    cPDF="cPDF-value"
    ICon="Icon-value"
    firstVal="FirstVal-value"
    matchVal="Matchval-Value"

    For j = 0 To UBound(wbName)
    DrillOne wbName(j), PM, supName(j), cPDF, lCon, firstVal, matchVal
    DrillTwo wbName(j), PM, supName(j)
    Next
    End Sub


    [/vba]



    [vba]
    Sub DrillOne(c01,c02,c03,c04,c05,c06,c07)

    msgbox c01 & vblf & c02 & vblf & c03 & vblf & c04 & vblf & c05 & vblf & c06 & vblf & c07
    end sub

    [/vba]




    [vba]
    Sub DrillTwo(c01,c02,c03)


    msgbox c01 & vblf & c02 & vblf & c03
    end sub





    [/vba]








    This you can't do
    Call RunThis(CStr(wbName)), CStr(supName)), cPDF As String, lCon As String, firstVal As String, matchVal As String, q)
    This you could
    [vba]
    RunThis wbName, supName, cPDF, lCon, firstVal , matchVal , q
    [/vba]


  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I never got the scope error that you did. As snb showed, your Call defined the variable types. Types are declared/defined in Dim, and the Sub or Function parameters. Look up the help for Call if you want to use that. I don't use it myself. When you don't use it, you don't need to surround the parameters with ()'s.

    Look into the use of Optional and maybe even parameter array for the Drill routines.

    [VBA]Sub ComesFirst()
    Dim wbName() As Variant
    Dim supName() As Variant
    Dim cPDF As String
    Dim lCon As String
    Dim firstVal As String
    Dim matchVal As String
    Dim q As Integer

    wbName = Array("Alpha", "Omega", "Trillvil")
    supName = Array("Richard", "George", "Jose")

    RunThis wbName(), supName(), cPDF, lCon, firstVal, matchVal, q
    End Sub

    Public Sub RunThis(wbName() As Variant, supName() As Variant, cPDF As String, _
    lCon As String, firstVal As String, matchVal As String, q As Integer)
    For q = LBound(wbName) To UBound(wbName)
    MsgBox wbName(q) & vbLf & supName(q)
    'Call DrillOne(wbName, PM, supName, cPDF, lCon, firstVal, matchVal)
    'Call DrillTwo(wbName, PM, supName)
    Next q
    End Sub[/VBA]

  7. #7
    The definitions of the variables should be in the called routine not the calling as:
    [vba]
    Public Sub ComesFirst()
    Dim wbName() As Variant
    Dim supName() As Variant
    Dim cPDF As String
    Dim lCon As String
    Dim firstVal As String
    Dim matchVal As String
    Dim q

    wbName = Array("Alpha", "Omega", "Trillvil")
    supName = Array("Richard", "George", "Jose")

    Call RunThis(wbName), supName, cPDF, lCon, firstVal, matchVal, q)

    End Sub

    Public Sub RunThis(wbName, supName, cPDF As String, lCon As String, firstVal As String, matchVal As String, q)
    For q = LBound(wbName) To UBound(wbName)
    Call DrillOne(wbName, PM, supName, cPDF, lCon, firstVal, matchVal)
    Call DrillTwo(wbName, PM, supName)
    Next q
    End Sub
    [/vba]

  8. #8
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location
    Ah, I see now. Instead of trying to convert the variable to String in the pass..
    [vba]
    CStr(wbName))
    [/vba]

    I just needed to pass the variable in it's declared format, and then convert to string when actually using the variable.

    Thanks to all who replied, seeing the examples definitely helped!

Posting Permissions

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