2014-10-11 27 views
1

我想在Excel中使用VBA對齊兩個表。如何使用VBA在Excel中排列表格?

基本上我有:

Table 1     Table 2    
7 columns    7 columns 

在表2中,一些行從表1人失蹤!

我在下面使用這個VBA,但它沒有工作,因爲在我的表中我有7列兩個。我需要確保兩個表的7列都匹配,即使我的第二個表中有一些行缺失。

Sub Macro1() 
    Dim rng1 As Range 
    Set rng1 = Range([a1], Cells(Columns.Count, "A").End(xlUp)) 
    rng1.Offset(0, 1).Columns.Insert 
    With rng1.Offset(0, 1) 
     .FormulaR1C1 = _ 
     "=IF(ISNA(MATCH(RC[-1],C[1],0)),"""",INDEX(C[1],MATCH(RC[-1],C[1],0)))" 
     .Value = .Value 
    End With 
End Sub 

任何想法任何人,我會附上一張圖片,但這是我第一次使用Stackoverflow哈哈!

enter image description here

enter image description here

+0

可以在表2固定之前和之後顯示一組簡單的數據(如文本)嗎? – 2014-10-11 16:14:09

+0

@PeterM無論如何,我可以添加圖片?因爲如果我能向你展示它更容易! – Nwaaaa 2014-10-11 16:30:25

+0

示例在鏈接http://imageshack.com/a/img540/6440/sTJOq9.jpg http://imageshack.com/a/img661/6121/c2aLle.jpg @PeterM – Nwaaaa 2014-10-11 16:56:14

回答

1

雖然這個答案確實包含了一些代碼,它更關注的是教您如何自己寫一個類似的宏。

我現在應該說,我不同意我的代碼:

  • 我的宏不檢查表2.如果表2中包含的行不存在於表1,我的宏將動議排和下面的任何一個,下來留下一個非常大的差距。
  • 現場更新數據是一種不好的做法。如果出現任何問題,數據將會部分改變。您可能必須重新開始備份原始工作簿。最好創建一個新的工作表並根據需要複製數據以創建您所尋找的外觀。

你的代碼找到一個工作表的最後一排,但下面是簡單的,只是作爲可靠:

With Worksheets("Data") 
    RowExistLast = .Cells(Rows.Count, ColExistId).End(xlUp).Row 
End With 

這是很少的活動工作表上運行一個好主意。例如,如果用戶啓動錯誤工作表激活的宏,該工作表將被損壞。我的代碼將在指定的工作表上運行,即使它未處於活動狀態。

您需要檢查在表1中的每一行下面的代碼表1中的每一行的ID列的內容輸出到立即窗口。如果運行此碼,但最後200行左右將有滾出窗口的頂部:

With Worksheets("Data") 
    RowExistLast = .Cells(Rows.Count, ColExistId).End(xlUp).Row 
    For RowExistCrnt = RowDataFirst To RowExistLast 
     Debug.Print .Cells(RowExistCrnt, ColExistId).Value 
    Next 
    End With 

使用For循環我不能下臺表2。循環的結束值不能在循環中更改,但我們將插入行。一個Do While循環將是必要的:

RowNewCrnt = RowDataFirst 
Do While RowNewCrnt <= RowNewLast 
    : 
    ' If row inserted 
    RowNewLast = RowNewLast + 1 
    : 
    ' With For loop, control variable is stepped automatically. 
    ' With Do loop, you must step it as necessary. 
    : 
    RowNewCrnt = RowNewCrnt + 1 
Next 

因爲我試圖對準兩個表中的行,我並不需要一個單獨的迴路表2,我只需要兩個表一個行變量。

在我的宏,我檢查兩個表的ID列,並插入一個部分行到表2時,有一個不匹配。所以:

A  A 
B  B 
C  D 
D   

變爲:

A  A 
B  B 
C 
D  D 

你可能想從表1的標題欄移動值(列A:B)構建表2中的行表的標題列2(J:K)並將值列(L)設置爲零,但我沒有爲您提供此代碼。

我希望任何VBA程序員快速熟悉For循環等,但插入部分行不是我每天都做的事情,所以我沒有必要的語法在我的指尖。我打開了宏錄製,插入部分線路,關閉宏記錄並檢查它創建的代碼:

Sub Macro1() 
' 
' Macro1 Macro 
' Macro recorded 14/10/2014 by Tony Dallimore 
' 

' 
    Range("J7:L7").Select 
    Selection.Insert Shift:=xlDown 
End Sub 

這是語法正確的VBA反而不好VBA。選擇單元格或範圍是很不好的做法。這兩個鍵語句可以被替換爲:

Range("J7:L7").Insert Shift:=xlDown 

"J7:L7"就是我可以建立在運行時的字符串:

.Range(ColNewFirst & RowBothCrnt & ":" & ColNewLast & RowBothCrnt).Insert Shift:=xlDown 

上面我已經介紹了下面的宏中的所有元素。如有必要,請回答問題,但您可以自己解密此代碼的次數越多,開發速度就越快。

' Look up this statement to read why its inclusion is a good idea 
Option Explicit 
Sub AlignRows() 

    ' Using constants instead of literals has the following effects: 
    ' * It takes a little longer to type your macro. 
    ' * It makes your macro self-documenting. 
    ' * If new header rows or data columns are added, amending the constants 
    ' will fix the macro. 
    Const ColExistId As String = "A" 
    Const ColNewId As String = "J" 
    Const ColNewFirst As String = "J" 
    Const ColNewLast As String = "L" 
    Const RowDataFirst As Long = 6 

    Dim RowBothCrnt As Long 
    Dim RowExistLast As Long 

    ' I do not know the name of your worksheet. Replace "Data" with your worksheet name 
    With Worksheets("Data") 

    RowExistLast = .Cells(Rows.Count, ColExistId).End(xlUp).Row 

    For RowBothCrnt = RowDataFirst To RowExistLast 
     If .Cells(RowBothCrnt, ColExistId).Value <> _ 
     .Cells(RowBothCrnt, ColNewId).Value Then 
     .Range(ColNewFirst & RowBothCrnt & ":" & _ 
       ColNewLast & RowBothCrnt).Insert Shift:=xlDown 
     End If 
    Next 

    End With 

End Sub 
相關問題