excel - 使用vba写出xml文件跳过空格
问题描述
我在excel中使用vba写出一个xml文件。我正在循环并使用引用循环中单元格值的常量进行替换。我不太明白的是如何在单元格为空时跳过写出一个部分。如果引用的单元格为空白,我想跳过循环单元格 a(i, 3) 并且不写该行,但我想继续进行下一个替换。见相关代码:
Const head = "<?xml version ""1.0"" encoding=""UTF-8""?>"
Const funct1 = "<Order function=""%%"" "
Const itemtype = " <Item type=""%%"">"
Dim all
Dim xml As String, i As Long
Dim oPath As String: oPath = "C:\test\test.xml"
Dim intFF As Integer: intFF = FreeFile()
a = Range("A2:BU" & Cells(Rows.Count, 73).End(xlUp).Row)
xml = head
For i = 1 To UBound(a)
xml = xml & vbNewLine & Replace(funct1, "%%", a(i, 3))
vbNewLine & Replace(itemtype, "%%", a(i, 31))
Next
Open oPath For Output As #intFF
Print #intFF, xml
Close #intFF
Dim sBuf As String
Dim sTemp As String
Dim int2FF As Integer: int2FF = FreeFile()
Dim sFileName As String
sFileName = "C:\test\test.xml"
Open sFileName For Input As int2FF
Do Until EOF(int2FF)
Line Input #int2FF, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close int2FF
解决方案
测试每个循环的字符数。如果字符数 = 0( Len(a(i, 3) = 0)
) 则转到下一个i
。您将需要添加j
为计数器,并且仅在您跳入XML
语句时才增加。
Dim j As Long: j = 1
For i = 1 To UBound(a)
If Len(a(i, 3)) <> 0 Then
XML = XML & vbNewLine & Replace(funct1, "%%", a(i, 3)) _
& vbNewLine & Replace(itemtype, "%%", a(i, 31))
j = j + 1
End If
Next
推荐阅读
- sql - sqlmock:期望不匹配(完全相同的查询)
- c# - 如何使用 UWP NumberBox 实现 NumberFormatter
- javascript - 如何防止 Typescript 从目录加载 `.d.ts` 文件?
- angular - 在单个测试规范中模拟服务并为所有测试规范全局调用它 Angular 单元测试用例
- python - 仅在 django 中显示更新和删除链接以发布作者
- typescript - 如何在打字稿中定义对象的形状?我们应该用类还是接口来做呢?
- ios - 如何在 SwiftUI 中为链接添加下划线?
- c++ - ......在c函数中是什么意思
- macos - 尝试使用 Python/Mac 录制音频时出错
- angular - 创建一个内部有一个可观察对象的可观察函数