Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
Vba 有没有办法让它不那么烦人/正常工作?_Vba_Excel - Fatal编程技术网

Vba 有没有办法让它不那么烦人/正常工作?

Vba 有没有办法让它不那么烦人/正常工作?,vba,excel,Vba,Excel,我有一个函数,可以根据唯一ID检查当前记录下的行。当前记录(循环变量=I)下最多可以有6个唯一的想法,与循环中正在检查的当前记录相匹配。完成此操作后,将检查下面的记录是否存在特定条件(循环变量x)。然而,出于某种原因,我遇到了几个问题。首先,我必须在两个循环内设置范围引用,否则会出错。第二个是,x循环之后的所有内容似乎都在它之前的i循环中输出。我做错了什么,我如何才能使这个功能正常 请在下面找到我的代码: Function First_check() dim i as long, x as lo

我有一个函数,可以根据唯一ID检查当前记录下的行。当前记录(循环变量=I)下最多可以有6个唯一的想法,与循环中正在检查的当前记录相匹配。完成此操作后,将检查下面的记录是否存在特定条件(循环变量x)。然而,出于某种原因,我遇到了几个问题。首先,我必须在两个循环内设置范围引用,否则会出错。第二个是,x循环之后的所有内容似乎都在它之前的i循环中输出。我做错了什么,我如何才能使这个功能正常

请在下面找到我的代码:

Function First_check()
dim i as long, x as long
Dim numComponents As Variant
Dim in1 As Range, in2 As Range, in3 As Range, in4 As Range, in5 As Range, _
in6 As Range, in7 As Range, in8 As Range, in9 As Range, in10 As Range, _
in11 As Range, in12 As Range, in13 As Range, in14 As Range, in15 As Range, _
in16 As Range, in17 As Range, in18 As Range, in19 As Range, in20 As Range
Dim out1 As Range, out2 As Range, out3 As Range, out4 As Range, out5 As Range, _
out6 As Range, out7 As Range, out8 As Range, out9 As Range, out10 As Range, _
out11 As Range, out12 As Range, out13 As Range, out14 As Range, out15 As Range, _
out16 As Range, out17 As Range, out18 As Range, out19 As Range, out20 As Range
Dim str, msg, oft, BTG, LOB, pdf, mht, emails, zip_rar, xls, doc, xls_doc, mrTT, lobVal, cmt1, giveURL, giveURLm As String

lastRow = Cells(Rows.Count, 1).End(xlUp).Row
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column

For i = 2 To lastRow
 If Cells(i, 5).Value2 = Cells(i + 6, 5).Value2 Then 
     numComponents = 6
 ElseIf Cells(i, 5).Value2 = Cells(i + 5, 5).Value2 Then
     numComponents = 5
 ElseIf Cells(i, 5).Value2 = Cells(i + 4, 5).Value2 Then
     numComponents = 4
 ElseIf Cells(i, 5).Value2 = Cells(i + 3, 5).Value2 Then
     numComponents = 3
 ElseIf Cells(i, 5).Value2 = Cells(i + 2, 5).Value2 Then
     numComponents = 2
 ElseIf Cells(i, 5).Value2 = Cells(i + 1, 5).Value2 Then
     numComponents = 1
 Else
     numComponents = 0
 End If

 For x = i + 1 To i + numComponents

    Set in1 = Cells(i, 11) 'test
    Set in2 = Cells(i, 12) 
    Set in3 = Cells(i, 13) 
    Set in4 = Cells(i, 16) 'e
    Set in5 = Cells(i, 37) 'target date 
    Set in6 = Cells(i, 38) 'target date end
    Set in7 = Cells(i, 35) 'target date actual 
    Set in8 = Cells(i, 37) 'target date start
    Set in9 = Cells(i, 38) 'target date end
    Set in10 = Cells(x, 50) ' date start
    Set in11 = Cells(x, 51) ' date end
    Set in12 = Cells(i, 42) 'pro
    Set in13 = Cells(i, 43) 'reco
    Set in14 = Cells(x, 62) 'cert
    Set in15 = Cells(x, 63) 'com
    Set in16 = Cells(x, 64) 'comp
    Set in17 = Cells(x, 49) 'uniqueID
    'outs
    Set out1 = Cells(i, 72) 'test
    Set out2 = Cells(i, 73) '
    Set out3 = Cells(i, 74) '
    Set out4 = Cells(i, 75) 'e
    Set out5 = Cells(i, 76) 'tar
    Set out6 = Cells(i, 77) 'comp
    Set out7 = Cells(i, 78) 'pro
    Set out8 = Cells(i, 75) 'empty
    Set out9 = Cells(i, 80) 'cer
    Set out10 = Cells(i, 81) 'comp
    Set out11 = Cells(i, 85) 'pre
    Set out12 = Cells(i, 88) 'missing
    Set out13 = Cells(i, 89) 'missing2
    Set out14 = Cells(i, 71) 'uniqueID
    '------ATTACHMENT SET
    str = Cells(i, 46).Value2
    msg = UBound(Split(str, ".msg"))
    oft = UBound(Split(str, ".oft"))
    BTG = UBound(Split(str, "BTG"))
    LOB = UBound(Split(str, "LOB"))
    pdf = UBound(Split(str, ".pdf"))
    mht = UBound(Split(str, ".mht"))
    emails = msg + oft + pdf + mht
    zip_rar = UBound(Split(str, ".zip"))
    xls = UBound(Split(str, ".xls"))
    doc = UBound(Split(str, ".doc"))
    xls_doc = xls Or doc

    If (in8.Value2 <> in10.Value2) Or (in9.Value <> in11.Value2) Then 'date
        out6.Value2 = Cells(x, 49).Value2 & ", " & out6.Value2
    End If

    If IsBlank(in14.Value2) Then 'Check cer
        out9.Value2 = Cells(x, 49).Value2 & ", " & out9.Value2
    End If

    If IsBlank(in15.Value2) Or IsBlank(in16.Value2) Then 'check loc
        out10.Value2 = Cells(x, 49).Value2 & ", " & out10.Value2
    End If

    If Not IsBlank(in17.Value2) Then
        out14.Value2 = in17.Value2 & ", " & out14.Value2
    End If

Next x

If Not IsBlank(out6.Value2) Then 'date
    out6.Value2 = "Wrong dates"
    out6.Value2 = fixtrail(out6.Value2)
End If

 If Not IsBlank(out9.Value2) Then 'cert
    out9.Value2 = "Cert Issue"
    out9.Value2 = fixtrail(out9.Value2)
 End If

 If Not IsBlank(out10.Value2) Then 'comp
    out10.Value2 = "Comp not found"
    out10.Value2 = fixtrail(out10.Value2)
 End If

 If IsBlank(in1.Value2) Then
    out1.Value2 = "Missing type"
 End If


'
'many more checks happening that i omittied for brevity
'


  If numComponents = 0 Then
    Cells(i, 70).Value2 = "0"
Else
    Cells(i, 70).Value2 = numComponents
End If


 i = i + numComponents

Next i
End Function
函数优先检查()
暗i等于长x等于长
Dim numComponents作为变体
在1范围内变暗,在2范围内变暗,在3范围内变暗,在4范围内变暗,在5范围内变暗_
in6为量程,in7为量程,in8为量程,in9为量程,in10为量程_
11为量程,12为量程,13为量程,14为量程,15为量程_
16为量程,17为量程,18为量程,19为量程,20为量程
调暗out1为量程,out2为量程,out3为量程,out4为量程,out5为量程_
out6为量程,out7为量程,out8为量程,out9为量程,out10为量程_
out11为量程,out12为量程,out13为量程,out14为量程,out15为量程_
out16为量程,out17为量程,out18为量程,out19为量程,out20为量程
Dim str、msg、oft、BTG、LOB、pdf、mht、电子邮件、zip_-rar、xls、doc、xls_-doc、mrTT、lobVal、cmt1、giveURL、giveURLm作为字符串
lastRow=单元格(Rows.Count,1).End(xlUp).Row
lastCol=单元格(1,Columns.Count).End(xlToLeft).Column
对于i=2到最后一行
如果单元格(i,5).Value2=单元格(i+6,5).Value2,则
numComponents=6
ElseIf Cells(i,5).Value2=Cells(i+5,5).Value2
numComponents=5
ElseIf Cells(i,5).Value2=Cells(i+4,5).Value2
numComponents=4
ElseIf Cells(i,5).Value2=Cells(i+3,5).Value2
numComponents=3
ElseIf Cells(i,5).Value2=Cells(i+2,5).Value2
numComponents=2
ElseIf Cells(i,5).Value2=Cells(i+1,5).Value2
numComponents=1
其他的
numComponents=0
如果结束
对于x=i+1到i+num组件
设置为1=单元(i,11)'测试
设置in2=单元(i,12)
设置为3=单元(i,13)
设置为4=单元(i,16)'e
在5中设置=单元格(i,37)“目标日期”
在6中设置=单元格(i,38)“目标日期结束”
在7中设置=单元格(i,35)“实际目标日期”
在8中设置=单元格(i,37)“目标日期开始”
在9中设置=单元格(i,38)“目标日期结束”
设置为10=单元格(x,50)“开始日期”
设置为11=单元格(x,51)“日期结束”
设置为12=单元格(i,42)'pro
设置为13=单元格(i,43)'reco
设置为14=单元格(x,62)'证书
设置为15=单元格(x,63)'com
设置为16=单元格(x,64)'comp
设置为17=单元(x,49)“唯一ID”
“出局
放线1=单元(i,72)'测试
放线2=单元(i,73)'
放线3=单元(i,74)'
放线4=单元(i,75)'e
放线5=单元(i,76)'tar
放线6=单元(i,77)'comp
设置7=单元(i,78)'pro
设置8=单元(i,75)“空”
放线9=单元(i,80)'cer
放线10=单元(i,81)'comp
设置11=单元(i,85)'预
设置12=单元(i,88)“缺失”
设置13=单元(i,89)“缺失2”
设置14=单元(i,71)“唯一ID”
'----附件集
str=细胞(i,46)。值2
msg=UBound(拆分(str,“.msg”))
oft=UBound(拆分(str,.oft”))
BTG=UBound(拆分(str,“BTG”))
LOB=UBound(拆分(str,LOB)))
pdf=UBound(拆分(str,.pdf”))
mht=UBound(拆分(str,.mht”))
电子邮件=msg+oft+pdf+mht
zip_rar=UBound(拆分(str,.zip)))
xls=UBound(拆分(str,.xls)))
doc=UBound(拆分(str,“.doc”))
xls_doc=xls或doc
如果(10.Value2中的8.Value2)或(11.Value2中的9.Value2),则“日期”
out6.Value2=单元格(x,49)。Value2&“,”和out6.Value2
如果结束
如果为空(在14.Value2中),则“检查cer”
out9.Value2=单元格(x,49).Value2&“,”&out9.Value2
如果结束
如果IsBlank(在15.Value2中)或IsBlank(在16.Value2中),则“check loc”
out10.Value2=单元格(x,49).Value2&“,”&out10.Value2
如果结束
如果Not为空(在17.Value2中),则
out14.Value2=in17.Value2&“,”和out14.Value2
如果结束
下一个x
如果不为空(out6.Value2),则为“日期”
out6.Value2=“日期错误”
out6.Value2=固定轨迹(out6.Value2)
如果结束
如果不为空(out9.Value2),则“证书”
out9.Value2=“证书颁发”
out9.Value2=固定轨迹(out9.Value2)
如果结束
如果不为空(out10.Value2),则为“comp”
out10.Value2=“未找到组件”
out10.Value2=固定轨迹(out10.Value2)
如果结束
如果为空(在1.Value2中),则
out1.Value2=“缺少类型”
如果结束
'
“为了简洁起见,我省略了更多的检查
'
如果numComponents=0,则
单元格(i,70)。值2=“0”
其他的
单元格(i,70).Value2=numComponents
如果结束
i=i+numComponents
接下来我
端函数

想到的第一个想法是使用
Range
对象数组来清理变量声明:

Dim inRange(20) As Range
Dim outRange(20) As Range

'...

For x = i + 1 To i + numComponents
    Set inRange(1) = Cells(i, 11)
    Set inRange(2) = Cells(i, 12)
    '...
Next
如果可以得到映射到每个数组位置的单元格编号的公式,那么这将非常有效

此外,我们还可以围绕两个循环的嵌套方式改进变量。外循环使用
i
变量,而内循环使用
x
变量。由于它们都在查看行,我将它们重新命名为
r0
r1
(或
rBase
rNested
rParent
rChild
rMaster
rdeail
,等等),以帮助您了解每个索引的内容。我还看到一些范围对象依赖于当前的
I
值,而另一些依赖于
x
。您应该能够在内环上方分配
i
范围,并通过这种方式节省一些CPU/内存工作:

For irParent = 2 To LastRow

    '...

    Set inRange(1) = Cells(irParent, 11) 'test
    Set inRange(2) = Cells(irParent, 12) 
    Set inRange(3) = Cells(irParent, 13) 
    Set inRange(4) = Cells(irParent, 16) 'e
    '...

    'If numComponents is 0, there are no child rows and this loop is skipped
    For rChild = rParent + 1 To rParent + numComponents
        Set inRange(10) = Cells(irChild, 50) ' date start
        Set inRange(11) = Cells(irChild, 51) ' date end

        '...

        str = Cells(irParent, 46).Value2
        msg = UBound(Split(str, ".msg"))
        oft = UBound(Split(str, ".oft"))
        '...

    Next

    irParent = irParent + numComponents
Next
另一件事是