gpt4 book ai didi

vba - 字 VBA : Table cell range to text file

转载 作者:行者123 更新时间:2023-12-04 14:23:57 29 4
gpt4 key购买 nike

我想复制一个表格,如 this在word文档中,只提取轨道标题和 Composer 。范围的选择按计划进行:

Dim myCells As Range
With ActiveDocument
Set myCells = .Range(Start:=.Tables(1).Cell(2, 3).Range.Start, _
End:=.Tables(1).Cell(.Tables(1).Rows.Count, 3).Range.End)
myCells.Select
End With

现在,当我手动复制此选择并将其粘贴到记事本中时,我得到了我想要的:
Title
Composer
Title
Composer
etc.

但是,我想将此选择自动写入文本文件。当我尝试这样做时,所有内容都塞在一行文本中,到处都是小方块(段落标志?)。

我怎样才能使用 VBA 获得手动复制的结果?

最佳答案

尝试这个

Sub Allmusic()
Dim filesize As Integer
Dim FlName As String, tempStr As String
Dim i As Long
Dim MyAr() As String

'~~> Name of Output File
FlName = "C:\Sample.Txt"

'~~> Get a free file handle
filesize = FreeFile()

'~~> Open your file
Open FlName For Output As #filesize

With ActiveDocument
For i = 2 To .Tables(1).Rows.Count

'~~> THIS LINE WILL NOT REFLECT CORRECTLY IN THE BROWSER
'~~> PLEASE REFER TO THE SCREENSHOT OR THE ATTACHED FILE
tempStr = Replace(.Tables(1).Cell(i, 3).Range.Text, "", "")

If InStr(1, tempStr, Chr(13)) Then
MyAr = Split(tempStr, Chr(13))
Print #filesize, MyAr(0)
Print #filesize, MyAr(1)
Else
Print #filesize, tempStr
End If
Print #filesize, ""
Next i
End With

Close #filesize
End Sub

快照

enter image description here

样本文件

http://sdrv.ms/Mo7Xel

下载文件并运行程序 Sub Allmusic()
输出

enter image description here

HTH

关于vba - 字 VBA : Table cell range to text file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10873080/

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