PDA

View Full Version : Solved: Compile Error of Duplicate Decleration in Current Scope



jo15765
07-11-2012, 06:36 PM
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!!!

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




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

Kenneth Hobs
07-11-2012, 07:13 PM
I don't see that error. Where is you first Sub? You have two End Subs.

jo15765
07-11-2012, 09:26 PM
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)

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

Aussiebear
07-12-2012, 02:24 AM
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

snb
07-12-2012, 03:57 AM
You can test the effect of the code with:


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








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









Sub DrillTwo(c01,c02,c03)


msgbox c01 & vblf & c02 & vblf & c03

end sub


















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

RunThis wbName, supName, cPDF, lCon, firstVal , matchVal , q

Kenneth Hobs
07-12-2012, 05:28 AM
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.

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

IanFScott
07-12-2012, 05:34 AM
The definitions of the variables should be in the called routine not the calling as:


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

jo15765
07-12-2012, 05:56 AM
Ah, I see now. Instead of trying to convert the variable to String in the pass..

CStr(wbName))


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!