Permalink
Cannot retrieve contributors at this time
| title | keywords | f1_keywords | ms.prod | ms.assetid | ms.date | ms.localizationpriority |
|---|---|---|---|---|---|---|
|
Duplicate declaration in current scope |
vblr6.chm1011221 |
vblr6.chm1011221 |
office |
55b4c056-c787-b30a-bc8c-30e552a3cdbb |
06/08/2017 |
medium |
The specified name is already used at this level of scope. For example, two variables can have the same name if they are defined in different procedures, but not if they are defined within the same procedure. This error has the following causes and solutions:
-
A new variable or procedure has the same name as an existing variable or procedure. For example:
Sub MySub() Dim A As Integer Dim A As Variant . . . ' Other declarations or procedure code here. End Sub
Check the current procedure, module, or project and remove any duplicate declarations.
-
A Const statement uses the same name as an existing variable or procedure. Remove or rename the constant in question.
-
You declared a fixed array more than once.
Remove or rename one of the arrays.
Search for the duplicate name. When specifying the name to search for, omit any type-declaration character because a conflict occurs if the names are the same and the type-declaration characters are different.
Note that a module-level variable can have the same name as a variable declared in a procedure, but when you want to refer to the module-level variable within the procedure, you must qualify it with the module name. Module names and the names of referenced projects can be reused as variable names within procedures and can also be qualified.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).
[!includeSupport and feedback]
I’m having an issue with a VBA program I made. I want to create a program which inputs 50,000 records throughout a table (which is the Employee Table in my case), and every time I try to run it, it states an error that says "Compile Error: Duplicate declaration in current scope."
My code is as follows:
Option Compare Database
Option Explicit
Sub arrayData1()
'This subroutine will pump in 50 k records for the first two columns of EMPLOYEE table.
'Also takes in sample names, attempts to clean the data beofre its entered in the table.
'Declare variable by using keyword DIM
Dim EmployeeFNames() As Variant 'implies array. array is always declared variant datatype.
Dim EmployeeLNames() As Variant
Dim EmployeeType() As Variant
Dim num As Integer, dbs As Database, InsertRecord As Variant, num1 As Long
Dim EmployeeID As Long, EmployeeFName As String, EmployeeLName As String, EmployeeType As String, EmployeeWages As Long
'assign value to variables
Set dbs = CurrentDb() 'assign current db(Stage 2 Project)
EmployeeID = 0 'initialise value.
For num1 = 0 To 50000
EmployeeID = EmployeeID + 1 'increment by 1.
EmployeeWages = EmployeeWages + 1
' array is populated with names.
EmployeeFNames = Array("Peter", "Mary", "Frances", "Paul", "Ian", "Ron", "Nathan", "Jesse", "John", "David")
EmployeeLNames = Array("Jacobs", "Smith", "Zane", "Key", "Doe", "Patel", "Chalmers", "Simpson", "Flanders", "Skinner")
EmployeeTypes = Array("Groundskeeper", "Housekeeper", "Concierge", "Front Desk", "Chef", "F&B", "Maintenance", "Accounts", "IT", "Manager")
'Equation for random generation
'INT (upperbound - lowerbound +1) * Rnd + lowerbound) ' upper & lower bound are index values of array
num = Int((9 - 0 + 1) * Rnd + 0) ' equation generates at random a number between 0 & 9.
EmployeeFName = EmployeeFNames(num) ' name is picked at random from array based on random number.
EmployeeLName = EmployeeLNames(num)
EmployeeType = EmployeeTypes(num)
' Use SQL INSERT statement to insert record in EPLOYEE table.
InsertRecord = "INSERT INTO EMPLOYEE(EmployeeID, EmployeeFName, EmployeeLName, EmployeeType, EmployeeWages) VALUES(" _
& "'" & EmployeeID & "'" & "," & "'" & EmployeeFName & "'" & "," & "'" & EmployeeLName & "'" & "," & "'" & EmployeeType & "'" & "," & "'" & EmployeeWages & "'" & ")"
dbs.Execute InsertRecord
Debug.Print EmployeeID; EmployeeFName; EmployeeLName; EmployeeType; EmployeeWages
Next
End Sub
I would appreciate any fixes to this problem and any suggestions towards my code.
|
янеты Пользователь Сообщений: 21 |
#1 15.04.2017 12:17:02 Крепкого Всем Здоровья! Можно ли помочь ученику? В макросе есть две такие штучки, поотдельности каждая работает, вместе пишет: Duplicate declaration in current scope. Поиск по форуму и инету никак не хочет помогать:). Заранее благодарен всем.
|
||
|
RAN Пользователь Сообщений: 7081 |
Двойное объявление в текущей области действия И, кстати, в том обрывке кода, который вы приложили, этой ошибки не будет. |
|
янеты Пользователь Сообщений: 21 |
Как бы я этого хотел! Есть эта ошибка если два этих действия делать подряд в одном макросе! Из инета понял надо что то переименовать вроде, но что и где, моего умишка не хватает… |
|
JayBhagavan Пользователь Сообщений: 11833 ПОЛ: МУЖСКОЙ | Win10x64, MSO2019x64 |
#4 15.04.2017 12:57:51
вырезать и вставить перед:
Оформляйте код кнопкой <…> <#0> |
||||
|
янеты Пользователь Сообщений: 21 |
#5 15.04.2017 13:24:27 Что надо изменить чтоб работало? Заранее спасибо.
Изменено: янеты — 15.04.2017 14:49:44 |
||
|
JayBhagavan Пользователь Сообщений: 11833 ПОЛ: МУЖСКОЙ | Win10x64, MSO2019x64 |
#6 15.04.2017 13:27:46
1) оформить код как полагается; <#0> |
||
|
RAN Пользователь Сообщений: 7081 |
#7 15.04.2017 13:31:10
Эту часть
Вам слово Duplicate ничего не говорит? Я даже перевел Гуглом. Изменено: RAN — 15.04.2017 21:17:48 |
||||
|
янеты Пользователь Сообщений: 21 |
Извините меня не опытного, я не знаю как правильно оформлять код, только учусь писать макрос, (для меня большой Изменено: янеты — 15.04.2017 14:58:26 |
|
JayBhagavan Пользователь Сообщений: 11833 ПОЛ: МУЖСКОЙ | Win10x64, MSO2019x64 |
янеты, я Вам про код в сообщении форума. Посмотрите как у меня код оформлен и сделайте также при помощи кнопки на панели формы ответов форума, о которой я Вам ранее говорил и показал, как она выглядит. Это никак не связано с Вашим умением писать макросы. <#0> |
|
янеты Пользователь Сообщений: 21 |
Я тоже перевел, и в название темы вынес. Из за неопытности не понимаю как один дубликат переделать до рабочего состояния … Если это нельзя сделать буду искать другие решения… |
|
Юрий М Модератор Сообщений: 60335 Контакты см. в профиле |
#11 15.04.2017 14:31:47
Для того, чтобы оформить код должным образом, умения писать макросы не нужно — ищите такую кнопку и исправьте своё сообщение. Прикрепленные файлы
|
||
|
янеты Пользователь Сообщений: 21 |
#12 15.04.2017 14:32:04
|
||
|
RAN Пользователь Сообщений: 7081 |
#13 15.04.2017 14:33:02
Конечно нельзя, ибо код вы не показываете.
Никак. Их быть не должно. |
||||
|
янеты Пользователь Сообщений: 21 |
иду по такому пути Прикрепленные файлы
|
|
Михаил Лебедев Пользователь Сообщений: 2838 |
#15 15.04.2017 15:26:45 А где файл с макросом?
или так?
или вот так?
Изменено: Михаил Лебедев — 15.04.2017 15:29:07 Всё сложное — не нужно. Всё нужное — просто /М. Т. Калашников/ |
||||||||
|
янеты Пользователь Сообщений: 21 |
#16 15.04.2017 16:24:43 УРА!!! Заработало!
Изменено: янеты — 15.04.2017 16:39:52 |
||
|
RAN Пользователь Сообщений: 7081 |
#17 15.04.2017 20:02:19 Михаил Лебедев, |
I just compiled this together it was working fine till i added more ElsIf’s
Error says:
«Compile error: Duplicate declaration in current scope»Code (pardon it a bit long):
[VBA] Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const WS_RANGE2 As String = «B216,B419,B622,B825,B1028,B1231,B1434,B1637,B1840,B2043,B2246,B2449,B2652, B2855,B3058,B3261,B3464,B3667,B3870,B4073,B4276,B4479,B4682,B4885,B5088»
Const WS_RANGE1 As String = «B15,B421,B624,B827,B1030,B1233,B1436,B1639,B1842,B2045,B2248,B2451,B2654,B 2857,B3060,B3263,B3466,B3669,B3872,B4075,B4278,B4481,B4684,B4887,B5090»
Const WS_RANGE2 As String = «B17,B220,B626,B829,B1032,B1235,B1438,B1641,B1844,B2047,B2250,B2453,B2656,B 2859,B3062,B3265,B3468,B3671,B3874,B4077,B4280,B4483,B4686,B4889,B5092»
Const WS_RANGE3 As String = «B19,B222,B425,B831,B1034,B1237,B1440,B1643,B1846,B2049,B2252,B2455,B2658,B 2861,B3064,B3267,B3470,B3673,B3876,B4079,B4282,B4485,B4688,B4891,B5094»
Const WS_RANGE3 As String = «B21,B224,B427,B630,B1036,B1239,B1442,B1645,B1848,B2051,B2254,B2457,B2660,B 2863,B3066,B3269,B3472,B3675,B3878,B4081,B4284,B4487,B4690,B4893,B5096»
Const WS_RANGE3 As String = «B23,B226,B429,B632,B835,B1241,B1444,B1647,B1850,B2053,B2256,B2459,B2662,B2 865,B3068,B3271,B3474,B3677,B3880,B4083,B4286,B4489,B4692,B4895,B5098»
Const WS_RANGE3 As String = «B25,B228,B431,B634,B837,B1040,B1446,B1649,B1852,B2055,B2258,B2461,B2664,B2 867,B3070,B3273,B3476,B3679,B3882,B4085,B4288,B4491,B4694,B4897,B5100»
Const WS_RANGE3 As String = «B27,B230,B433,B636,B839,B1042,B1245,B1651,B1854,B2057,B2260,B2463,B2666,B2 869,B3072,B3275,B3478,B3681,B3884,B4087,B4290,B4493,B4696,B4899,B5102»
Const WS_RANGE3 As String = «B29,B232,B435,B638,B841,B1044,B1247,B1450,B1856,B2059,B2262,B2465,B2668,B2 871,B3074,B3277,B3480,B3683,B3886,B4089,B4292,B4495,B4698,B4901,B5104»
Const WS_RANGE3 As String = «B31,B234,B437,B640,B843,B1046,B1249,B1452,B1655,B2061,B2264,B2467,B2670,B2 873,B3076,B3279,B3482,B3685,B3888,B4091,B4294,B4497,B4700,B4903,B5106»
Const WS_RANGE3 As String = «B33,B236,B439,B642,B845,B1048,B1251,B1454,B1657,B1860,B2266,B2469,B2672,B2 875,B3078,B3281,B3484,B3687,B3890,B4093,B4296,B4499,B4702,B4905,B5108»
Const WS_RANGE3 As String = «B35,B238,B441,B644,B847,B1050,B1253,B1456,B1659,B1862,B2065,B2471,B2674,B2 877,B3080,B3283,B3486,B3689,B3892,B4095,B4298,B4501,B4704,B4907,B5110»
Const WS_RANGE3 As String = «B37,B240,B443,B646,B849,B1052,B1255,B1458,B1661,B1864,B2067,B2270,B2676,B2 879,B3082,B3285,B3488,B3691,B3894,B4097,B4300,B4503,B4706,B4909,B5112»
Const WS_RANGE3 As String = «B39,B242,B445,B648,B851,B1054,B1257,B1460,B1663,B1866,B2069,B2272,B2475,B2 881,B3084,B3287,B3490,B3693,B3896,B4099,B4302,B4505,B4708,B4911,B5114»
Const WS_RANGE3 As String = «B41,B244,B447,B650,B853,B1056,B1259,B1462,B1665,B1868,B2071,B2274,B2477,B2 680,B3086,B3289,B3492,B3695,B3898,B4101,B4304,B4507,B4710,B4913,B5116»
Const WS_RANGE3 As String = «B43,B246,B449,B652,B855,B1058,B1261,B1464,B1667,B1870,B2073,B2276,B2479,B2 682,B2885,B3291,B3494,B3697,B3900,B4103,B4306,B4509,B4712,B4915,B5118»
Const WS_RANGE3 As String = «B45,B248,B451,B654,B857,B1060,B1263,B1466,B1669,B1872,B2075,B2278,B2481,B2 684,B2887,B3090,B3496,B3699,B3902,B4105,B4308,B4511,B4714,B4917,B5120»
Const WS_RANGE3 As String = «B47,B250,B453,B656,B859,B1062,B1265,B1468,B1671,B1874,B2077,B2280,B2483,B2 686,B2889,B3092,B3295,B3701,B3904,B4107,B4310,B4513,B4716,B4919,B5122»
Const WS_RANGE3 As String = «B49,B252,B455,B658,B861,B1064,B1267,B1470,B1673,B1876,B2079,B2282,B2485,B2 688,B2891,B3094,B3297,B3500,B3906,B4109,B4312,B4515,B4718,B4921,B5124»
Const WS_RANGE3 As String = «B51,B254,B457,B660,B863,B1066,B1269,B1472,B1675,B1878,B2081,B2284,B2487,B2 690,B2893,B3096,B3299,B3502,B3705,B4111,B4314,B4517,B4720,B4923,B5126»
Const WS_RANGE3 As String = «B53,B256,B459,B662,B865,B1068,B1271,B1474,B1677,B1880,B2083,B2286,B2489,B2 692,B2895,B3098,B3301,B3504,B3707,B3910,B4316,B4519,B4722,B4925,B5128»
Const WS_RANGE3 As String = «B55,B258,B461,B664,B867,B1070,B1273,B1476,B1679,B1882,B2085,B2288,B2491,B2 694,B2897,B3100,B3303,B3506,B3709,B3912,B4115,B4521,B4724,B4927,B5130»
Const WS_RANGE3 As String = «B57,B260,B463,B666,B869,B1072,B1275,B1478,B1681,B1884,B2087,B2290,B2493,B2 696,B2899,B3102,B3305,B3508,B3711,B3914,B4117,B4320,B4726,B4929,B5132»
Const WS_RANGE3 As String = «B59,B262,B465,B668,B871,B1074,B1277,B1480,B1683,B1886,B2089,B2292,B2495,B2 698,B2901,B3104,B3307,B3510,B3713,B3916,B4119,B4322,B4525,B4931,B5134»
Const WS_RANGE3 As String = «B61,B264,B467,B670,B873,B1076,B1279,B1482,B1685,B1888,B2091,B2294,B2497,B2 700,B2903,B3106,B3309,B3512,B3715,B3918,B4121,B4324,B4527,B4730,B5136»
Const WS_RANGE3 As String = «B63,B266,B469,B672,B875,B1078,B1281,B1484,B1687,B1890,B2093,B2296,B2499,B2 702,B2905,B3108,B3311,B3514,B3717,B3920,B4123,B4326,B4529,B4732,B4935»If Not Intersect(Target, Me.Range(WS_RANGE1)) Is Nothing Then
With Target
Range(«B13»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B218»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B628»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B833»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B1038»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B1243»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B1448»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B1653»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B1858»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B2063»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B2268»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B2473»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B2678»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B2883»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B3088»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B3293»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B3498»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B3703»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B3908»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B4113»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B4318»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B4523»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B4728»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B4933»).Select
End With
ElseIf Not Intersect(Target, Me.Range(WS_RANGE2)) Is Nothing Then
With Target
Range(«B5138»).Select
End WithEnd If
End Sub[/VBA]THE VBA help log says:
The specified name is already used at this level of scope. For example, two variables can have the same name if they are defined in different procedures, but not if they are defined within the same procedure. This error has the following causes and solutions:
- A new variable or procedure has the same name as an existing variable or procedure. For example: [vba]Sub MySub()
Dim A As Integer
Dim A As Variant
. . . ‘ Other declarations or procedure code here.
End Sub
[/vba]Check the current procedure, module, or project and remove any duplicate declarations.- A Const statement uses the same name as an existing variable or procedure. Remove or rename the constant in question.
- You declared a fixed array more than once. Remove or rename one of the arrays.
Search for the duplicate name. When specifying the name to search for, omit any type-declaration character because a conflict occurs if the names are the same and the type-declaration characters are different.
Note that a module-level variable can have the same name as a variable declared in a procedure, but when you want to refer to the module-level variable within the procedure, you must qualify it with the module name. Module names and the names of referenced projects can be reused as variable names within procedures and can also be qualified.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).I was Referd to this type of code:
[vba]
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Goto errH
Application.EnableEvents = False
Select Case Target.Address(0, 0)
Case «B15»
Range(«B218»).Select
Case «B17»
Call Range(«B219»).Select ‘<== macro2(target)
Case Else
Goto errH
End Select
errH:
Application.EnableEvents = True
End Sub
[/vba]
but couldnt get it to workps. thanks for looking, any ideas love to here um.
MERKY
New Member
- Joined
- Apr 29, 2002
- Messages
- 38
-
#1
this s part of a larger formula:
Dim c
For Each c In Range(«C1:C» & x)
If c = «» Then
MsgBox «Please enter data in cell » & c.Address
c.Select
Exit Sub
End If
Dim x As Integer, counter As Integer, y As Integer, MyArr
Randomize
MyArr = Array(4, 8, 16, 32, 64, 128, 256)
when i try and execute the macro i get this message «Compile error : Duplicate declaration in current scope». And also the line «Dim x as Interger» get highlited.
How should i stop this from happening?
This message was edited by MERKY on 2002-05-02 10:54
