[VBA ] 計算選取檔案的列數和行數

看板Visual_Basic作者 (Piggy)時間14年前 (2009/08/09 22:47), 編輯推噓1(1014)
留言15則, 3人參與, 最新討論串1/1
各位vb的版友們你們好 小弟是vba的新手 之前都是再寫c/c++ 想請問vb或vba有沒有可以計算檔案的行數跟列數的指令 因為我想要先算出有幾行幾列再去配置陣列大小 我的檔案大概長成這樣 Date 2882 2881 2888 2892 . . . 2008/08/08 58.32 32.58 57.21 23.56 . . . . . . 大約有1000列11行 但是檔案拿到並不知道有幾列幾行 最後要把這個檔案擺進一個大矩陣 以下是我用c++可以跑出來的主要讀檔程式碼 char filename[50]; cout<<" 請輸入檔案路徑和名稱 : ";cin.getline(filename,50); int i,j; char word,word2; ifstream ifile(filename,ios::in); while(!ifile.eof()){ ifile.get(word); if(word == '\n') nrow++; } ifile.close(); ifstream ifile1(filename,ios::in); while(word2!='\n'){ ifile1.get(word2); if(word2 == '\t') ncol++; } ifile1.close(); nrow=nrow+1; ncol=ncol+1; ifstream ifile2(filename,ios::in); //二維動態記憶體配置 data=(double**)malloc(nrow*sizeof(*data)); for(int i=0;i<nrow;i++) *(data+i)=(double*)malloc((ncol+1)*sizeof(double)); for(i=0;i<nrow;i++) for(j=0;j<ncol;j++) ifile2>>data[i][j]; ifile2.close(); VBA的迴圈指令我已經會改了 陣列存放我也會了 只是不太曉得在vba要怎麼計算行列數 這個問題應該很簡單 請不要鞭我@@ 題外話: 還有就是用如果設計出一個介面 有辦法讓使用者輸入檔名 然後再由程式讀取檔名嗎? -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 203.67.196.10

08/09 22:54, , 1F
阿...我好像會讀有幾筆資料了...但是有幾行還不會@@
08/09 22:54, 1F

08/09 22:57, , 2F
Dim nrow As Integer
08/09 22:57, 2F

08/09 22:57, , 3F
nrow=0
08/09 22:57, 3F

08/09 22:57, , 4F
While Not EOF(1)
08/09 22:57, 4F

08/09 22:58, , 5F
nrow=nrow+1
08/09 22:58, 5F

08/09 22:58, , 6F
Wend
08/09 22:58, 6F

08/10 00:54, , 7F
ThisWorkbook.Worksheets(1).UsedRange.Columns.Count
08/10 00:54, 7F

08/10 00:54, , 8F
ThisWorkbook.Worksheets(1).UsedRange.Rows.Count
08/10 00:54, 8F

08/11 07:55, , 9F
樓上的方法我有試了一下...但是好像還是要開啟那個檔案
08/11 07:55, 9F

08/11 07:56, , 10F
我的意思是想在一個新的工作表直接叫出那個檔案
08/11 07:56, 10F

08/11 07:56, , 11F
那個檔案我可以自動算出行列數
08/11 07:56, 11F

08/13 21:01, , 12F
我用ADO把檔案當成DB讀取資料,不用開啟檔案。
08/13 21:01, 12F

08/13 21:02, , 13F
再利用SQL的方式做資料處理,習慣後還蠻好用的。
08/13 21:02, 13F

08/13 21:03, , 14F
原PO既然會寫C++這方面應該難不倒
08/13 21:03, 14F

08/15 14:20, , 15F
哈!樓上方法不錯耶!我try一下!謝謝!
08/15 14:20, 15F
文章代碼(AID): #1AVk5xAK (Visual_Basic)