【发布时间】:2018-02-22 14:23:03
【问题描述】:
enter image description here我很难弄清楚我的代码中发生了什么。它一直说“运行时错误:'13'”。我该如何处理这个错误?
Dim ws As Worksheet
Dim rearr(), wrarr()
Dim fName As String
Dim rowno As Long, colno As Long, rec As Long
Dim cnt As Long, cnt2 As Long
Dim delim As String
'specify output sheet
Set ws = Worksheets("1.Locations")
'specify text file to read (.csv in this example)
fName = "C:\Users\HP\Desktop\names.csv"
'set text file delimiter
delim = "," 'for Tab delimiter use delim = Chr(9)
ifnum = FreeFile
'set start row/col for text data to be placed ("A1" in this example)
rowno = 3 'row 1
colno = 2 'col A
With ws
Open fName For Input Access Read As #ifnum
rec = 0
Do While Not EOF(ifnum)
Line Input #ifnum, tmpvar
rec = rec + 1
'Put whole record into array
ReDim Preserve rearr(1 To rec)
rearr(rec) = tmpvar
'Split fields into a second array
wrarr = Split(rearr(rec), delim)
cnt2 = UBound(wrarr)
'Write fields out to specified ws range, one row per record
For cnt = 0 To cnt2
ws.Cells(rowno, colno + cnt) = wrarr(cnt)
Next cnt
rowno = rowno + 1
Loop
Close #ifnum
End With
【问题讨论】:
-
ifnum = FreeFile这些都不存在。 -
你在哪里声明了 ifnum?
-
@Zerk 从技术上讲,它们确实存在...作为隐式变量。 ;)
-
@Zerk,
FreeFile是一个返回Input和Ouput的空闲文件号的函数