![Excel,以結構化方式為儲存格新增更多信息](https://rvso.com/image/1543366/Excel%EF%BC%8C%E4%BB%A5%E7%B5%90%E6%A7%8B%E5%8C%96%E6%96%B9%E5%BC%8F%E7%82%BA%E5%84%B2%E5%AD%98%E6%A0%BC%E6%96%B0%E5%A2%9E%E6%9B%B4%E5%A4%9A%E4%BF%A1%E6%81%AF.png)
答案1
我最終創建了一個用戶表單,其中包含一些文字方塊和一個按鈕,用於將值保存在另一個名為「資料」的工作表中的同一列/行中。
像這樣的事情:
Dim xml As String
xml = xml + "<CellDetails>"
xml = xml + " <Budget>" + UserForm1.txtBudget.Text + "</Budget>"
xml = xml + " <Comments>" + UserForm1.txtComments.Text + "</Comments>"
xml = xml + " <StartDate>" + Format(MonthView1.Value, "yyyy-mm-dd") + "</StartDate>"
xml = xml + " <EndDate>" + Format(MonthView2.Value, "yyyy-mm-dd") + "</EndDate>"
xml = xml + "</CellDetails>"
ThisWorkbook.Sheets("Data").Range(Selection.Address).Value = xml
然後,我在單元格的“右鍵單擊”上使用事件列表器從工作表中觸發此操作,並使用值填充表單控制項。
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh 作為對象,ByVal 目標作為範圍,取消作為布林值)
出錯時繼續下一步
' 僅在按下 Ctrl 鍵時觸發此操作 If IsControlKeyDown() = True then
If Not ThisWorkbook.Sheets("Data").Range(Selection.Address).Value = "" Then
Dim XDoc As MSXML2.DOMDocument
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.LoadXML (ThisWorkbook.Sheets("Data").Range(Selection.Address).Value)
' Setting the form values
UserForm1.txtBudget.Text = XDoc.SelectSingleNode("//CellDetails/Budget").Text
UserForm1.txtComments.Text = XDoc.SelectSingleNode("//CellDetails/Comments").Text
' Setting the dates
UserForm1.MonthView1.Value = CDate(XDoc.SelectSingleNode("//CellDetails/StartDate").Text)
UserForm1.lbStartDate = XDoc.SelectSingleNode("//CellDetails/StartDate").Text
UserForm1.MonthView2.Value = CDate(XDoc.SelectSingleNode("//CellDetails/EndDate").Text)
UserForm1.lbEndDate = XDoc.SelectSingleNode("//CellDetails/EndDate").Text
End If
UserForm1.Show
Cancel = True
End If
On Error GoTo 0
結束子