我对VBA很新。循环总是给我带来麻烦。我似乎无法绕过它们(所以,如果有人知道学习循环的好来源,那么我的所有耳朵都是如此)。无论如何,我已经从我与开发人员合作的过去项目中构建了下面的代码。我尽可能地从他教给我的东西中进行逆向工程 - 但实际上,其中一些我不明白。我只想在表单中获取所有记录" Project Analysis"与表格中的项目编号匹配"细分"范围C2,如果范围F大于范围E,则匹配这些标准,如果两者之间的差异大于1.5,则取辅助名称(D),从&分配FTE(E)和Billed FTE(F) #34;项目分析"并将它们填充到" Breakdown"从范围32开始(其间有一个数据透视表)。
编译错误发生在" Dim StudyFound为整数"就在第二次循环开始之前。
以下是代码:
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
非常感谢任何帮助。谢谢!
答案 0 :(得分:0)
您已经在顶部附近定义了StudyFound
:
...
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
...
删除其中一个定义,应该没问题。