2015-11-14 113 views
0

我需要您的幫助才能找到Excel vba代碼。我有輸入行作爲RNC名稱和CellID。在連接列中,我使用連接公式來組合RNC名稱和CellID。它是通過一個記錄的宏,需要非常長的時間完成複製/粘貼特殊的3-500,000個單元格作爲值。更快計算500,000行

RNC Name CELLID Concatenate  Cell Name Count 
RNCBESI05 10621 RNCBESI05_10621 ALJRMG_1 31 
RNCBESI05 10621 RNCBESI05_10621 ALJRMG_1 31 
RNCBESI05 10621 RNCBESI05_10621 ALJRMG_1 31 

請問請您建議如何計算1/2M細胞更快?

記錄碼:

Sheets("Neighbor").Select 
Range("C2").Select 
    ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-2],""_"",RC[-1])" 
    Range("D2").Select 
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],Cell!C[-3]:C,4,0)" 
    Range("E2").Select 
    ActiveCell.FormulaR1C1 = "=COUNTIF(C[-1],RC[-1])" 
    Range("C2:E2").Select 
    Selection.Copy 
    Range("B2").Select 
    Selection.End(xlDown).Offset(0, 1).Select 
    Range(Selection, Selection.End(xlUp)).Select 
    ActiveSheet.Paste 
    Application.CutCopyMode = False 
    Selection.Copy 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
    Selection.End(xlUp).Select 

回答

2

捕獲小區範圍值,以在存儲器中的變體陣列和處理應改善的計算時間。

Sub three_ops() 
    Dim v As Long, vVALs As Variant, vCELs As Variant 

    With Worksheets("Cell") 
     Debug.Print Intersect(.Columns("A:D"), .UsedRange).Address(0, 0) 
     vCELs = Intersect(.Columns("A:D"), .UsedRange).Value2 
    End With 

    With Worksheets("Neighbor") 
     vVALs = Intersect(.Columns("A:D"), .UsedRange).Value2 
     For v = LBound(vVALs, 1) + 1 To UBound(vVALs, 1) 
      vVALs(v, 3) = Join(Array(vVALs(v, 1), vVALs(v, 2)), Chr(95)) 
      If Not IsError(Application.Match(vVALs(v, 3), Application.Index(vCELs, 0, 1), 0)) Then 
       vVALs(v, 4) = Application.VLookup(vVALs(v, 3), vCELs, 4, 0) 
      End If 
     Next v 
     With Intersect(.Columns("A:D"), .UsedRange) 
      .Value = vVALs 
      With .Resize(.Rows.Count - 1, 1).Offset(1, 4) 
       .Formula = "=countif(" & .Offset(0, -1).Address(1, 0) & ", d2)" 
       .Value = .Value2 
      End With 
     End With 
    End With 
End Sub 

我從來沒有太多成功與COUNTIF function和變種陣列。您可能想要查看Is there a faster CountIf以加速該操作。