gpt4 book ai didi

vba - 编译错误: Sub or Function not defined after adding form button

转载 作者:行者123 更新时间:2023-12-01 16:41:58 24 4
gpt4 key购买 nike

下面是我编写的代码,用于将数据从多个工作表复制到新的主工作表中,并且工作正常,直到我对代码进行了一些更改以添加表单按钮以在单击按钮时导入数据我收到错误

Compile Error: Sub or Function not defined

有人可以帮助我吗?以下是 ImportForm 中的 btnImport 按钮的代码:

Private Sub btnImport_Click()

Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object

Dim folderpath As String

folderpath = Range("I5").Value

If Range("I5").Value = "" Then
MsgBox "Select the folder which contains the reports.", vbInformation, "Cannot Import."
Range("I2").Select
ElseIf FileFolderExists(folderpath) = False Then
MsgBox "Selected Folder Does Not Exist.", vbInformation, "Cannot Import."
Range("I5").Select
ElseIf Dir(folderpath, vbDirectory) = "" Then
MsgBox "Selected Folder Not Found.", vbInformation, "Invalid Folder Name."
Range("I5").Select

Else
Me.lblWait.Visible = True
Me.btnCancel.Visible = False
Me.btnImport.Visible = False

Application.ScreenUpdating = False
Application.StatusBar = "Collecting Data, Please Wait..."
Set mergeObj = CreateObject("Scripting.FileSystemObject")

'change folder path of excel files here
Set dirObj = mergeObj.Getfolder("folderpath")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)

On Error Resume Next

'Change B3:H to the range your working on and also B in B65536 to any column required.
bookList.Worksheets(1).Range("B3:H350" & bookList.Worksheets(1).Range("B65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(1).Activate

'Below only change "B" column name to your required column name
Range("B65536").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
bookList.Close
Next

End If
End Sub

最佳答案

请删除此行中的引号,

Set dirObj = mergeObj.Getfolder("folderpath")

由于它是一个变量,这将导致错误。

应该是这样的

Set dirObj = mergeObj.Getfolder(folderpath)

关于vba - 编译错误: Sub or Function not defined after adding form button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com