Excel自定义格式添加“之前和之后的logging

我想添加"之前我的logging在Excel中。
我也想在每条logging之后添加"|

我在VBA中使用自定义格式。

我的格式是"\""@\""|"

我的结果是

 """1111""|" """Noel Gallagher""|" | """Individual""|" """Employee""|" """111""|" """Main Road""|" """Town""|" """""|" """County"|" """City""|" | | | | | | | | | | | | | | | """IE""|" """AAA""|" """Value""|" 

我需要

 "1111"| "Noel Gallagher"|""|"Individual"|"Employee"|"111"|"Main Road"|"Town"|""|"County"|"City"|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|"IE"|"AAA"|"Value"| 

我似乎无法通过自定义格式获得正确的结果。 有没有其他解决scheme?

现在我对你的问题有了更好的理解,下面的代码应该会给你以后的东西。

这将采用选定的范围并将其导出(使用文本限定符和分隔符要求)到x驱动器上名为outputfile.txt的文件中。 根据需要更改此项。 这意味着你不需要使用Excel来保存文件,这一切都在macros中完成。

我假设你不需要/需要文件每一行的最后一个pipe道。 如果不是这种情况并且是必需的,则删除将其去除的行(它将启动If Right(outputline, 1)...

 Sub export_range_with_quotes_and_pipe() vbQt = """" delim = "|" exportfilename = "x:\outputfile.txt" Dim rng As Range Set rng = Selection ' or this could be a range such as = range("A2:X50") ff = FreeFile 'create text file for output Open exportfilename For Output As ff 'read each row from range For Each r In rng.Rows outputline = "" 'read each cell in row R For Each c In r.Cells 'build outputline from each cell in row R outputline = outputline & vbQt & c.Cells(1, 1).Text & vbQt & delim Next 'strip last pipe delimiter as not required If Right(outputline, 1) = delim Then outputline = Left(outputline, Len(outputline) - 1) 'send outputline to file Print #ff, outputline Next ' close file Close ff Set rng = Nothing End Sub 

自定义格式的\“@ \”| 应该这样做吗?

所以在vba ..这将是:

Range(“xxx:yyy”)。NumberFormat =“\”“@ \”“|”

警告下面的代码将改变单元格内容,而不是将格式应用到单元格。 出于这个原因,具有公式的单元格将被转换为文本。

目前它可以运行在所有当前选定的单元格上,但是如果需要的话,可以将其改变为在特定的范围上运行。

 Sub add_quotes_and_pipe_to_selected_cells() vbQt = """" Dim rng As Range Set rng = Selection ' or this could be a range such as = range("A2:X50") For Each c In rng.Cells c.Cells(1, 1) = vbQt & c.Cells(1, 1).Text & vbQt & "|" Next Set rng = Nothing End Sub