excel - 有没有办法在作为字符串变量的输出中打印一个范围?
问题描述
我正在尝试将 Excel 工作表的这一部分导出到 txt 文件中。n_select
确定有多少行有数据需要导出。我试过制作Range
from n_select
,但它给了我一个类型不匹配的错误。
Private Sub Exporter_Click()
Dim n_personne As Integer
Dim n_select As String
Dim user As String
Dim fileName As String
n_personne = 1
user = Environ("username")
fileName = "Résultats Pêchés"
'DETERMINER LE NOMBRE DE RANGS DONNERS
Do Until IsEmpty(Worksheets("output").Cells(n_personne, 1).Value) = True
n_personne = n_personne + 1
Loop
'DETERMINER L'INTERVALLE A EXPORTER
n_select = "A1:" + Cells(n_personne, 5).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Open "C:\Users\" + user + "\Desktop\" + fileName + ".txt" For Output As #1
Print #1, Range(n_select)
Close #1
Call Shell("Explorer.exe ""C:\Users\" + user + "\Desktop\" + fileName + ".txt")
End Sub
解决方案
一些建议
- 使用变量、对象。让您的生活更轻松。:)
- 代替
+
,&
用于连接。 - 要将
Bulk
范围写入文本文件,请将其存储在数组中并转置。这样就不需要循环了。 - 避免循环查找最后一行。您可能想查看如何找到最后一行。
- 处理行时,避免使用
Integer
. 使用Long
.
这是你正在尝试的吗?
Option Explicit
Private Sub Exporter_Click()
Dim ws As Worksheet
Dim lRow As Long
Dim rng As Range
Dim FilePath As String
Dim fileName As String
'~~> Construct your path and filename
FilePath = "C:\Users\" & Environ("username") & "\Desktop\"
fileName = FilePath & "Résultats Pêchés.txt"
'~~> Set your worksheet
Set ws = ThisWorkbook.Sheets("Output")
With ws
'~~> Find last row in Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Identify the range that you want to write to text
Set rng = .Range("A1:A" & lRow)
End With
Dim rngLines() As Variant
Dim output As Variant
'~~> Transfer the range value to a 2D array and join
rngLines = Application.Transpose(rng.Value)
output = Join(rngLines, vbCrLf)
'~~> Write to text file
Open fileName For Output As #1
Print #1, output
Close #1
'~~> Launch the file
Call Shell("Explorer.exe " & Chr(34) & fileName & Chr(34))
End Sub
编辑
非常感谢,我真的很感激。我将如何去复制多列?数据将在 A1 到 E 最后一行的范围内。当我将变量 rng Set rng = .Range("A1:E" & lRow) 更改为此时,它会给我一个错误,即存在无效的过程或参数。– WhiteStrips 6 分钟前
这是你想要的吗?
Option Explicit
Private Sub Exporter_Click()
Dim ws As Worksheet
Dim lRow As Long
Dim rng As Range
Dim FilePath As String
Dim fileName As String
'~~> Construct your path and filename
FilePath = "C:\Users\" & Environ("username") & "\Desktop\"
fileName = FilePath & "Résultats Pêchés.txt"
'~~> Set your worksheet
Set ws = ThisWorkbook.Sheets("Output")
With ws
'~~> Find last row in Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Identify the range that you want to write to text
Set rng = .Range("A1:E" & lRow)
End With
Dim rw As Variant, col As Variant
Dim output As String
For Each rw In rng.Rows
For Each col In rng.Cells
output = output & col.Value & ","
Next col
output = output & vbNewLine
Next rw
'~~> Write to text file
Open fileName For Output As #1
Print #1, output
Close #1
'~~> Launch the file
Call Shell("Explorer.exe " & Chr(34) & fileName & Chr(34))
End Sub
推荐阅读
- python-3.x - 如何为每个脚本运行创建一个动态列表以读取和更新
- c# - 使用 TextTransform.exe 从命令行生成 T4 的问题
- angular - 带有 Angular 的 Docker 给出错误“未找到 'AppModule' 的 NgModule 元数据中的错误。”
- r - R 包 Googleway。函数:google_map()。参数: search_box 。控制参数:SIZE
- node.js - 无法解码亚马逊广告 API 下载报告
- xml - XQuery 获取没有命名空间的节点
- javascript - NPM 未安装所需的依赖项
- php - Yii2 控制台:app\models Class not found 但 common\models found
- angular - 为什么切换一个复选框也会切换其他一些复选框?
- android - 协程父作业被取消异常