Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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电子表格宏_Excel_Vba - Fatal编程技术网

Excel电子表格宏

Excel电子表格宏,excel,vba,Excel,Vba,我正在使用以下Excel宏从Excel电子表格生成文本文件 Option Explicit Public Sub WriteToTextFile() Dim ws As Worksheet Set ws = ActiveSheet 'better by its name: ThisWorkbook.Worksheets("Sheet1") Dim LastRow As Long LastRow = ws.Cells(ws.Rows.Count, "A").En

我正在使用以下Excel宏从Excel电子表格生成文本文件

Option Explicit

Public Sub WriteToTextFile()
    Dim ws As Worksheet
    Set ws = ActiveSheet 'better by its name: ThisWorkbook.Worksheets("Sheet1")

    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    Dim LastCol As Long
    LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

    Open "C:\Users\Admin\Desktop\123.txt" For Output As #1

    Dim iRow As Long
    For iRow = 2 To LastRow
        Dim OutputLine As String

        Dim iCol As Long
        For iCol = 1 To LastCol
            If iCol = 1 Then 'first column (is different)
                OutputLine = ws.Cells(iRow, iCol).Text
            Else 'append other columns
                OutputLine = OutputLine & "," & ws.Cells(1, iCol).Text & ": " & ws.Cells(iRow, iCol).Text
            End If
        Next iCol
        'Debug.Print OutputLine
        Print #1, OutputLine 'output the full line to the text file
    Next iRow

    Close #1
    Shell "notepad.exe ""C:\Users\Admin\Desktop\123.txt""", vbNormalFocus
End Sub
Excel电子表格数据示例如下

ID1 | ID2 |ID3
97  | 12  | 47
08  | 09  | 54
17  | 46  | 07
我的最终输出是

97,ID2: 12,ID3: 47
08,ID2: 09,ID3: 54
17,ID2: 46,ID3: 07
在最终输出时,我无法获取行数据(标题;请参阅下面的示例)。有人能帮我吗

ID1,ID2,ID3
97,ID2: 12,ID3: 47
08,ID2: 09,ID3: 54
17,ID2: 46,ID3: 07

提前谢谢

编辑:由于上一个答案没有解决问题而更新

该回路在样本数据上进行了测试,应按预期工作:

    For iCol = 1 To LastCol
        If iRow = 1 Then
            If OutputLine = vbNullString Then
                OutputLine = ws.Cells(iRow, iCol).Text
            Else
                OutputLine = OutputLine & ", " & ws.Cells(iRow, iCol).Text
            End If
        ElseIf iCol = 1 Then 'first column (is different)
            OutputLine = ws.Cells(iRow, iCol).Text
        Else 'append other columns
            OutputLine = OutputLine & "," & ws.Cells(1, iCol).Text & ": " & ws.Cells(iRow, iCol).Text
        End If
    Next iCol

@实际上,M.Schalk总是声明变量是最佳实践。此外,不需要在循环之外声明变量<代码>Dim语句未执行。此时,感谢您的反馈。注释已被删除。好吧,使用示例数据尝试您对上述代码的建议,您将看到它没有生成所需的输出。因此,答案不能解决问题或/也没有用处。@Pᴇ很公平,你是对的。我查看了一些示例数据,并更新了我的答案以生成所需的输出。@well结构在我看来有点奇怪,但实际上它是有效的。我的建议是在iRow循环(原始代码)的第一个
之前添加一行
Print#1 Join(WorksheetFunction.Transpose(WorksheetFunction.Transpose(ws.Range(“A1”,ws.Cells(1,LastCol)).Value)),“,”,”
。不,OP太懒了,我让他这么做的时候,他甚至没有做任何尝试(我已经在上面的问题中写了完整的代码,所以这已经超出了他在这里的预期)。如果他没有表现出任何努力,我就不会这么做。•事实上,我只是想向你展示另一种方法,因为看到
If…ElseIf
construct wich需要在每一列和每一行上运行,我感到有点奇怪(即使只需要第一行)这可能会让它变慢(但我没有测试,只是看起来像)。我只是通读了你链接的前一个问题,OP确实看起来很懒。谢谢你的输入,一直在寻求改进!