2015-12-22 90 views
0

我想知道更好更快的方式來比較兩列並偏移匹配的值。我想要抵消整行數據,而不僅僅是單元格。Excel VBA列比較,匹配和值偏移量

這是我的代碼:

子ForRawMatches() Application.ScreenUpdating =假

'Declare variables 
Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long, bln As Boolean, dar As Range 

    'Set up the count as the number of filled rows in the first column of Sheet1. 
    iRowL = Cells(Rows.Count, 2).End(xlUp).Row 

    'Cycle through all the cells in that column: 
    For iRow = 2 To iRowL 
     'For every cell that is not empty, search through the first column in each worksheet in the 
     'workbook for a value that matches that cell value. 

     If Not IsEmpty(Cells(iRow, 2)) Then 
     For iSheet = ActiveSheet.Index + 1 To Worksheets.Count 
      bln = False 
      var = Application.Match(Cells(iRow, 2).Value, Worksheets(iSheet).Columns(2), 0) 

      'If you find a matching value, indicate success by setting bln to true and exit the loop; 
      'otherwise, continue searching until you reach the end of the workbook. 
      If Not IsError(var) Then 
       bln = True 
       Exit For 
      End If 
     Next iSheet 
     End If 

     'If you do not find a matching value, do not bold the value in the original list; 
     'if you do find a value, bold it. 
     If bln = False Then 
     Cells(iRow, 2).Font.Bold = True 

     'this offsets cell value 
     Cells(iRow, 1).Offset(7500, 0) = Cells(iRow, 1).Value 



     Else 
     Cells(iRow, 2).Font.Bold = False 

     End If 
    Next iRow 
Application.ScreenUpdating = True 

結束子

+1

()而不是使用範圍()。使用範圍(單元格(row,1),單元格(row,lastcolumn))可以指示整行。 – Luuklag

+0

而不是:「var = Application.Match(Cells(iRow,2).Value,Worksheets(iSheet).Columns(2),0)」我想只比較2列,但在匹配時抵消整行。 – Erc

+0

目前還不清楚是什麼造成你的麻煩。檢索完'Match'後,你想要偏離'Match'結果中的1行?所以'rng =匹配(某物)'和'rng2 = rng.Offset(1)'? – Vegard

回答

0

我用Luuklag的示例和數據放置到另一片材進行比較。代碼如下:

子ForRawMatches()

Application.ScreenUpdating = False 

'Declare variables 
Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long, bln As Boolean, dar As Range 

    'Set up the count as the number of filled rows in the first column of Sheet1. 
    iRowL = Cells(Rows.Count, 2).End(xlUp).Row 







    'Cycle through all the cells in that column: 
    For iRow = 2 To iRowL 
     'For every cell that is not empty, search through the first column in each worksheet in the 
     'workbook for a value that matches that cell value. 

     If Not IsEmpty(Cells(iRow, 2)) Then 
     For iSheet = ActiveSheet.Index + 1 To Worksheets.Count 
      bln = False 
      var = Application.Match(Cells(iRow, 2).Value, Worksheets(iSheet).Columns(8), 0) 

      'If you find a matching value, indicate success by setting bln to true and exit the loop; 
      'otherwise, continue searching until you reach the end of the workbook. 
      If Not IsError(var) Then 
       bln = True 
       Exit For 
      End If 
     Next iSheet 
     End If 

     'If you do not find a matching value, do not bold the value in the original list; 
     'if you do find a value, bold it. 
     If bln = False Then 
     Cells(iRow, 2).Font.Bold = False 

     Else 
     Cells(iRow, 2).Font.Bold = True 
     Range("K2:K10000").NumberFormat = "000000000000" 
      Range(Cells(iRow, 1), Cells(iRow, 34)).Offset(3500, 0) = Range(Cells(iRow, 1), Cells(iRow, 34)).Value 
     End If 
    Next iRow 
Application.ScreenUpdating = True 

末子細胞