我需要帮助将导入的文件名插入excel的工作表中。我从某处在线获得了此代码,只是希望将导入的文件名填充在工作簿中已经存在的名为“ LOGSTAT报告”的工作表上的单元格中。
这是我拥有的宏
Option Explicit
' ************************************************
' Variables For File Open Dialogue Box
' ************************************************
Public strDialogueFileTitle As String
Public strFilt As String
Public intFilterIndex As Integer
Public strCancel As String
Public strWorkbookNameAndPath As String
Public strWorkbookName As String
Public strWorksheetName As String
Public Sub OpenAFileAndCopyData()
Dim wkbMasterWorkbook As Workbook
Dim wksMasterWorksheet As Worksheet
Dim wkbImportedWorkbook As Workbook
Dim wksImportedWorksheet As Worksheet
Dim rngImportCopyRange As Range
Application.ScreenUpdating = False
Set wkbMasterWorkbook = ThisWorkbook
Set wksMasterWorksheet = Sheets("READIsim Output")
' ****************************************************************************
' Set Up Filters For Which Files Should Show In The Open File Dialog Box
' ****************************************************************************
strFilt = "CSV Files (*.csv),*.csv,"
' ****************************************************************************
' Set Up The Prompt In The Dialogue Box
' ****************************************************************************
intFilterIndex = 1
strDialogueFileTitle = "Select The Workbook You Want To Import"
' ****************************************************************************
' Present the Open File Dialogue To The User
' ****************************************************************************
Call OpenFileDialogue
' ****************************************************************************
' Notify The User If No File Was Successfully Opened
' ****************************************************************************
If strCancel = "Y" Then
MsgBox ("An Open Error Occurred Importing Your File Selection")
Exit Sub
End If
' ****************************************************************************
' Set Imported Workbook and Worksheet Variables
' ****************************************************************************
Set wkbImportedWorkbook = ActiveWorkbook
Set wksImportedWorksheet = wkbImportedWorkbook.Sheets(1)
' ****************************************************************************
' Choose This Or The Next
' Copy The Data (Paste Special Will Be The Next Example)
' ****************************************************************************
Set rngImportCopyRange = Range(wksImportedWorksheet.Cells(1, 1), Cells(500, 1)).EntireRow
rngImportCopyRange.Copy wksMasterWorksheet.Cells(1, 1)
wkbMasterWorkbook.Activate
Application.DisplayAlerts = False
wkbImportedWorkbook.Close Savechanges:=False
Application.DisplayAlerts = True
wksMasterWorksheet.Activate
wksMasterWorksheet.Cells(1, 1).Select
Application.ScreenUpdating = True
End Sub
Private Sub OpenFileDialogue()
' ************************************************
' Display a File Open Dialogue Box For The User
' ************************************************
strCancel = "N"
strWorkbookNameAndPath = Application.GetOpenFilename _
(FileFilter:=strFilt, _
FilterIndex:=intFilterIndex, _
Title:=strDialogueFileTitle)
' ************************************************
' Exit If No File Selected
' ************************************************
If strWorkbookNameAndPath = "" Then
MsgBox ("No Filename Selected")
strCancel = "Y"
Exit Sub
ElseIf strWorkbookNameAndPath = "False" Then
MsgBox ("You Clicked The Cancel Button")
strCancel = "Y"
Exit Sub
End If
' ******************************************************
' Now That You Have The User Selected File Name, Open It
' ******************************************************
Workbooks.Open strWorkbookNameAndPath
ActiveSheet.Columns("B:AN").NumberFormat = "General"
End Sub
感谢您的帮助。
答案 0 :(得分:0)
您的第三行代码是:
Workbooks.Open strWorkbookNameAndPath
在此行之后,您可以添加
MsgBox strWorkbookNameAndPath
获得一个带有已打开文件名称的msgBox。
您可以使用
Debug.Print strWorkbookNameAndPath
在立即窗口中查看打开的文件的名称(在VBE中为Ctrl G)。
使用命令
Worksheets("LOGSTAT Report").Range("A1").Value = strWorkbookNameAndPath
文件名插入工作表“ LOGSTAT报告”的单元格“ A1”中。