在Mac上打开CSV文件时出现错误53 [英] Error 53 when opening CSV file on mac
问题描述
当我尝试打开CSV文件时,我得到:
When I try and open a CSV file, I get:
错误53:找不到文件
Error 53: File not found
我在第四行得到错误,Open FilePath For Input As #1
我究竟做错了什么?
I get the error on the 4th line, Open FilePath For Input As #1
What am I doing wrong?
这是我第一次打开CSV,请放心我的代码.
It is my first time opening a CSV, please be indulgent with my code.
Sub opentextfile()
Dim FilePath As String
FilePath = "/Users/christinekelly/Desktop/authors.csv"
file1 = FreeFile
Open FilePath For Input As #file1
row_number = 0
Do Until EOF(1)
Line Input #file1, LineFromFile
LineItems = Split(LineFromFile, ",")
ActiveCell.Offset(row_number, 0).Value = LineItems(2)
ActiveCell.Offset(row_number, 1).Value = LineItems(1)
ActiveCell.Offset(row_number, 2).Value = LineItems(0)
Number = row_number + 1
Loop
Close #file1
End Sub
推荐答案
So from looking at @Rebekare's answer to this question this is what worked for me.
我转到有问题的文件test.csv
,打开立即窗口,键入?ThisWorkbook.Path
,得到了HDD:Users:USER:Desktop
.
I went to the file in question test.csv
and opened the immediate window and typed ?ThisWorkbook.Path
and got HDD:Users:USER:Desktop
.
然后,我将此路径的建议串联与Application.PathSeparator
&文件名,即
I then used the suggested concatenatation of this path with Application.PathSeparator
& filename i.e.
FilePath = "HDD:Users:USER:Desktop" & Application.PathSeparator & "test.csv"
这是一种有用的方法,因为您可以获取实际的文件路径语法,然后将路径分隔符决策提供给Application
.
This is a useful approach as you get the actual file path syntax and then yield the path separator decision to the Application
.
Following on from @Mat'sMug suggestion, I found this, which opens the file dialog, and you select the file you want the full path of and it is returned via the message box.
Sub PathofFile()
OpenFile = Application.GetOpenFilename()
MsgBox OpenFile
End Sub
在同一链接内,建议使用Dir
函数测试文件路径是否有效.如果有效,则返回文件名,否则返回错误,您可以使用该错误来确定下一步操作,例如如果文件路径中存在该文件,则以下命令将返回"test.csv".
Within that same link was a suggestion for using the Dir
function to test if the filepath is valid. If valid you get the filename back, if not you get an error which you can use to determine your next action e.g. the following returns "test.csv" if it exists at that filepath.
MsgBox Dir("HDD:Users:USER:Desktop:test.csv")
如果您要对该文件进行其他操作,则可能需要添加测试,以使用Microsoft的 IsFileOpen 函数.示例如下:
If you are doing other operations with the file you might want to add a test to to see if the file is open first, using Microsoft's IsFileOpen function. An example as follows:
Sub Test
If Not IsFileOpen(FilePath) Then
Set wb = Workbooks.Open(FilePath)
End If
End Sub
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function
这篇关于在Mac上打开CSV文件时出现错误53的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!