【发布时间】:2019-08-07 14:20:27
【问题描述】:
假设我在同一个文件夹中有 3 个文件(item.xlsx、master.xlsx、transfer.xlsm) 主要目的是将数据从项目传输到主文件。
我在 transfer.xlsm 中编写所有代码,并允许用户输入文件名和列映射。我已经为代码做了几个小时并且已经测试了几次并且工作得很好。只需单击一个按钮,就可以从 item.xlsx 读取数据并根据列映射复制到 master.xlsx。
但是当我关闭所有 3 个文件并重新打开时出现问题。我打开所有 3 个文件,当我单击传输按钮时,xlsm 它显示未找到文件,这是我所做的错误处理。我确实尝试在我的桌面上创建一个新文件夹并在其中创建一个全新的 transfer.xlsm,我将项目和主文件复制过来,并将我的代码复制到我的新按钮中。它实际上可以工作,但是当我关闭并在该新文件夹中重新打开时不起作用,
当我处理它时基本上工作正常,当我完全关闭它并重新打开它时无法检测到这两个文件。
根据用户输入在 transfer.xlsm 中输入单元格值
source = Cells(5, 2)
sourceSheet = Cells(6, 2)
sourceSheetRow = Cells(7, 2) - 1
destination = Cells(8, 2)
destinationSheet = Cells(9, 2)
destinationSheetRow = Cells(10, 2) - 1
source = source + ".xlsx"
destination = destination + ".xlsx"
rows = Cells(11, 2)
If FileExists(source) = False Or FileExists(destination) = False Then
MsgBox "File not found, please double check file name and make sure is in the same folder"
Exit Sub
End If
For i = 1 To rows
...
Next i
Function FileExists(FilePath As String) As Boolean
Dim TestStr As String
TestStr = ""
On Error Resume Next
TestStr = Dir(FilePath)
On Error GoTo 0
If TestStr = "" Then
FileExists = False
Else
FileExists = True
End If
End Function
我创建了这个 transfer.xlsm,以便我可以将它发送给人们,如果他们想将数据块从一个 excel 复制到另一个而不是逐行复制粘贴。希望有人能给我一些指导
【问题讨论】:
-
可以添加单元格值的截图吗?
标签: excel vba file-not-found