答案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
然后我使用 Sheet 中的事件监听器在单元格“右键单击”时触发此功能,并用值填充表单控件。
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh 作为对象,ByVal 目标作为范围,取消作为布尔值)
出错时继续下一步
' 仅当按下 Ctrl 键时才触发此操作 如果 IsControlKeyDown() = True 则
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
子目录结束