简体   繁体   English

Excel宏的以下VBA代码有什么问题?

[英]What is wrong with the following VBA code for Excel macros?

The following code is VBA code for an Excel macros. 以下代码是Excel宏的VBA代码。 The objective is to read input from the file Impeller_hub.dat and to write it into copy_hub.dat . 目标是从文件Impeller_hub.dat读取输入并将其写入copy_hub.dat The error message I received stated that there's a type mismatch, Run-time error '13'. 我收到的错误消息表明存在类型不匹配,运行时错误“13”。 Where is the error and how can it be rectified? 错误在哪里以及如何纠正?

Private Sub fn_write_to_text_Click()
   Dim FilePath As String
   Dim CellData As String
   Dim LastCol As Long
   Dim LastRow As Long

   Dim fso As FileSystemObject
   Set fso = New FileSystemObject
   Dim stream As TextStream
   Dim stream2 As String

   LastCol = ActiveSheet.UsedRange.Columns.Count
   LastRow = ActiveSheet.UsedRange.Rows.Count
    stream2 = "C:\Users\devanandd\Desktop\copy_hub.dat"

   Set stream = fso.OpenTextFile("C:\Users\devanandd\Desktop\Files\NUMECA\Impeller_Hub.dat", stream2, True)

   CellData = ""

   For i = 1 To LastRow
      For j = 1 To LastCol
         CellData = Trim(ActiveCell(i, j).Value)
         stream.WriteLine "The Value at location (" & i & "," & j & ")" & CellData
      Next j
   Next i

   stream.Close
   MsgBox ("Job Done")
End Sub

Something like this works for me (using as much from your code as possible): 这样的东西对我有用(尽可能多地使用你的代码):

Option Explicit
Option Private Module

Private Sub fn_write_to_text_Click()

    Dim FilePath        As String
    Dim CellData        As String
    Dim LastCol         As Long
    Dim LastRow         As Long

    Dim fso             As Object
    Dim stream          As Object
    Dim stream2         As String

    Dim i               As Long
    Dim j               As Long

    Set fso = CreateObject("Scripting.FileSystemObject")

    LastCol = ActiveSheet.UsedRange.Columns.Count
    LastRow = ActiveSheet.UsedRange.Rows.Count
    stream2 = "C:\YOURPATH\Desktop\aaa.txt"

    'Uncomment the next line if you do not have the file
    'Set stream = fso.CreateTextFile(stream2, True)
    Set stream = fso.OpenTextFile(stream2, 8) '8 is ForAppending

    CellData = ""

    For i = 1 To LastRow
        For j = 1 To LastCol
            CellData = Trim(ActiveCell(i, j).value)
            stream.WriteLine "The Value at location (" & i & "," & j & ")" & CellData
        Next j
    Next i

    stream.Close
    MsgBox ("Job Done")

End Sub

The code below would work if you have the file aaa.txt on your desktop. 如果您的桌面上有文件aaa.txt ,则以下代码可以使用。 If you do not have it write: 如果你没有它写:

Set stream = fso.CreateTextFile(stream2, True) to create it and delete the line Set stream = fso.OpenTextFile(stream2,8) . Set stream = fso.CreateTextFile(stream2, True)以创建它并删除行Set stream = fso.OpenTextFile(stream2,8)

If you want to read from file Impeller_hub.dat and to write to file copy_hub.dat , then you need two TextStream variables, and two separate calls of fso.OpenTextFile : one with second argument ForReading , the other with second argument ForWriting . 如果你想从文件Impeller_hub.dat读取并写入文件copy_hub.dat ,那么你需要两个TextStream变量,以及两个单独的fso.OpenTextFile调用:一个带有第二个参数ForReading ,另一个带有第二个参数ForWriting

Or, if you want to add data to the end of file Impeller_hub.dat , then - as CLR already wrote - the second argument for fso.OpenTextFile should be ForAppending . 或者,如果要将数据添加到文件Impeller_hub.dat的末尾,那么 - 正如CLR已经写的那样 - fso.OpenTextFile的第二个参数应该是ForAppending

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM