我是巴西一家工業公司的實習生,事實上我正在使用excel。幾天前我剛剛開始玩VBA,我對它可以爲我做的很多事情感到好笑!VBA - 從封閉工作簿中複製數據的最佳方法
我沒有很強的編程背景,所以我基本上是在學習。代碼工作正常,從開始到結束只需不到15秒。我不打擾時間,但如果可以改善,那會很棒。
我的主要目標是保持代碼簡單高效。我將在接下來的幾個月裏離開公司,我希望它很容易維護和使用。我問的是更好的方式來編寫我的代碼,以便其他人更容易理解,並且如果可能的話(當然是!)花費更少的時間。
我的代碼刪除當前工作簿中的4張內容,然後從其他4個已關閉的工作簿中複製更新的數據。然後關閉一切。 :)數據是關於日常生產和他們的名字是葡萄牙語,對此感到遺憾。
Sub CopiarBase()
'
' Atalho do teclado: Ctrl+q
'
' Variables
Dim MyCurrentWB As Workbook
Dim BMalharia As Worksheet
Dim BBeneficiamento As Worksheet
Dim BEmbalagem As Worksheet
Dim BDikla As Worksheet
Set MyCurrentWB = ThisWorkbook
Set BMalharia = MyCurrentWB.Worksheets("B-Malharia")
Set BBeneficiamento = MyCurrentWB.Worksheets("B-Beneficiamento")
Set BEmbalagem = MyCurrentWB.Worksheets("B-Embalagem")
Set BDikla = MyCurrentWB.Worksheets("B-Dikla")
'Clean all the cells - Workbook 1
Dim Malharia_rng As Range
Set Malharia_rng = BMalharia.Range("A2:CN" & BMalharia.Cells(Rows.Count, 1).End(xlUp).Row)
Malharia_rng.ClearContents
Dim Ben_rng As Range
Set Ben_rng = BBeneficiamento.Range("A2:CY" & BBeneficiamento.Cells(Rows.Count, 1).End(xlUp).Row)
Ben_rng.ClearContents
Dim Emb_rng As Range
Set Emb_rng = BEmbalagem.Range("A2:CT" & BEmbalagem.Cells(Rows.Count, 1).End(xlUp).Row)
Emb_rng.ClearContents
Dim Dikla_rng As Range
Set Dikla_rng = BDikla.Range("A2:AV" & BDikla.Cells(Rows.Count, 1).End(xlUp).Row)
Dikla_rng.ClearContents
'Copy from Malharia Workbook
Workbooks.Open "C:\Users\marco.henrique\Desktop\Bases\Malharia Base.xls"
LastRowMB = Workbooks("Malharia Base.xls").Worksheets("Malharia Base").Cells(Rows.Count, 1).End(xlUp).Row
Dim Malha_base As Range
Set Malha_base = Workbooks("Malharia Base.xls").Worksheets("Malharia Base").Range("A2:CN" & LastRowMB)
MyCurrentWB.Worksheets("B-Malharia").Range("A2:CN" & LastRowMB).Value = Malha_base.Value
Workbooks("Malharia Base.xls").Close
'Copy from Beneficiamento Workbook
Workbooks.Open "C:\Users\marco.henrique\Desktop\Bases\Beneficiamento Base.xls"
LastRowBB = Workbooks("Beneficiamento Base.xls").Worksheets("Beneficiamento Base").Cells(Rows.Count, 1).End(xlUp).Row
Dim Ben_base As Range
Set Ben_base = Workbooks("Beneficiamento Base.xls").Worksheets("Beneficiamento Base").Range("A2:CY" & LastRowBB)
MyCurrentWB.Worksheets("B-Beneficiamento").Range("A2:CY" & LastRowBB).Value = Ben_base.Value
Workbooks("Beneficiamento Base.xls").Close
'Copy from Embalagem Workbook
Workbooks.Open "C:\Users\marco.henrique\Desktop\Bases\Embalagem Base.xls"
LastRowEB = Workbooks("Embalagem Base.xls").Worksheets("Embalagem Base").Cells(Rows.Count, 1).End(xlUp).Row
Dim Emb_base As Range
Set Emb_base = Workbooks("Embalagem Base.xls").Worksheets("Embalagem Base").Range("A2:CT" & LastRowEB)
MyCurrentWB.Worksheets("B-Embalagem").Range("A2:CT" & LastRowEB).Value = Emb_base.Value
Workbooks("Embalagem Base.xls").Close
'Copy from Dikla Workbook
Workbooks.Open "C:\Users\marco.henrique\Desktop\Bases\Diklatex Base.xls"
LastRowDB = Workbooks("Diklatex Base.xls").Worksheets("Diklatex Base").Cells(Rows.Count, 1).End(xlUp).Row
Dim Dikla_base As Range
Set Dikla_base = Workbooks("Diklatex Base.xls").Worksheets("Diklatex Base").Range("A2:AV" & LastRowDB)
MyCurrentWB.Worksheets("B-Dikla").Range("A2:AV" & LastRowDB).Value = Dikla_base.Value
Workbooks("Diklatex Base.xls").Close
End Sub
對不起,如果我不夠清楚,當然英語不是我的母語。任何關於我的代碼或整個想法的疑問都可以自由地提出問題。
在此先感謝您的幫助!
我投票結束這個問題作爲題外話,因爲它完全適合[代碼評論](http://codereview.stackexchange.com/) –
這可能是一個很好的問題[codereview.se],只要:**(A)** _代碼作品_,**和(B)** _it不以任何方式假設或不完整。在發佈之前,請閱讀[主題指南](http://codereview.stackexchange.com/help/on-topic),如果您選擇轉到[代碼評論](http://codereview.stackexchange.com/問題/賣出)。如果您有任何問題或疑慮,請加入我們的[CR幫助臺](http://chat.stackexchange.com/rooms/34045)。 – Phrancis
我會那樣做的。對不起,一塌糊塗! – mschlindwein