返回列表 上一主題 發帖

關於:VBA Function的問題

關於:VBA Function的問題

我在網路上找到一個可以取得檔名的VBA,但是它是用Function寫的!!!
如下……
  1. 'Description:取得活頁簿目錄或底下目錄的全部(指定)檔案名稱集合 - Ver. 1.05
  2. 'Parameters:wb 活頁簿、strSplitChar 分隔字元、strFolderPath 活頁簿底下目錄的路徑、strExtension 指定的副檔名
  3. 'Retrun:String
  4. 'Package:
  5. 'Reference:Microsoft Scripting Runtime
  6. 'Creater:Darren.NET - 2009/04/17
  7. 'Modify:
  8. Public Function F_File_GetFileNameArry(wb As Workbook, strSplitChar As String, strFolderPath As String, strExtension As String) As String
  9.     Dim fsoFiles As Scripting.FileSystemObject
  10.     Dim fsoExtension As Scripting.FileSystemObject
  11.     Dim files As Scripting.files
  12.     Dim file As Scripting.file
  13.     Dim strCompletePath As String
  14.     Set fsoFiles = New Scripting.FileSystemObject
  15.     Set fsoExtension = New Scripting.FileSystemObject
  16.      
  17.     '判斷路徑(strFolderPath 是否有值),若不為空完整路徑為活頁簿所在路徑+底下目錄路徑,
  18.     '                                  若是空代表為完整路徑即為活頁簿所在路徑
  19.     If strFolderPath <> "" Then
  20.         strCompletePath = wb.Path & strFolderPath
  21.     Else: strCompletePath = wb.Path
  22.     End If
  23.      
  24.     '取得完整路徑目錄下的全部檔案集合
  25.     Set files = fsoFiles.GetFolder(strCompletePath).files
  26.      
  27.     '判斷是否有指定副檔名(strExtension 是否有值),若不為空(有指定)則須過濾檔案,
  28.     '               若是指定的副檔名才加入字串;若為空表示全部檔案名稱加入字串
  29.     If strExtension <> "" Then
  30.         For Each file In files
  31.             If UCase(fsoExtension.GetExtensionName(file.Name)) = UCase(strExtension) Then
  32.                 If F_File_GetFileNameArry <> "" Then
  33.                     F_File_GetFileNameArry = F_File_GetFileNameArry + strSplitChar + file.Name
  34.                 Else: F_File_GetFileNameArry = file.Name
  35.                 End If
  36.             End If
  37.         Next
  38.     Else
  39.         For Each file In files
  40.             If F_File_GetFileNameArry <> "" Then
  41.                     F_File_GetFileNameArry = F_File_GetFileNameArry + strSplitChar + file.Name
  42.             Else: F_File_GetFileNameArry = file.Name
  43.             End If
  44.         Next
  45.     End If
  46.      
  47.     Set fsoFiles = Nothing
  48.     Set fsoExtension = Nothing
  49.     Set files = Nothing
  50.     Set file = Nothing
  51. End Function
複製代碼
小弟第一次看到這種東西,請教一下
一、Function的用途是什麼??
二、Function如何使用?
三、什麼樣子的情況下會用到Function?
初學VBA乍到twbts

回復 1# play9091
Function: 函數  ,Excel 已建立許多的函數 , 可自訂 Function 可依個人需求量身訂做 ,來使用
例: 在一般模組 (Module)   內有 Function Play9091()  
                                              '''''
                                              '''
                                      End Function
可在如圖使用

TOP

回復 2# GBKEE

剛剛試用了一下的確是可以用的,又學到一招了……

另外,關於昨天請教先進的「貼上值的」問題,今天又遇到了,我真的不知道是哪裡有問題也……把圖貼出來給您看一下!

未命名.png (108.83 KB)

未命名.png

初學VBA乍到twbts

TOP

本帖最後由 GBKEE 於 2011-7-27 13:43 編輯

回復 3# play9091
陣列索引超出範圍    的意思是找不到
檢查 WorkBooks(Main ).Sheets(Re). Cells(NoR) ....的變數是否正確

TOP

本帖最後由 play9091 於 2011-7-27 14:25 編輯

回復 4# GBKEE

是我粗心……沒有打對正確的變數……

想再請教……如果開啟檔案的時候會問,更新連結。那個視窗有沒有辦法不讓它出現……
初學VBA乍到twbts

TOP

本帖最後由 GBKEE 於 2011-7-27 15:30 編輯

回復 5# play9091
  Option Explicit 陳述式  在模組層次中強迫每個在模組裏的變數都必須明確的宣告。  如此 變數打錯立刻知道


如圖

TOP

看起來Sheets(Re). 工作表名不合法。為什麼有問號產生?

TOP

回復 5# play9091
更新連結。那個視窗有沒有辦法不讓它出現……
DEE連結  = Excel|aa.xls!aa    沒辦法

公式連結='D:\試算表\aa.xls'!aa
  1. ActiveWorkbook.UpdateLinks = xlUpdateLinksAlways
複製代碼



TOP

回復 8# GBKEE

哈!還是不行,我叫他們自已按「不要更新」好了!!!
初學VBA乍到twbts

TOP

回復 9# play9091


    Workbooks.Open Filename:=路徑,UpdateLinks:=False
學海無涯_不恥下問

TOP

        靜思自在 : 閒人無樂趣,忙人無是非。
返回列表 上一主題