仅返回getopenfilename中的文件名

时间:2013-11-29 07:59:44

标签: excel vba excel-2010

有什么方法可以从这个声明中获取文件名?我正试图从这个声明中取出文件名,这样我就可以继续我的编码..

基本上我正在尝试打开多个工作簿并根据工作簿的名称打开多个工作表。

        strWorkbookNameAndPath = Application.GetOpenFilename _
       (FileFilter:=strFilt, _
        FilterIndex:=intFilterIndex, _
        Title:=strDialogueFileTitle, _
        MultiSelect:=True)

       fileArraySize = UBound(strWorkbookNameAndPath, 1) 
       nameArr = Split(strWorkbookNameAndPath, "\")
       strName = nameArr(UBound(nameArr))
       Worksheets.Add().Name = strName

       For i = 1 To fileArraySize

       Set wkbImportedWorkbook = Workbooks.Open(strWorkbookNameAndPath(i))
       Set wksImportedWorksheet = wkbImportedWorkbook.Sheets("Sheet1")
       With wksImportedWorksheet
       lrow = .Range("A" & .Rows.count).End(xlUp).Row
       Set rngImportCopyRange = .Range("A1:A" & lrow).EntireRow
       rngImportCopyRange.Copy
       wksMasterWorksheet.Range("A" & Rows.count).End(xlUp).Offset(1, 0).PasteSpecial        xlPasteValues
End With
wkbImportedWorkbook.Close
Set wkbImportedWorkbook = Nothing
Set wksImportedWorksheet = Nothing
Next i

1 个答案:

答案 0 :(得分:1)

添加以下代码:

dim nameArr
dim strName as string

nameArr = Split(strWorkbookNameAndPath, "\")
strName = nameArr(UBound(nameArr))

修改

strWorkbookNameAndPath = Application.GetOpenFilename _
  (FileFilter:="", _
  FilterIndex:=0, _
  Title:="", _
  MultiSelect:=True)

fileArraySize = UBound(strWorkbookNameAndPath, 1)

For i = 1 To fileArraySize

  nameArr = Split(strWorkbookNameAndPath(i), "\")
  strName = nameArr(UBound(nameArr))
  Worksheets.Add().Name = strName

  Set wkbImportedWorkbook = Workbooks.Open(strWorkbookNameAndPath(i))
  Set wksImportedWorksheet = wkbImportedWorkbook.Sheets("Sheet1")
  With wksImportedWorksheet
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    Set rngImportCopyRange = .Range("A1:A" & lrow).EntireRow
    rngImportCopyRange.Copy
    wksMasterWorksheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
  End With
  wkbImportedWorkbook.Close
  Set wkbImportedWorkbook = Nothing
  Set wksImportedWorksheet = Nothing
Next i