- 帖子
- 1018
- 主題
- 15
- 精華
- 0
- 積分
- 1058
- 點名
- 0
- 作業系統
- win7 32bit
- 軟體版本
- Office 2016 64-bit
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 桃園
- 註冊時間
- 2012-5-9
- 最後登錄
- 2022-9-28
|
30#
發表於 2013-8-2 10:12
| 只看該作者
回復 29# happycoccolin - Sub TEST()
- Const DATABASE_NAME = "A" '資料庫工作表名稱
- Const DATABASE_COL = 5 'E欄
- Const COMPARE_COL = 11 'K欄
-
- Dim d, ar, filein, fileout, s, i As Long
-
- Set d = CreateObject("scripting.dictionary")
- With Sheets(DATABASE_NAME)
- ar = .[A1].CurrentRegion.Resize(.Cells(.Rows.Count, "A").End(xlUp).Row).Value
- End With
- For i = 2 To UBound(ar)
- d(Replace(ar(i, DATABASE_COL), "-", "")) = ar(i, 1)
- Next
-
- filein = Application.GetOpenFilename(FileFilter:="Excel 活頁簿 (*.xlsx),*.xlsx", Title:="選擇要比對的檔案")
- If Not TypeName(filein) = "String" Then Exit Sub '取消則結束
-
- Application.ScreenUpdating = False
- With Workbooks.Open(filein).Sheets(1)
- ar = .[A1].CurrentRegion.Resize(.Cells(.Rows.Count, "A").End(xlUp).Row).Value
- .Parent.Close False
- End With
- Application.ScreenUpdating = True
-
- ReDim Preserve ar(LBound(ar) To UBound(ar), LBound(ar, 2) To UBound(ar, 2) + 1)
- For i = LBound(ar) + 1 To UBound(ar)
- s = Replace(ar(i, COMPARE_COL), "-", "")
- If d.exists(s) Then
- ar(i, UBound(ar, 2)) = d(s)
- Else
- ar(i, UBound(ar, 2)) = "No Data"
- End If
- Next
-
- With Workbooks.Add
- Application.ScreenUpdating = False
- With .Sheets(1).[A1].Resize(UBound(ar), UBound(ar, 2))
- .Value = ar
- .Font.Name = "Verdana" '字體名稱
- .Font.Size = 14 '字體大小
- .Borders.LineStyle = xlContinuous '框線
- .EntireColumn.AutoFit '調整欄寬
-
- .Rows(1).Interior.Color = 12567966 '標頭顏色
- .Rows(1).Font.Bold = True '標頭粗體字
- End With
- Application.ScreenUpdating = True
-
- If MsgBox("是否要儲存檔案?", vbYesNo) = vbYes Then
- fileout = Application.GetSaveAsFilename(FileFilter:="Excel 活頁簿 (*.xlsx),*.xlsx", Title:="另存為新檔")
- If Not TypeName(fileout) = "String" Then Exit Sub '取消則結束
- .SaveAs fileout, FileFormat:=xlWorkbookDefault
- End If
- End With
- End Sub
複製代碼 |
|