使用Index& amp;更新列的值匹配(Excel / VBA)

时间:2017-08-02 11:51:30

标签: excel excel-vba insert-update vba

Bellow是我的代码的一部分,我的编码几乎已经完成,但现在我发现了一个很大的问题,需要纠正。

我在超过10000行的工作表上使用此代码,我的目的是使用Index& amp;更新列。匹配功能。它正在做的是它将J列中所有范围的单元格替换为另一个工作表中的相应值。最大的问题是我不希望没有匹配项目的单元格被#N / A替换,并保留旧值。

我该怎么办?

Dim SourceRange As Range
Dim fillRange As Range

'bellow formula in A1 notation:
'=INDEX('[myWB.xls]Sheet1'!$A:$B;
'IF(INDEX('[myWB.xls]Sheet1'!$A:$A;MATCH(C4;'[myWB.xls]Sheet1'!$A:$A))=C4;
'    MATCH(C4;'[myWB.xls]Sheet1'!$A:$A);
'    NA());
'    2)

ActiveCell.FormulaR1C1 = _
"=INDEX([myWB.xls]Sheet1!C1:C2,IF(INDEX([myWB.xls]Sheet1!C1,MATCH(R[-11]C[-12],[myWB.xls]Sheet1!C1))=R[-11]C[-12],MATCH(R[-11]C[-12],[myWB.xls]Sheet1!C1),NA()),2)"

' autofill formula in column
Set SourceRange = Cells(2, 10)
Set fillRange = Range(Cells(2, 10), Cells(10000, 10))

With SourceRange
   .AutoFill Destination:=fillRange, Type:=xlCopy
End With

'replace formula with its value
With Range(Cells(2, 10), Cells(10000, 10))
   .Value = .Value
End With

更新

这是我目前的代码,有一些改进。感谢Rob Anthony的帮助。

Dim calc As XlCalculation
Dim f as String

With Workbooks(FinalWB.xls).Worksheets(1)  'Worksheet to be updated
    calc = Application.Calculation
    Application.Calculation = xlCalculationAutomatic  'Ensure automatic calculation is ON
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    'Copy Column J to Column Z
    Range(.Cells(2, 26), .Cells(10000, 26)).Value = _  ' Col Z =
    Range(.Cells(2, 10), .Cells(10000, 10)).Value      ' Col J

    f = "=if(ISERROR(INDEX([myWB.xls]Sheet1!C1:C2,IF(INDEX([myWB.xls]Sheet1!C1,MATCH(R[-11]C[-12],[myWB.xls]Sheet1!C1))=R[-11]C[-12],MATCH(R[-11]C[-12],[myWB.xls]Sheet1!C1),NA()),2)),RC[16],INDEX([myWB.xls]Sheet1!C1:C2,IF(INDEX([myWB.xls]Sheet1!C1,MATCH(R[-11]C[-12],[myWB.xls]Sheet1!C1))=R[-11]C[-12],MATCH(R[-11]C[-12],[myWB.xls]Sheet1!C1),NA()),2))"

    With Range(.Cells(2, 10), .Cells(10000, 10))
       .FormulaR1C1 = f    'fill formula without Autofill
       .Value = .Value     'replace formula with its value
    End With

    'Delete Column Z
    .Columns(26).EntireColumn.Delete
End With

Application.Calculation = calc
Application.ScreenUpdating = True
Application.EnableEvents = True

'This is above formula. now seems so easy to understand :)
'    f = 
'    "=IF(                                   'if calculations result an error...
'        ISERROR(
'               INDEX([myWB.xls]Sheet1!C1:C2,
'                    IF(
'                      INDEX([myWB.xls]Sheet1!C1,
'                           MATCH(R[-11]C[-12],
'                                [myWB.xls]Sheet1!C1)
'                           )=R[-11]C[-12],
'                      MATCH(R[-11]C[-12],
'                           [myWB.xls]Sheet1!C1),
'                      NA()
'                      ),
'                    2
'                    )
'               ),
'        RC[16],                             'use previousely copied value of the cell...
'        INDEX([myWB.xls]Sheet1!C1:C2,       'else use that calculations 
'             IF(
'               INDEX([myWB.xls]Sheet1!C1,
'                    MATCH(R[-11]C[-12],
'                         [myWB.xls]Sheet1!C1)
'                    )=R[-11]C[-12],
'               MATCH(R[-11]C[-12],
'                    [myWB.xls]Sheet1!C1),
'               NA()
'               ),
'             2
'             )
'        )
'    "

1 个答案:

答案 0 :(得分:0)

解决此问题的最简单方法是将Column J复制并粘贴到新列(例如Z)中。然后,将此公式放入第J列

IOException

我检查公式是否返回一个有效值,如果没有,那么我从列Z中输入值(我假设RC [16]是一个有效的参考,来自你之前的评论) ,否则我把计算出来的值。

如果没有看到您的电子表格,很难确切地看到您要做的事情。有可能缩短这个功能。