• 2

「Excel VBA」資料量過大,匯入欄位超過1048576

大家好,由於小弟最近在處理一份檔案,該檔案資料量相當大

欄位會超過Excel 2007的欄位上限1048576

於是小弟想到一種解決方式,流程是:

1.匯入檔案後

2.超過欄位上限的資料自動置於第二個工作表

3.若第二個工作表也放不下,再往第三個工作表放…

4.依此類推直到檔案全部匯入完畢

但苦於小弟VBA功力不足,所以想請板上的高手幫忙解答一下,感謝~~

2012-09-05 20:56 發佈

頂好Wellcome wrote:
大家好,由於小弟最近...(恕刪)



超市最好的辦法就是請專家協助= =
我不是超市員工=.=

那只是看到桌上衛生紙想到的暱稱....
稻草人到處草人

[笑到噴淚][笑到噴淚][笑到噴淚][笑到噴淚]

2021-08-03 20:34
換用資料庫來做如access
或者是excel 2010 with power pivot

kltwn wrote:
換用資料庫來做如ac...(恕刪)



Dear kltwn worte:

抱歉,我今天有在公司試,發現您所說的這兩套軟體都沒有= =

慘了~~~

今天有去書局翻了一下vba的書,還是沒有我要的答案..

懇請版上高手大大賜教,感謝~

頂好Wellcome wrote:
大家好,由於小弟最近...(恕刪)
我有思考過這個問題,但還沒有實做過
目前想到的方法,第一步是先判斷資料筆數
第二步則是設定每一個工作表要儲存多少筆資料,再用資料筆數去除,以得知需要產生幾個工作表
第三步就是利用迴圈去循序讀取原本要匯入的檔案,達到資料數上限後則換到下一個工作表繼續

所以,如果是我要寫VBA,大概會分這幾個步驟去處理
因為不知道你的原始資料是什麼格式,我也沒有實做過這個部分
看看其他高手能不能給你更清楚的建議囉
一個中文,各自表述
你的需求已經是需要access或更高級資料庫啦 很少人選擇硬塞excel 平常這種狀況我也選擇放棄直接用excel整理原始資料

頂好Wellcome wrote:
大家好,由於小弟最近...(恕刪)


你的原始檔案應該CSV檔
有最笨的方法可以用
用記事本開啟該CSV資料檔
再做適當的修改就可以了

以前遇過相同問題的設備紀錄檔
你可以試試看
感謝大家

我有在網路上搜到這些程式碼,實作後應該沒問題...

只是資料量真的太大,跑完要三小時= =+,目前也正在試著用access

有興趣的可以試試...

Sub LargeFileImport()
'Dimension Variables
Dim ResultStr As String
Dim FileName As String
Dim FileNum As Integer
Dim Counter As Double
'Ask User for File's Name
FileName = InputBox("Please enter the Text File's name, e.g. test.txt")
'Check for no entry
If FileName = "" Then End
'Get Next Available File Handle Number
FileNum = FreeFile()
'Open Text File For Input
Open FileName For Input As #FileNum
'Turn Screen Updating Off
Application.ScreenUpdating = False
'Create A New WorkBook With One Worksheet In It
'Workbooks.Add template:=xlWorksheet
'Set The Counter to 1
Counter = 1
'Loop Until the End Of File Is Reached
Do While Seek(FileNum) <= LOF(FileNum)
'Display Importing Row Number On Status Bar
Application.StatusBar = "Importing Row " & _
Counter & " of text file " & FileName
'Store One Line Of Text From File To Variable
Line Input #FileNum, ResultStr
'Store Variable Data Into Active Cell
'修改如下
'ActiveCell(1, 1).Value = Mid(ResultStr, 1, 3) '第一欄
'ActiveCell(1, 2).Value = Mid(ResultStr, 4, 8) '第二欄
'ActiveCell(1, 3).Value = Mid(ResultStr, 9, 11) '第三欄
ActiveCell(1, 1).Value = ResultStr '第一欄
'ActiveCell(1, 2).Value = Mid(ResultStr, 4, 8) '第二欄
'ActiveCell(1, 3).Value = Mid(ResultStr, 9, 11) '第三欄
'請依實際需求修正
'For Excel versions before Excel 97, change 65536 to 16384
If ActiveCell.Row = 1048576 Then
'If On The Last Row Then Add A New Sheet
ActiveWorkbook.Sheets.Add
Else
'If Not The Last Row Then Go One Cell Down
ActiveCell.Offset(1, 0).Select
End If
'Increment the Counter By 1
Counter = Counter + 1
'Start Again At Top Of 'Do While' Statement
Loop
'Close The Open Text File
Close
'Remove Message From Status Bar
Application.StatusBar = False
End Sub
既然透過 VBA 分割匯入有困難,換個方式先把匯入的檔案分割成數個小的檔案,再進行匯入,是否能解決你的問題

Try try 這個工具
可分割巨大文字檔的Split Large Text Files
或者 Google "split large text files",相信能找到很多相關的解決方案
http://kivava.blogspot.com
  • 2
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?