iT邦幫忙

0

輸出EXCEL的內文不存檔的狀況下速度加快

vba
  • 分享至 

  • xImage

請問:我有一份excel會抓SQL內的資料到檔案內,若我未存檔狀況下,將資料用VBA貼到新的EXCEL表格,速度就會變得很慢(超過30秒上上上);若我先存檔案在把資料輸出到新的EXCEL速度1秒就好,請問有辦法在不預存的情況下,將資料導出變快嗎?(因為我的EXCEL想設唯讀檔,不想讓人修改,所以想把SAVE拿掉。)謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
12456778
iT邦見習生 ‧ 2024-08-19 21:46:27

If you're experiencing slow performance with VBA when pasting data directly into a new Excel table and finding that saving the file first speeds things up, there are a few strategies you can employ to improve the speed without having to save the file first. Here are some tips to optimize the data export process in VBA:

  1. Optimize VBA Code:
    Turn Off Screen Updating: Disabling screen updating can significantly improve performance. Use Application.ScreenUpdating = False at the start of your macro and set it back to True at the end.
    vba
    Copy code
    Application.ScreenUpdating = False
    ' Your code to paste data
    Application.ScreenUpdating = True
    Turn Off Automatic Calculations: If your Excel file contains formulas, set calculation to manual before running your macro and set it back to automatic afterward.
    vba
    Copy code
    Application.Calculation = xlCalculationManual
    ' Your code to paste data
    Application.Calculation = xlCalculationAutomatic
    Disable Events: Temporarily disable workbook and worksheet events to prevent unnecessary processing during data operations.
    vba
    Copy code
    Application.EnableEvents = False
    ' Your code to paste data
    Application.EnableEvents = True
  2. Use Efficient Methods for Data Transfer:
    Array-Based Transfer: Instead of pasting data cell by cell, use an array to handle the data. This method is faster because it reduces the number of interactions between VBA and Excel.
    vba
    Copy code
    Dim dataArray As Variant
    Dim wsSource As Worksheet
    Dim wsDest As Worksheet

Set wsSource = ThisWorkbook.Sheets("SourceSheet")
Set wsDest = ThisWorkbook.Sheets("DestSheet")

' Load data into array
dataArray = wsSource.Range("A1:D1000").Value

' Paste data from array to destination
wsDest.Range("A1").Resize(UBound(dataArray, 1), UBound(dataArray, 2)).Value = dataArray
3. Use Excel's Built-In Features:
Query Tables or Power Query: If you're importing data from SQL, consider using Excel's Query Tables or Power Query. These tools are optimized for handling data imports efficiently and can avoid the overhead of VBA.
4. Work with Temporary Data:
Use a Temporary Sheet: Create a temporary worksheet to handle the data transfer and then move the data to the final destination. This can help in cases where direct data pasting causes performance issues.
vba
Copy code
Dim tempSheet As Worksheet

' Create a temporary worksheet
Set tempSheet = ThisWorkbook.Worksheets.Add

' Paste data into the temporary sheet
wsSource.Range("A1:D1000").Copy Destination:=tempSheet.Range("A1")

' Move data from the temporary sheet to the destination
tempSheet.Range("A1:D1000").Copy Destination:=wsDest.Range("A1")

' Delete the temporary sheet
Application.DisplayAlerts = False
tempSheet.Delete
Application.DisplayAlerts = True
5. Avoid Unnecessary Formatting:
Minimize Formatting: If you're applying formatting while copying data, try to avoid it or apply it after the data is pasted.
By applying these strategies, you should be able to improve the speed of data transfer without the need to save the file first. If you're still facing issues, consider providing more specific details about the VBA code and data you're working with for further assistance.

0
japhenchen
iT邦超人 1 級 ‧ 2024-08-20 08:10:30

我用EPPLUS+C#(iis上跑),輸出上萬行資料約不到30秒(包括SQL查詢及整併)

我要發表回答

立即登入回答