Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当设置了断点时,用于导出到文本文件的EXCEL VBA代码才起作用_Excel_Vba - Fatal编程技术网

仅当设置了断点时,用于导出到文本文件的EXCEL VBA代码才起作用

仅当设置了断点时,用于导出到文本文件的EXCEL VBA代码才起作用,excel,vba,Excel,Vba,我有一些代码可以将数据从电子表格导出到逗号分隔的文件。如果在代码中的任何位置设置了断点,数据将按预期导出到文件中。如果没有断点,则创建的文件中没有任何数据。考虑到这是一个时间问题,我在代码中尝试了一个等待周期,但这并没有解决这个问题。代码如下: Private Sub WriteDataToFile() Dim i As Long Dim iLastRow As Integer Dim strFile As String Dim FSO As FileSystemObject Dim FSO

我有一些代码可以将数据从电子表格导出到逗号分隔的文件。如果在代码中的任何位置设置了断点,数据将按预期导出到文件中。如果没有断点,则创建的文件中没有任何数据。考虑到这是一个时间问题,我在代码中尝试了一个等待周期,但这并没有解决这个问题。代码如下:

Private Sub WriteDataToFile()  
Dim i As Long
Dim iLastRow As Integer
Dim strFile As String
Dim FSO As FileSystemObject
Dim FSOFile As TextStream
Dim strData As String
Dim s As String

strFile = "C:\Temp\DSGELIG.txt"
'  Delete the file if it already exists
DeleteFile (strFile)

'  Determine the last row
iLastRow = 50
For i = 2 To 65000
    strData = Range("B" + CStr(i))
    If Len(strData) < 1 Then
        iLastRow = i - 1
        Exit For
    End If
Next i


'  Create the file system object
Set FSO = New FileSystemObject
Set FSOFile = FSO.OpenTextFile(strFile, ForWriting, True)




For i = 2 To iLastRow
    strData = ""
    With Worksheets(1)
        'Debug.Print Range("B" + CStr(i))
        strData = """"
        '  Patient Name
        strData = strData + Range("B" + CStr(i))
        strData = strData + """" + "," + """"
        '  SSN / Policy #
        strData = strData + Range("C" + CStr(i))
        strData = strData + """" + "," + """"
        '  Birthdate
        strData = strData + CStr(Range("D" + CStr(i)))
        strData = strData + """" + "," + """"
        '  Admit Date
        strData = strData + CStr(Range("E" + CStr(i)))
        strData = strData + """" + "," + """"
        '  Admit Date - 2
        strData = strData + CStr(Range("F" + CStr(i)))
        strData = strData + """" + "," + """"
        '  Account Number
        strData = strData + CStr(Range("G" + CStr(i)))
        strData = strData + """" + "," + """"
        '  Insurance Code
        strData = strData + Range("H" + CStr(i))
        strData = strData + """" + "," + """"
        '  Financial Class
        strData = strData + Range("I" + CStr(i))
        strData = strData + """" + "," + """"
        '  Location
        strData = strData + Range("J" + CStr(i))
        strData = strData + """"

        '  Write the record to the file
        FSOFile.WriteLine (strData)
    End With


Next i

FSOFile.Close

End Sub
Private子写状态文件()
我想我会坚持多久
Dim iLastRow作为整数
作为字符串的Dim strFile
将FSO设置为文件系统对象
将文件设置为文本流
将strData设置为字符串
像线一样变暗
strFile=“C:\Temp\DSGELIG.txt”
'如果文件已存在,请删除该文件
删除文件(strFile)
'确定最后一行
iLastRow=50
对于i=2至65000
标准数据=范围(“B”+CStr(i))
如果Len(strData)<1,则
iLastRow=i-1
退出
如果结束
接下来我
'创建文件系统对象
Set FSO=新文件系统对象
设置FSOFile=FSO.OpenTextFile(strFile,ForWriting,True)
对于i=2到iLastRow
strData=“”
附工作表(1)
'调试打印范围(“B”+CStr(i))
strData=“”“
“患者姓名
标准数据=标准数据+量程(“B”+CStr(i))
strData=strData++、“++”
“SSN/政策#
标准数据=标准数据+量程(“C”+CStr(i))
strData=strData++、“++”
“生日
strData=strData+CStr(范围(“D”+CStr(i)))
strData=strData++、“++”
“录取日期
strData=strData+CStr(范围(“E”+CStr(i)))
strData=strData++、“++”
“录取日期-2
strData=strData+CStr(范围(“F”+CStr(i)))
strData=strData++、“++”
“账号
strData=strData+CStr(范围(“G”+CStr(i)))
strData=strData++、“++”
“保险法
标准数据=标准数据+量程(“H”+CStr(i))
strData=strData++、“++”
"金融阶层",
标准数据=标准数据+量程(“I”+CStr(I))
strData=strData++、“++”
“位置
标准数据=标准数据+量程(“J”+CStr(i))
strData=strData+“”“
'将记录写入文件
FSOFile.WriteLine(strData)
以
接下来我
F文件,关闭
端接头

提前感谢您-我已经有一段时间没有做任何VBA了。

更新了我如何从头开始做这件事:

Option Explicit

Sub Test()
    Dim FSO As New FileSystemObject
    Dim ts As TextStream
    Dim WS As Worksheet
    Dim arrData(9) As String
    Dim strFile As String, strData As String
    Dim iLastRow As Integer, i As Integer, j As Integer

    strFile = "C:\Temp\DSGELIG.txt"
    Set ts = FSO.CreateTextFile(strFile, True)
    Set WS = ThisWorkbook.Worksheets(1)
    iLastRow = WS.UsedRange.SpecialCells(xlCellTypeLastCell).Row

    With WS
        For i = 2 To iLastRow
            For j = 2 To 10
                arrData(j - 2) = """" & CStr(.Cells(i, j)) & """"
            Next j

            strData = Join(arrData, ",")
            ts.WriteLine Left(strData, Len(strData) - 1)
        Next i
    End With

    ts.Close

    Set ts = Nothing
    Set FSO = Nothing
End Sub
不需要代码来删除文件,因为您只需在
CreateTextFile()
中将
overwrite
设置为
true


我已经测试过了,在编写文件时没有遇到任何问题。我甚至尝试根据原始导出创建一组数据,这些数据可能与您的数据相当,但效果很好。

我重新编写了一些Kittoes代码,如下所示:

Sub Test()

    Dim FSO As New FileSystemObject
    Dim ts As TextStream
    Dim strFile As String, strData As String
    Dim iLastRow As Integer, i As Long, c As Long

    strFile = "C:\Temp\DSGELIG.txt"

    '  Determine the last row
    iLastRow = Cells(Rows.Count, 2).End(xlUp).Row

    '  Create the file system object
    Set ts = FSO.CreateTextFile(strFile, True)
    With Worksheets(1)
        For i = 2 To iLastRow
            strData = ""
            For c = 2 To 10
                strData = "'" & strData & .Cells(i, c) & "',"
            Next c

            '  Write the record to the file without the last comma
            ts.WriteLine Left(strData, Len(strData) - 1)
        Next i
    End With

    ts.Close

    Set ts = Nothing
    Set FSO = Nothing

End Sub

我必须说,我从未使用过FSO.CreateFile和其他。我通常使用旧的
Print#1
语法。如果可以的话,我通常会尽量避免任何额外的参考。

与工作表(1)
没有效果,因为您没有在.Range前面加上点@这是真的。在他的原始代码上有一些地方需要改进,因此我将尝试编辑我的帖子,并包括一些必要的/建议的更改。如果将带有…的
放在
之外,对于I…。
@iDevlop Yeah=P,这里有很多事情可以更有效地完成。我会尽量把它们都包括进来…我已经做了建议的更改-谢谢你指出我可以直接覆盖文件,但是,我仍然有同样的问题。如果代码中有一个breakpooint,数据就会写入文件。如果我没有断点,我会得到一个没有任何数据的0字节文件。非常奇怪的行为。