我有一个报告,该报告当前可以为单个component_cd拉出component_cd。我被要求为多个constiuent_cds运行报告。因此,我需要将此公式转换为VBA。我被困在Do While Loop
中。 -解决了一个问题,创造了一个新的问题。现在,我的拳头磨损流程拳头已经过去了。
我包括了工作数据输出和工作公式的图像,以及我的VBA代码(已更新)。我知道我已经搞砸了Do While loop
,因为其他所有东西都已经过测试并且可以正常工作。有人可以帮我解开这个循环吗?
[成分表] [1]
工作公式:
=IF((VALUE(LEFT(OFFSET(INDIRECT(CELL("address",INDEX(ComponentData!D:D,MATCH('START-DATA_State'!$C$2,ComponentData!D:D,0),1)),TRUE),H2+1,0),LEN($E$2)))=$C$2),OFFSET(INDIRECT(CELL'("address",INDEX(ComponentData!D:D,MATCH('START-DATA_State'!'$C$2,ComponentData!D:D,0),1)),TRUE),H2+1,0),"")
len_constituent_cd_for_finding_component_cd = LEN(IF(OR('START-DATA_State'!$C$2<5,'START-DATA_State'!$C$2=6),TEXT('START-DATA_State'!$C$2,"00"),'START-DATA_State'!$C$2))
=IF((
VALUE(
LEFT(
OFFSET(
INDIRECT(
CELL("address",
INDEX(ComponentData!constituent_state,MATCH('START-DATA_State'!State_Abbreviation,ComponentData!constituent_state,EXACT),1)),TRUE),row_offset_count+1,0),
len_constituent_cd_for_finding_component_cd)) = constituent_cd),
TRUE
OFFSET(
INDIRECT(
CELL("address",
INDEX(ComponentData!constituent_state,
MATCH('START-DATA_State!constituent_cd,ComponentData!constituent_state,EXACT),1)),TRUE),row_offset_count+1,0),
FALSE
'"")
VBA代码:
Dim myLookupValue As String
Dim myFirstColumn As Long
Dim myLastColumn As Long
Dim myColumnIndex As Long
Dim myFirstRow As Long
Dim myLastRow As Long
Dim myVLookupResult As String
Dim intConstituentLength As Integer
Dim strConstituentLength As String
Dim myColumn As Long
Dim myTableArray As Range
Dim myWorkRange As Range
Dim lgth_Consti
Dim i, r, n As Integer
myColumn = 5
myLastRow = Worksheets("START-DATA_State").Range("G2").Value '=count_NonBlankCellsColumn
myColumnIndex = 1
n = 0
r = 0
count_NonBlankCellsColumn
For i = 0 To myLastRow
myConstituentCd = Worksheets("START-DATA_State").Cells(j + 2, 5).Value
intConstituentCd = Worksheets("START-DATA_State").Cells(r + 2, 5).Value
strConstituentCd = CStr(Right("0" & intConstituentCd, 2))
intConstituentLength = Len(intConstituentCd)
strConstituentLength = Len(strConstituentCd)
If (myConstituentCd < 5) Or (myConstituentCd = 6) Then
For n = 0 To WorksheetFunction.CountIf(Worksheets("ComponentData").Range("G2:G643"), strConstituentCd = Left(WorksheetFunction.Index(Worksheets("ComponentData").Range("G2:G643"), WorksheetFunction.Match(strConstituentCd, Worksheets("ComponentData").Range("G2:G643"), 0)), strConstituentLength))
Do While strConstituentCd = Left(WorksheetFunction.Index(Worksheets("ComponentData").Range("G2:G643"), WorksheetFunction.Match(strConstituentCd, Worksheets("ComponentData").Range("G2:G643"), 0)).Offset(rowOffset:=r + 1, columnOffset:=0), strConstituentLength)
If strConstituentLength < Len(WorksheetFunction.Index(Worksheets("ComponentData").Range("G2:G643"), WorksheetFunction.Match(strConstituentCd, Worksheets("ComponentData").Range("G2:G643"), 0)).Offset(rowOffset:=n + 1, columnOffset:=0)) Then
Worksheets("START-DATA_State").Cells(r + 2, 6).Value = WorksheetFunction.Index(Worksheets("ComponentData").Range("G2:G643"), WorksheetFunction.Match(strConstituentCd, Worksheets("ComponentData").Range("G2:G643"), 0)).Offset(rowOffset:=r + 1, columnOffset:=0)
End If
r = r + 1
Loop
Next n
Else
For n = 0 To WorksheetFunction.CountIf(Worksheets("ComponentData").Range("G2:G643"), intConstituentCd = CLng(Left(CStr(WorksheetFunction.Index(Worksheets("ComponentData").Range("G2:G643"), WorksheetFunction.Match(strConstituentCd, Worksheets("ComponentData").Range("G2:G643"), 0))), intConstituentLength)))
Do While intConstituentCd = Left(WorksheetFunction.Index(Worksheets("ComponentData").Range("G2:G643"), WorksheetFunction.Match(strConstituentCd, Worksheets("ComponentData").Range("G2:G643"), 0)).Offset(rowOffset:=r + 1, columnOffset:=0), intConstituentCd)
If strConstituentLength < Len(WorksheetFunction.Index(Worksheets("ComponentData").Range("G2:G643"), WorksheetFunction.Match(strConstituentCd, Worksheets("ComponentData").Range("G2:G643"), 0)).Offset(rowOffset:=n + 1, columnOffset:=0)) Then
Worksheets("START-DATA_State").Cells(r + 2, 6).Value = WorksheetFunction.Index(Worksheets("ComponentData").Range("G2:G643"), WorksheetFunction.Match(strConstituentCd, Worksheets("ComponentData").Range("G2:G643"), 0)).Offset(rowOffset:=r + 1, columnOffset:=0)
End If
r = r + 1
Loop
Next n
End If
j = j + 1
Next i
我无法让i环绕第一次旋转。
答案 0 :(得分:0)
好的,对于需要循环帮助的人,这里的解决方案是有效的:
子findComponent_Cd()
Dim myLookupValue As String
Dim myFirstColumn As Long
Dim myLastColumn As Long
Dim myColumnIndex As Long
Dim myFirstRow As Long
Dim myLastRow As Long
Dim myVLookupResult As String
Dim intConstituentLength As Long
Dim strConstituentLength As String
Dim intComponentCdLft As Long
Dim strComponentCdLft As String
Dim intConstituentCd As Long
Dim strConstituentCd As String
Dim myConstituentCd As Long
Dim myColumn As Long
Dim myCount As Long
Dim myTableArray As Range
Dim myWorkRange As Range
Dim myCell As Range
Dim i, r, n, j As Long
count_NonBlankCellsColumn
myColumn = 5
myLastRow = Worksheets("START-DATA_State").Range("G2").Value '=count_NonBlankCellsColumn
myColumnIndex = 1
n = 0
r = 0
For i = 0 To myLastRow
intConstituentCd = Worksheets("START-DATA_State").Cells(j + 2, 5).Value
strConstituentCd = CStr(Right("0" & intConstituentCd, 2))
intConstituentLength = Len(intConstituentCd)
strConstituentLength = Len(strConstituentCd)
If (intConstituentCd = 5) Or _
(intConstituentCd = 7) Or _
(intConstituentCd = 8) Or _
(intConstituentCd = 30) Or _
(intConstituentCd = 33) Or _
(intConstituentCd = 39) Or _
(intConstituentCd = 47) Or _
(intConstituentCd = 51) Or _
(intConstituentCd = 99) Then
strConstituentCd = intConstituentCd
intConstituentLength = Len(intConstituentCd)
strConstituentLength = intConstituentLength - 3
For Each myCell In Worksheets("ComponentData").Range("G2:G643")
If Len(Left(myCell.Value, strConstituentLength)) = strConstituentLength Then
If strConstituentCd = Left(myCell.Value, strConstituentLength) Then
If Len(myCell.Value) > strConstituentLength Then
If IsNumeric(myCell.Value) Then 'GoTo NextIteration
Worksheets("START-DATA_State").Cells(r + 2, 6).Value = myCell.Value
r = r + 1
End If
End If
End If
End If
Next myCell
End If
If (intConstituentCd < 5) Or (intConstituentCd = 6) Then
If (intConstituentCd <> 5) Or _
(intConstituentCd <> 7) Or _
(intConstituentCd <> 8) Or _
(intConstituentCd <> 30) Or _
(intConstituentCd <> 33) Or _
(intConstituentCd <> 39) Or _
(intConstituentCd <> 47) Or _
(intConstituentCd <> 51) Or _
(intConstituentCd <> 99) Then
For Each myCell In Worksheets("ComponentData").Range("G2:G643")
If Len(Left(myCell.Value, strConstituentLength)) = strConstituentLength Then
If strConstituentCd = Left(myCell.Value, strConstituentLength) Then
Worksheets("START-DATA_State").Cells(r + 2, 6).Value = myCell.Value
r = r + 1
End If
End If
Next myCell
End If
Else
If (intConstituentCd > 8) Then
If (intConstituentCd <> 5) Or _
(intConstituentCd <> 7) Or _
(intConstituentCd <> 8) Or _
(intConstituentCd <> 30) Or _
(intConstituentCd <> 33) Or _
(intConstituentCd <> 47) Or _
(intConstituentCd <> 51) Or _
(intConstituentCd <> 99) Then
strConstituentCd = intConstituentCd
strConstituentLength = Len(strConstituentCd)
For Each myCell In Worksheets("ComponentData").Range("G2:G643")
If Len(Left(myCell.Value, strConstituentLength)) = strConstituentLength Then
If strConstituentCd = Left(myCell.Value, strConstituentLength) Then
Worksheets("START-DATA_State").Cells(r + 2, 6).Value = myCell.Value
r = r + 1
End If
End If
Next myCell
End If
End If
End If
j = j + 1
Next i
结束子