-1

我需要将一个单元格的内容保存到另一个单元格命名的 xml 文件中。

我需要对工作表中的每一行执行此检查。

Sub FormatRange()

    Dim rng As Range
    Dim row As Range
    Dim cell As Range

    Set rng = Range("A1:AG686")

    For Each row In rng.Rows


    Next row

End Sub

对于每一行,我需要将单元格保存AG到以同一行中的xml单元格命名的文件中。C

我猜我可以使用 StreamWriter 来编写文件。我认为真正的问题是引用我需要的单元格。

4

3 回答 3

4
 Sub FormatRange()

 Dim rng As Range
Dim row As Range
Dim cell As Range

Set rng = Range("A2:AG686")

' Declare a FileSystemObject.
Dim fso As FileSystemObject

' Create a FileSystemObject.
Set fso = New FileSystemObject
' Declare a TextStream.
Dim stream As TextStream

' Create a TextStream.

For Each row In rng.Rows
        Dim path As String
        path = "C:\vba\" & row.Cells(1, 4) & ".xml"
       ' MsgBox (path + row.Cells(1, 33))
        Set stream = fso.CreateTextFile(path, True)
        stream.Write row.Cells(1, 33)


Next row
stream.Close

End Sub

做到了!聚集我的方式。感谢您的建议。

于 2013-08-06T22:22:17.473 回答
1

你必须使用VBA吗?我猜您是否知道 StreamWriter 是什么,您使用 C# 或其他 .NET 语言。如果是这样,请查看 EPPlus,您可以轻松地遍历 Excel 工作表并使用 .NET Framework。

http://epplus.codeplex.com/

于 2013-08-06T21:52:03.377 回答
0

这是我的灵活解决方案,带有右键单击操作和文件选择对话框:

Public Sub CellSaver()
    Dim cell
    For Each cell In Application.Selection.Cells
        With Application.FileDialog(msoFileDialogSaveAs)
          .Title = "Please select the file to save cell contents"
          If .Show Then
            With CreateObject("Scripting.FileSystemObject").CreateTextFile(.SelectedItems(1), True)
                .Write cell
                .Close
            End With
          End If
        End With
    Next cell
End Sub

Private Sub Workbook_Open()
    With Application.CommandBars("Cell").Controls.Add(Temporary:=True)
        .Caption = "Save to file"
        .Style = msoButtonCaption
        .OnAction = "'CellSaver'"
    End With
End Sub

学分转到:

于 2019-03-19T22:22:10.197 回答