【问题标题】:error mismatch VBA错误不匹配 VBA
【发布时间】: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 是一个返回InputOuput 的空闲文件号的函数

标签: windows vba excel


【解决方案1】:

我已经重现了你的错误。

在你的声明中,改变

Dim wrarr()

Dim wrarr

【讨论】:

  • 有时重要的是变量声明中的细微差别。但最重要的是定义导致错误的行。
猜你喜欢
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 2017-04-12
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多