gpt4 book ai didi

vba - Excel VBA .Saveas() 函数保留格式

转载 作者:行者123 更新时间:2023-12-04 20:33:24 30 4
gpt4 key购买 nike

我正在尝试使用下面的 saveas() 函数将 excel 工作表输出到 xltext 文件。该文件生成良好,但数字具有特殊格式#,###.00。输出为“1,000.00”,而不仅仅是 1,000.00。如何删除这些双引号。

Dim tab_output_line, tab_output_head, tab_source As String
Dim File_Location As String

tab_output_line = "Upload_PO_LineItem"
tab_source = "PBOOK"

File_Location = Sheets(tab_source).Range("S5").Value

Sheets(tab_output_line).Select
ChDir File_Location
ActiveWorkbook.SaveAs Filename:= _
File_Location + tab_output_line + ".txt", FileFormat:=xlText, _
CreateBackup:=False

最佳答案

您当前的代码正在制作 制表符分隔 文件。包含逗号(即使已格式化)的单元格通过用双引号括起来而受到“保护”。

为了避免这种“保护”,试试这样的代码:

Sub tony2()
Dim N As Long, i As Long, j As Long, Record As String
Dim M As Long

N = Cells(Rows.Count, 1).End(xlUp).Row
Close #2
Open "C:\Users\Garys\desktop\tony.txt" For Output As #2

For i = 1 To N
Record = ""
M = Cells(i, Columns.Count).End(xlToLeft).Column
For j = 1 To M
Record = Record & vbTab & Cells(i, j).Text
Next j
Record = Mid(Record, 2)
Print #2, Record
Next i
Close #2
End Sub

输入:

enter image description here

和输出:

enter image description here

关于vba - Excel VBA .Saveas() 函数保留格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46430707/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com