A檔案的格式如下
A B C D E
100-1 XXX XXX XXX DOG
100-2 XXX XXX XXX CAT
100-3 XXX XXX XXX CAT-1
100-4 XXX XXX XXX CAT-2
B檔案的格式如下
A B C D E F G H I J
11 XX XX XX XX XX XX XX CC DOG
12 XX XX XX XX XX XX XX CC CAT
13 XX XX XX XX XX XX XX CC CAT1
14 XX XX XX XX XX XX XX CC CAT-1
15 XX XX XX XX XX XX XX CC CAT3
比對結果希望產生C檔案
A B C D E F G H I J K
11 XX XX XX XX XX XX XX CC DOG 100-1
12 XX XX XX XX XX XX XX CC CAT 100-2
13 XX XX XX XX XX XX XX CC CAT1 100-3
14 XX XX XX XX XX XX XX CC CAT-1 100-3
15 XX XX XX XX XX XX XX CC CAT3 No Data作者: happycoccolin 時間: 2013-7-25 11:15
Sub 比對()
Set d = CreateObject("Scripting.Dictionary") '字典
With Workbooks("A.xls").Sheets(1) 'with 報名名稱.工作表名稱
For Each a In .Range(.[E2], .[E65536].End(xlUp))
d(a & "") = Array(a.Offset(, -4).Value)
Next
End With
With Workbooks("B.xls").Sheets(1)
For Each a In .Range(.[J2], .[J65536].End(xlUp))
a.Offset(, 1).Resize(, 1).Value = d(a & "")
Next
End With
End Sub
i = 1
With Workbooks("A.xlsx").Sheets(1) '
Do While .Cells(i, "e") <> ""
d(.Cells(i, "e").Value) = .Cells(i, "A").Value
i = i + 1
Loop作者: GBKEE 時間: 2013-7-26 19:58
回復 17#happycoccolin
因為我已經將資料庫放在ABC.xlsm裡面了
With Workbooks("A.xlsx").Sheets(1) -改一下檔名 With Workbooks("ABC.xlsm").Sheets(1)作者: happycoccolin 時間: 2013-7-30 10:54
只修改以下後偵錯沒有問題
With Wb.Sheets(1)
Do While .Cells(i, "K") <> ""
S = Join(Application.Transpose(Application.Transpose(.Range("A" & i & ":K" & i))), ",")
If d.Exists(.Cells(i, "K").Value) Then
S = S & "," & d(.Cells(i, "K").Value)
d(.Cells(i, "K").Value) = Split(S, ",")
Else
d(.Cells(i, "K").Value) = Split(S & ",No Data", ",")
S = d(.Cells(i, "K").Value)
End If
i = i + 1
Loop
.Parent.Close False '關閉指定檔案不存檔
End With
但因為B.xlsx比對欄位"K"後 還有一欄"L"
新產生的檔案資料會向後順延一欄填到"M"嗎?
偵錯現在停在
S = Application.Transpose(Application.Transpose(d.ITEMS))
ABC.xlsx
A B C D E
100-1 XXX XXX XXX DOG
100-2 XXX XXX XXX CAT
100-3 XXX XXX XXX CAT-1
100-4 XXX XXX XXX CAT-2
↑比對完畢後Show出此欄位值 ↑比對欄位
B.xlsx
A B C D E F G H I J K L 希望顯示在這欄位(M)
11 XX XX XX 1 A1 XX XX XX CC DOG XX
12 XX XX XX 2 A2 XX XX XX CC CAT XX
13 XX XX XX 2 A3 XX XX XX CC CAT1 XX
14 XX XX XX 1 A4 XX XX XX CC CAT-1 XX
15 XX XX XX 1 A5 XX XX XX CC CAT3 XX
↑比對欄位 ↑若有多一欄位 產生的比對資料是否可向後順延到M呢?
應該說是否可自動填在最後一欄呢?
而且這次CAT1沒有被比對出來~~
附檔
[attach]15652[/attach]
11 XX XX XX XX XX XX XX CC DOG 100-1
12 XX XX XX XX XX XX XX CC CAT 100-2
14 XX XX XX XX XX XX XX CC CAT-1 100-3
13 XX XX XX XX XX XX XX CC CAT1 No Data
15 XX XX XX XX XX XX XX CC CAT3 No Data
With .Columns("F")
If .Width > 250 Then
.Width = 250 (偵錯停在這一行)
.WrapText = True
End If
End With
With .Columns("M")
If .Width > 250 Then
.Width = 250
.WrapText = True
End If
Application.ScreenUpdating = False
With Workbooks.Open(f) 這應該是指新開的資料檔嗎?
With .Sheets(1)
ar = .Range("A5:H" & .[A5].CurrentRegion.Rows.Count).Value 這句不懂甚麼意思
End With
.Close False
End With
Application.ScreenUpdating = True
r = UBound(ar)
With Workbooks.Add
With .Sheets(1)
For i = LBound(cIndexOld) To UBound(cIndexOld)
.Cells(1, cIndexNew(i)).Resize(r).Value = Application.WorksheetFunction.Index(ar, 0, cIndexOld(i))
.Cells(1, cIndexNew(i)).Value = arNewHeader(i)
Next
End With作者: stillfish00 時間: 2013-8-15 17:46
S大~~我想加入這兩行~但是一直有錯可以幫忙看一下嗎~~~謝謝~~~~
With Workbooks.Add
With .Sheets(1)
.Font.Name = "Tahoma" '字體名稱
.Font.Size = 10 '字體大小
End With
End With作者: stillfish00 時間: 2013-8-16 14:39
回復 52#happycoccolin
要給儲存格範圍,如
With Workbooks.Add
With .Sheets(1)
.[A1:H1].Font.Name = "Tahoma" '字體名稱
.[A1:H1].Font.Size = 10 '字體大小
End With
End With作者: happycoccolin 時間: 2013-8-16 15:13
r = UBound(ar)
With Workbooks.Add
With .Sheets(1)
For i = LBound(cIndexOld) To UBound(cIndexOld)
.Cells(1, cIndexNew(i)).Resize(r).Value = Application.WorksheetFunction.Index(ar, 0, cIndexOld(i))
Next
.[A1].Resize(, UBound(arNewHeader) + 1).Value = arNewHeader
.[A1:AV10000].Font.Name = "Arial" '字體名稱
.[A1:AV10000].Font.Size = 10 '字體大小