我在工作中有一个问题,明天我需要整理出来,只有我不做VB所以在VBA是废话。 我需要将单元格的内容保存到另一个单元格命名的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来编写文件?我认为真正的问题是参考我需要的细胞,我想?任何帮助都会很棒:)
答案 0 :(得分:3)
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
做到了!聚集在一起。感谢您的建议。
答案 1 :(得分:1)
你必须使用VBA吗?我猜你是否知道你使用C#或其他.NET语言是什么StreamWriter。如果是这样,请查看EPPlus,您可以轻松遍历Excel工作表并使用.NET Framework。
答案 2 :(得分: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
贷方转到: