2017-04-13 126 views
1

新的VBA用戶在這裏,感謝您的耐心等待。我想複製並粘貼一個值範圍從單個封閉的工作表到活動工作表。具體而言,我想在活動工作簿中使用VBA從TOOL.XLSM中的「AllData」選項卡複製範圍A1:HW6000,同時TOOL.XLSM關閉並粘貼到活動工作表中範圍A1:HW6000中的活動工作簿中作爲值。從關閉的工作簿複製大活件表到活動工作簿,停止計算,Excel VBA,Mac OSX

我有這樣做的代碼(關心Peh在stackoverflow,謝謝你!),但代碼永遠運行(超過45分鐘),因爲運行代碼似乎重新計算新的工作簿和導入工作簿,並且導入工作簿(TEST.xslm)非常大。我在Mac上運行。下面是代碼我目前有:

Sub ImportData() 
Dim App As New Excel.Application 'create a new (hidden) Excel 

' remember active sheet 
Dim wsActive As Worksheet 
Set wsActive = ThisWorkbook.ActiveSheet 

' open the import workbook in new Excel (as read only) 
Dim wbImport As Workbook 
Set wbImport = App.Workbooks.Open(Filename:="/Users/cwight/Desktop/TOOL.xlsm", UpdateLinks:=True, ReadOnly:=True) 

'copy the data of the import sheet 
wbImport.Worksheets("AllDATA").Range("A1:HW6000").Copy 
wsActive.Range("A1").PasteSpecial Paste:=xlPasteFormats 'paste formats 
wsActive.Range("A1").PasteSpecial Paste:=xlPasteValues 'paste values 

App.CutCopyMode = False 'clear clipboard (prevents asking when wb is closed) 
wbImport.Close SaveChanges:=False 'close wb without saving 
App.Quit 'quit the hidden Excel 
End Sub 

我可以集成下面的代碼位在導入過程中關掉計算?如果是這樣,究竟如何?我想不出來:

Application.Calculation = xlCalculationManual 
Application.ScreenUpdating = False 
Application.DisplayStatusBar = False 

有什麼我可以做的嗎?非常感謝你的時間。

+0

45分鐘。令人印象深刻!爲什麼你需要另一個Excel實例?爲什麼你需要正好6000行?目標工作表中的其他行中有什麼?其他欄目中有什麼?複製數據中有什麼 - 數據是什麼? - 這需要兩個工作簿重新計算45分鐘? – Variatus

+0

在'Set wbImport = ....'後面插入這三行代碼,記得在代碼結束時將它們變回true。 – Luuklag

+0

在哪一行花費時間?當工作簿打開或複製粘貼時?一步一步運行宏來弄清楚。當你像往常一樣打開該文件到excel時是否也需要這段時間? –

回答

-1

這是一個函數,它使用向量從一個excel複製數據到另一個excel,確保將其分配給一個按鈕並指定一個單元格以指定路徑。 創建一個名爲模塊:「功能」,並粘貼在那裏:

Function range_to_variant(variant_arr As Variant, sheet As Worksheet, first_range As String, last_column As String, last_row_column As String) 
variant_arr = sheet.Range(first_range & ":" & last_column & sheet.Cells(sheet.Rows.Count, last_row_column).End(xlUp).Row).Value 
End Function 

Function array_to_range(variant_arr As Variant, sheet As Worksheet, first_range As String) 
'example 
' Call array_to_range(new_variant, Worksheets("Sheet1"), "1.1") 
Dim split_arr() As String 
split_arr = Split(first_range, ".") 
Dim range1 As String 
Dim range2 As String 
Dim range3 As String 
Dim range4 As String 
range1 = Replace(sheet.Cells(CInt(split_arr(0)), CInt(split_arr(1))).Address, "$", "") 
range2 = Replace(sheet.Cells(CInt(split_arr(0)) + UBound(variant_arr, 1) - 1, CInt(split_arr(1)) + UBound(variant_arr, 2) - 1).Address, "$", "") 
sheet.Range(range1 & ":" & range2).Value = variant_arr 
sheet.Range(range1 & ":" & range2).Columns.AutoFit 
End Function 

您完成後創建2子,其中寫:

Sub select_fle2() 
Call Select_file("b10", "xlsm") 
End Sub 

Sub Run() 
Dim xl As New Excel.Application 
xl.Workbooks.Open (Worksheets("MAIN").Range("B7").Value) 
xl.Visible = False 
Dim raw_data As Variant 
Call range_to_variant(raw_data, xl.Worksheets("your_sheet_name"), "A1", "HW", "A") 
xl.Quit 
Set xl = Nothing 
ThisWorkbook.Worksheets("sheet_paste").Columns("A:HW").ClearContents 
Call array_to_range(raw_data, Worksheets("sheet_paste"), "1.1") 
End sub 
+0

這應該根據需要複製數據 – Ionut

相關問題