我有兩個excel表。我必須合併這兩個值,使得一個值與另一個匹配。例如。使用普通列合併兩個excel文件
The first excel, the 2nd excel
1 t 1 tes1
2 5 3 tes3
3 t 4 tes4
4 g
注意,在第二Excel中的第一列,2缺失,所以我想第一個Excel到這個樣子,
1 tes1 t
2 5
3 tes3 t
4 tes4 g
我是新來的出色。任何幫助,將不勝感激。
我有兩個excel表。我必須合併這兩個值,使得一個值與另一個匹配。例如。使用普通列合併兩個excel文件
The first excel, the 2nd excel
1 t 1 tes1
2 5 3 tes3
3 t 4 tes4
4 g
注意,在第二Excel中的第一列,2缺失,所以我想第一個Excel到這個樣子,
1 tes1 t
2 5
3 tes3 t
4 tes4 g
我是新來的出色。任何幫助,將不勝感激。
Sub left_join()
Dim res As Variant
Dim i As Long, lastUsedRowSh1 As Long, lastUsedRowSh2 As Long
Dim cell As Range
Sheets(3).Cells.ClearContents
Sheets(1).Range("a:b").Copy Destination:=Sheets(3).Range("a1")
Sheets(3).Columns(2).Insert Shift:=xlToRight
lastUsedRowSh1 = Sheets(1).Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
lastUsedRowSh2 = Sheets(2).Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
i = 1
For Each cell In Sheets(1).Range("a1:a" & lastUsedRowSh1)
On Error Resume Next
res = Application.WorksheetFunction.VLookup(cell.Value, Sheets(2).Range("a1:b" & lastUsedRowSh2), 2, 0)
If Err.Number = 0 Then
Sheets(3).Range("b" & i).Value = res
i = i + 1
Else
i = i + 1
End If
Next cell
End Sub
你甚至可以用一個簡單的公式求解。
Foglio1
A B
1 t
2 5
3 t
4 g
Foglio2
A B
1 tes1
3 tes3
4 tes4
Foglio3
複印Foglio1在Foglio3的內容,然後運行該式
=IF(ISERROR(VLOOKUP(Foglio1!A1,Foglio2!$A$1:$B$3,2,0))=TRUE,"",VLOOKUP(Foglio1!A1,Foglio2!$A$1:$B$3,2,0))
並將其向下拖動。問候。
聽起來像是你想有一個關係型數據庫(例如訪問),而不是一個電子表格。無論哪種方式,SuperUser.com上最好問這個問題。 – Thomas
謝謝哥們..我問過那裏 – Indy