2013-03-12 83 views
0

我正在研究一個VBA代碼,這將允許我日常查看交易頭寸的變化。我對VBA編程相當陌生,希望對我寫的代碼有所反饋(並在此處複製各種帖子)。VBA比較Excel中的行/列

我有3張紙,其中一張是今天的交易(表單1),一張是昨天的交易(表單2)和一張表單,應該突出顯示從「第n天」到「第n天」(表單3)的變化。在「A」欄中,我具有對於每筆交易(ID1,ID2等)唯一的交易ID,從列「B」到「AA」,我有諸如日期,值,文本等數據。

In在「更改」表(表3)中,我想在欄「A」中有一條評論,說明交易是「新」,「已刪除/已成熟」還是「已更改」。如果一筆交易改變了,我想看看哪些單元格通過顯示值前後發生了變化。

我的主要問題是我的代碼和性能出錯,例如有時我會在電子表格中使用超過500行x 30列。

當我從我的交易系統下載數據到excel時,我有時會有沒有內容的單元格,我希望我的代碼考慮到這一點。我不認爲我現在的代碼以一種好的方式做到了這一點。

任何建議/意見將不勝感激!你可以這樣做

Sub CompData() 

    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet 
    Dim ws1LRow As Long, ws2LRow As Long 
    Dim i As Long, j As Long 
    Dim ws1LCol As Long, ws2LCol As Long 
    Dim Cell1 As Range, Cell2 As Range 
    Dim SearchString As String 
    Dim MatchFound As Boolean 
    Dim n As Integer 
    Dim NewDeal As String, MatDeal As String, ChangedDeal As String 

    NewDeal = "New deal" 
    MatDeal = "Matured/deleted deal" 
    ChangedDeal = "Changed deal" 


    Set ws1 = Sheets("sheet1") 
    With ws1 
     ws1LRow = .Range("A" & .Rows.Count).End(xlUp).Row 
     ws1LCol = .Cells(2, .Columns.Count).End(xlToLeft).Column 
    End With 

    Set ws2 = Sheets("sheet2") 
    With ws2 
     ws2LRow = .Range("A" & .Rows.Count).End(xlUp).Row 
     ws2LCol = .Cells(2, .Columns.Count).End(xlToLeft).Column 
    End With 

    Set ws3 = Sheets("sheet3") 
    With ws3 
     Cells.Clear 
    End With 

    n = 1 

    For i = 1 To ws1LRow 

     SearchString = ws1.Range("A" & i).Value 

     Set Cell1 = ws2.Columns(1).Find(What:=SearchString, LookIn:=xlValues, _ 
       LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
       MatchCase:=False, SearchFormat:=False) 




     If Not Cell1 Is Nothing Then 
      Set Cell2 = Cell1 
      MatchFound = True 

      For j = 1 To ws1LCol 
       If ws1.Cells(i, j).Value <> ws2.Cells(Cell1.Row, j).Value Then 
        MatchFound = False 
        Exit For 
       End If 
      Next 

      If MatchFound = False Then 
       ws3.Cells(n, 2).Value = ws1.Cells(i, 1).Value 
       ws3.Cells(n, j + 1).Value = ws1.Cells(i, j).Value 
       ws3.Cells(n, ws2LCol + 2).Value = ws2.Cells(i, 1).Value 
       ws3.Cells(n, ws2LCol + j + 1).Value = ws2.Cells(i, j).Value 
       ws3.Cells(n, 1).Value = ChangedDeal 
       n = n + 1 
      End If 

     Else: 
      ws3.Cells(n, 2).Value = ws1.Cells(i, 1).Value 
      ws3.Cells(n, 1).Value = NewDeal 
      n = n + 1 
     End If 
    Next 

    ws2.Select 
    For i = 1 To ws2LRow 
     SearchString = ws2.Range("A" & i).Value 

     Set Cell2 = ws1.Columns(1).Find(What:=SearchString, LookIn:=xlValues, _ 
       LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
       MatchCase:=False, SearchFormat:=False) 
     If Cell2 Is Nothing Then 
      ws3.Cells(n, 2).Value = ws2.Cells(i, 1).Value 
      ws3.Cells(n, 1).Value = MatDeal 
      n = n + 1 
     End If 
    Next 

    ws3.Select 

End Sub 

回答

0

一種方式是通過建立交易ID的排序列表有:

  • 第1列:TRADE ID
  • 第2列:行是第一個列表(#NA如果不)
  • 第三列:行是在第二列表(#NA如果不)

如果在每個工作表上構建每個排序列表,然後在另一個函數中彙總數據,則可以輕鬆完成此操作。

關於你的空行,我認爲你應該做一些預處理來聚合行,或者如果你想允許空白行,你需要決定電子表格中最大行數。

PS:如果你在某個法國銀行工作,可能會在我的地板上彈出,我們可以喝杯咖啡:)