Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA 2010:我不断收到一个声明错误:编译错误:当前范围中存在重复声明_Vba_Loops_Compilation - Fatal编程技术网

Excel VBA 2010:我不断收到一个声明错误:编译错误:当前范围中存在重复声明

Excel VBA 2010:我不断收到一个声明错误:编译错误:当前范围中存在重复声明,vba,loops,compilation,Vba,Loops,Compilation,我对VBA很陌生。循环总是给我带来麻烦。我只是不能把我的头围绕在它们周围(所以,如果有人知道学习循环的好来源,我洗耳恭听)。无论如何,我已经从过去的一个项目中构建了下面的代码,我曾与一位开发人员合作过。我试着从他教我的东西中尽可能多地进行逆向工程——但说实话,有些我不明白。我只想获得表“项目分析”中与表“细分”范围C2中的项目编号匹配的所有记录,如果范围F大于范围E,并且如果两者之间的差异大于1.5,则从“项目分析”中获取员工姓名(D)、分配的FTE(E)和计费的FTE(F)并从范围32开始将其

我对VBA很陌生。循环总是给我带来麻烦。我只是不能把我的头围绕在它们周围(所以,如果有人知道学习循环的好来源,我洗耳恭听)。无论如何,我已经从过去的一个项目中构建了下面的代码,我曾与一位开发人员合作过。我试着从他教我的东西中尽可能多地进行逆向工程——但说实话,有些我不明白。我只想获得表“项目分析”中与表“细分”范围C2中的项目编号匹配的所有记录,如果范围F大于范围E,并且如果两者之间的差异大于1.5,则从“项目分析”中获取员工姓名(D)、分配的FTE(E)和计费的FTE(F)并从范围32开始将其填充为“细分”(中间有一个透视表)

编译错误发生在第二个循环开始之前的“Dim StudyFind as integer”处

代码如下:

Option Explicit

Sub EmployeeBill()

Dim i As Long
Dim j As Long
Dim RecFound As Long
Dim LastRec As Long
Dim CollVar() As String             'Collect Labor/Bill data from Project Analysis
Dim StaffStudyInfo() As String      'Collect unique project number data
Dim StudyRec() As String            'Collect Unique Project name per Project Number
Dim StudyFound As Integer             'Collect number of valid records
Dim UniqueInfoFound As Long         'Collect number of valid records

'Collect Project ID, Associate, Assigned FTE, and Billed FTE records from Project Analysis tab
ReDim CollVar(LastRec, 4) 'Array to collect 4  data elements
LastRec = ActiveCell.SpecialCells(xlLastCell).Row - 1
Sheets("Project Analysis").Select
RecFound = 0
For i = 1 To LastRec
    If ActiveCell.Offset(i, 3).Value = Sheets("Breakdown").Range("C2").Value Then
        RecFound = RecFound + 1
        CollVar(RecFound, 1) = ActiveCell.Offset(i, 3).Value 'Collect Project ID
        CollVar(RecFound, 2) = ActiveCell.Offset(i, 4).Value 'Collect Associate
        CollVar(RecFound, 3) = ActiveCell.Offset(i, 5).Value 'Collect Assigned FTE
        CollVar(RecFound, 4) = ActiveCell.Offset(i, 6).Value 'Collect Billed FTE
    End If
Next i

'Collect Associates who billed 1.5 times their FTE Assigned value
ReDim StaffStudyInfo(4, RecFound)
**Dim StudyFound As Integer**
UniqueInfoFound = 0
For i = 1 To RecFound
    StudyFound = 0
    For j = 1 To UniqueInfoFound
        If StaffStudyInfo(1, j) = CollVar(i, 1) Then
            If CollVar(i, 6).Value > CollVar(i, 5) Then
                If CollVar(i, 6) - CollVar(i, 5) > 1.5 Then
                    StudyFound = 1
                    Exit For
                End If
            End If
        End If
    Next j
    If StudyFound = 0 Then
        UniqueInfoFound = UniqueInfoFound + 1
        StaffStudyInfo(1, UniqueInfoFound) = CollVar(i, 4) 'Associate
        StaffStudyInfo(2, UniqueInfoFound) = CollVar(i, 5) 'Assigned FTE
        StaffStudyInfo(3, UniqueInfoFound) = CollVar(i, 6) 'Billed FTE
    End If
Next i

'Populate all that meet above criteria onto "Breakdown" sheet
Sheets("Breakdown").Select
    For i = 1 To UniqueInfoFound
        Range("C" & i + 4) = StaffStudyInfo(1, i)   'Populate Associate
        Range("D" & i + 4) = StaffStudyInfo(2, i)   'Populate Assigned FTE
        Range("E" & i + 4) = StaffStudyInfo(3, i)   'Populate Billed FTE
    Next i
Range("A1").Select
End sub     

任何帮助都将不胜感激。谢谢大家!

您已经在顶部附近定义了
studyfind

...
Dim StudyRec() As String            'Collect Unique Project name per Project Number
Dim StudyFound As Integer             'Collect number of valid records
Dim UniqueInfoFound As Long         'Collect number of valid records
...

删除其中一个定义就可以了。

谢谢。明天早上我会试试。好吧,我试着把第一个声明评论出来。我没有出错,但什么也没发生。你能告诉我我在代码中哪里出错了吗?正如我所说,我试图对与应用程序开发人员合作的项目中的代码进行反向工程。我真的很想知道我出了什么问题。谢谢。我会将上一个循环中的
范围
s更改为
范围(“C”+(I+4))
&
设计用于将字符串连接到字符串,我不确定运算符的优先级。好的,我这样做了(并将其更改为第31行,因为上面有一个透视表。我没有收到任何错误,但也没有发生任何事情。正如我所说,我对外观很糟糕。我想知道是否对关闭的数组做了什么。