假設A工作表為目的表
B、C、D工作表內分別有A工作表需要的不同資料
我的做法是取A工作表的A欄員工代號
然後再分別用回圈去找B、C、D工作表內有無該員工代號,
若有,則可能會將B工作表內的C:D欄資料丟回至A工作表的相對位址
同樣的,若C、D工作表內也有A工作表內目的員工編號,則再視情況去抓對應的欄位的資料,然後再貼到A工作表的相對位址
一開始是還好,主要是只有A、B兩份工作表的比對與抓取
但後來加入C、D工作表後,效率就變得奇慢了..
想請教神人們,我該如何能快速達到目的...謝謝您們Orz
個人積分:294分
文章編號:87327926
文章段落
MaverickRay wrote:
假設A工作表為目的表...(恕刪)
請愛用ADO 程式庫,把整個工作表資料變成DB資料,下SQL語法把要的資料抓出來,速度會快一倍以上。
https://learn.microsoft.com/zh-tw/sql/ado/guide/appendixes/using-ado-with-microsoft-visual-basic?view=sql-server-ver16
資料來源:Microsoft
Dim arr
arr = array(“sheetB”,“sheet C”,“sheetD”)
For a = 2 to sheetA.usedrange.rows.count
For each sheet in arr
Set target = sheet.usedrange.find(sheetA.cells(a,1),lookin:=Xlvalous , lookat:=Xlwhole)
If not target is nothing then
name1 = sheet.cells(1,”C”)
name2 = sheet.cells(1,”D”)
Set target2 = sheetA.rows(1).find(name1)
If not target2 is nothing then
Cells(a,target2.column) = _sheet.cells(target.row,”C”)
End if
Set target3 = sheetA.rows(1).find(name2)
If not target3 is nothing then
Cells(a,target3.column) = _sheet.cells(target.row,”D”)
End if
End if
Next sheet
Next a
End sub
——————————————————————
因為不知道表格格式,所以默認第一欄是標題。
因為不知道寫入資料的欄位是否相同,所以用find確定資料欄位,這部分需確保欄位標題一致。
程式碼中的sheetA到sheetD,需改成實際工作表名稱。
感謝您的寶貴回覆,這問題我應該是有解決,只是不知道與您提供的方法是否有差異,待有空時我再來好好研究,自去看病後已有好段時日沒去修改了,加上後來仰賴GPT居多,不論如何您的分享深深感謝,祝您有美好的一日
為提供您更優質的服務,本網站使用cookies。若您繼續瀏覽網頁,即表示您同意我們的cookies政策。 了解隱私權條款



























































































