我正在尝试在VBA中编写代码来复制一系列单元格。我已经成功完成了这一步但却陷入了下一步。这是第一部分:
Range("L1", Range("L1").End(xlDown)).Select
Selection.Copy
接下来,我希望宏从excel选项卡中打开一个具有指定名称的新文本文件(.txt),然后插入复制的单元格。
如何打开文本文件?
谢谢
答案 0 :(得分:0)
以下是许多可能的参考资料之一(通过Google):http://www.homeandlearn.org/write_to_a_text_file.html
以下是他们提供的示例 - 您更好地阅读文章然后适应您的方案:
Open FilePath For Output As #2
For i = 1 To LastRow
For j = 1 To LastCol
If j = LastCol Then
CellData = CellData + Trim(ActiveCell(i, j).Value)
Else
CellData = CellData + Trim(ActiveCell(i, j).Value) + ","
End If
Next j
Write #2, CellData
CellData = ""
Next i
答案 1 :(得分:0)
在第一个标签中添加 L 列,请尝试:
Sub dural()
Dim wb1 As Workbook, wb2 As Workbook
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Add
wb1.Activate
Sheets(1).Select
tabname = ActiveSheet.Name
Range("L1", Range("L1").End(xlDown)).Copy
wb2.Activate
ActiveSheet.Paste
wb2.SaveAs Filename:=tabname & ".txt", FileFormat:=xlTextWindows
wb2.Saved = True
wb2.Close
End Sub