键入Mismatch错误,但写入文本文件... [英] Type Mismatch error, but writing to a Text file ...

查看:81
本文介绍了键入Mismatch错误,但写入文本文件...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一条消息运行时错误'13':类型不匹配...我意识到如果我尝试添加到非数字或类似的东西会发生这种情况,但在这里我只是试图将Cell的值写入Text文件,所以它无关紧要......出了什么问题?



I am getting a message "Run-time error '13': Type mismatch ... I realise that this can happen if I am trying to add to a non-numeric or that kind of thing, but here I am just trying to write the value of a Cell to a Text file, so it doesn't matter what it is ... what is wrong ?

Sub SortAndPrepareTables()

    Dim myPath As String
    Dim myTextFile As String
    Dim myTables As String
    Dim myFile As Integer
    Dim myRow As Range
    Dim myCell As Range
    
    Range("A17:N23").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("M18:M23"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("N18:N23"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("K18:K23"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A17:N23")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    myPath = Application.ActiveWorkbook.Path
    myTextFile = "League Cup Tables.txt"
    myTables = myPath & "\" & myTextFile
    myFile = FreeFile()

    Open myTables For Output As myFile
    
    For Each myRow In Range("A17:N23").Rows
        For Each myCell In myRow.Rows
             Print #myFile, myCell.Value <<< Error occurs here
        Next myCell
        Write #myFile,
    Next myRow
        
    Close myFile

End Sub

推荐答案

我改变了我的方式,而不是使用代码...



I have changed the way I do this, and instead of using the code ...

For Each myRow In Range("A17:N23").Rows
    For Each myCell In myRow.Rows
        Print #myFile, myCell.Value <<< Error occurs here
    Next myCell
    Write #myFile,
Next myRow





...我正在使用...





... I am using ...

For myRow = 17 To 23
    For myColumn = 1 To 14
        myCell = Trim(Cells(myRow, myColumn).Value)
        Print #myFile, myCell
    Next myColumn
    Print #myFile,
Next myRow





这完美无缺!



This works perfectly !


我认为你需要将值转换为字符串;请参阅 http://www.vb6.us/tutorials/writing-files-vb6 -using-write-and-print [ ^ ]。
I think you need to convert the value to a string; see http://www.vb6.us/tutorials/writing-files-vb6-using-write-and-print[^].


这篇关于键入Mismatch错误,但写入文本文件...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆