因此,我通过阅读XML文件,使用以下代码动态生成工作表名称:
Set GenreNodes = oXMLFile.SelectNodes("/catalog/query/genre/text()")
.
.
Private Sub CreateSheet(sheetName)
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(after:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = sheetName
End Sub
在使用XML时完美工作,而无需废除类似流派的标签,例如:
<?xml version="1.0"?>
<catalog>
<query id="bk101">
<question>Do we have cloud security</question>
<answer>Yes</answer>
<genre>Cloud</genre>
</query>
<query id="bk102">
<question>Do we have locks on the door</question>
<answer>No</answer>
<comment>We have fingerprint access.</comment>
<genre>Physical Security</genre>
</query>
</catalog>
问题是,在本示例中,当我在流派(SDLC)中有重复时,例如:
<?xml version="1.0"?>
<catalog>
<query id="bk101">
<question>Do we have cloud security</question>
<answer>Yes</answer>
<genre>Cloud</genre>
</query>
<query id="bk103">
<question>What SDLC Priciple is follwed?</question>
<answer>None</answer>
<comment>We have code ninjas.</comment>
<genre>SDLC</genre>
</query>
<query id="bk104">
<question>Do you have OSWP status?</question>
<answer>None</answer>
<comment>We plan to do next year.</comment>
<genre>SDLC</genre>
</query>
</catalog>
Excel给了我Name already exists
错误。如何提取没有此错误的唯一节点名称?
谢谢。
答案 0 :(得分:1)
更新您的工作表创建代码,这样它就不会尝试创建重复的工作表:
Private Sub CreateSheet(sheetName)
Dim ws As Worksheet
On Error Resume Next '<< ignore error if sheet doesn't exist
Set ws = ThisWorkbook.Sheets(sheetName)
On Error Goto 0 '<< stop ignoring errors
If ws Is Nothing Then
'sheet doesn't exist, so create it.
Set ws = ThisWorkbook.Sheets.Add(after:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = sheetName
End If
End Sub