gpt4 book ai didi

vba - 在vba中用两个函数编写一个文件

转载 作者:行者123 更新时间:2023-12-03 19:58:35 24 4
gpt4 key购买 nike

这是我将 excel 单元格写入 XML 文件的主要功能的标题。我想让这个调用另一个函数,它可以自己写一套。

Public Sub WriteXML()
Dim Sheet As Worksheet
Dim Cell As Range
Dim xmlFile

xmlFile = ThisWorkbook.Path & "\" & 'Test1' & ".xml"

Set Sheet = ActiveWorkbook.Worksheets("Sht1")
Open xmlFile For Output As #1
Print #1, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & _
" encoding=" & Chr(34) & "UTF-8" & Chr(34) & "?>"
Call WriteCustomer(xmlFile)

这是第二个函数的开始,尽管我收到了“找不到对象”类型的错误。
Sub WriteCustomer(x As Variant)

Print x, " <Customer>"
Print x, " <First>" & 'Bill' & "</First>"
Print x, " <Last>" & 'Johnson' & "</Last>"
Print x, " </Customer>"
Print x, ""
End Sub

我需要如何构造调用和/或变量以将打开的文件作为对象传递给第二个函数?

最佳答案

您可以按如下方式请求、存储和传递句柄:

Dim handle As Integer
handle = FreeFile()

Open xmlFile For Output As #handle
Print #handle, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & _

...
Call WriteCustomer(handle)


Sub WriteCustomer(handle As Integer)
Print #handle, " <Customer>"

关于vba - 在vba中用两个函数编写一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17299393/

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