问题
Assuming I have 3 files all in same folder (item.xlsx, master.xlsx, transfer.xlsm) Main purpose is to transfer data from item to master.
I do all the codes inside transfer.xlsm and allow users to input file name and column mappings. I have been doing few hours for the codes and have tested several times and is working perfectly fine. With a click of a button is able to read data from item.xlsx and copy over to master.xlsx according to the column mapping.
However problem arise when i close all 3 files and reopen again. I open up all 3 file, when i click on the button on transfer,xlsm it show file not found which is the error handling i did. I did tried creating a new folder on my desktop and create a brand new transfer.xlsm inside, i copy the item and master file over and my code into my new button. It actually able to work but when i close and reopen in that new folder is not working,
Basically is working fine when i am working on it, when i close it totally and reopen it is unable to detect the 2 files.
cell values are entered inside transfer.xlsm according to user input
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
I created this transfer.xlsm so that i can send it to people if they want to copy chunks of data from one excel to another instead of copy paste row by row. Hope someone can give me some guidance
回答1:
Based on the information you provided, I am assuming that the information provided by the users is just the file name without the extension: item
or master
, and not the full file path C:\SampleFolder\item.xlsx
or C:\SampleFolder\master.xlsx
. Additionally, I am assuming when you run this code all three files must be in the same folder.
If this is the case, try using ThisWorkbook.Path
, you can apply this to your Source and Destination paths to ensure that the appropriate file path is being used.
Dim sPath as String
sPath = ThisWorkbook.Path + "\"
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"
If FileExists(sPath + source) = False Or FileExists(sPath + destination) = False Then
MsgBox "File not found, please double check file name and make sure is in the same folder"
Exit Sub
End If
...
来源:https://stackoverflow.com/questions/57396560/vba-unable-to-read-files-after-closing-and-reopen-excel