【问题标题】:Trying to use Vbscript to find data within excel and select its value from a dropdownlist within an HTA尝试使用 Vbscript 在 excel 中查找数据并从 HTA 中的下拉列表中选择其值
【发布时间】:2023-08-16 05:32:02
【问题描述】:

我首先要补充一点,我对 vbscript 很陌生,如果这是我遗漏的简单内容,我深表歉意。我已经搜索过,但在提出正确的代码时遇到了问题(尽管这个网站帮助了我很多)。

无论如何,我已经创建了一个 HTA,您可以将数据输入到表单中并让它填充到 Excel 电子表格中。如果序列号已经在电子表格中,那么它会更新该列,否则它会添加一个新列。所有这一切都很好。我正在尝试添加一个 SUB,这样我就可以有一个搜索按钮来填充字段,这样您就可以在更新之前查看其中已经存在的数据。我能够填充文本框,但下拉列表无法选择匹配值。这是填充表单的代码部分。如果我将下拉菜单更改为文本输入,那么它会很好地填充。我将箭头放在失败的两条线的左侧,我尝试了许多不同的东西,但它们似乎都失败了。希望这一切都说得通。提前致谢!

  Sub SearchINV()
    Dim FSO, oExcel, oData, FoundCell, FindTag, FilePath, oWorkSheet

    FindTag = document.all.serial.value
    FilePath = "C:\file.xlsx"


    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oExcel = CreateObject("Excel.Application")
    Set oData = oExcel.Workbooks.Open(FilePath)
    oData.Worksheets("sheet1").Select
    Set FoundCell = oData.Worksheets("sheet1").Range("A1:A20000").Find(FindTag)
    oExcel.DisplayAlerts = False

    Set FoundCell = oData.Worksheets("sheet1").Range("A1:A20000").Find(FindTag)
    If Not FoundCell Is Nothing Then
      Dim r, c
      r = FoundCell.Row
      c = FoundCell.Column  
      Set oWorkSheet = oData.Worksheets("sheet1")
      document.getElementById("serial").value = FoundCell.Value
      document.getElementById("combination").value = oWorksheet.Cells(r, c+1).Value
      document.getElementById("last").value = oWorksheet.Cells(r, c+2).Value
      document.getElementById("first").value = oWorksheet.Cells(r, c+3).Value
      document.getElementById("department").value = oWorksheet.Cells(r, c+4).Value
--->  document.getElementById("floor").value = oWorksheet.Cells(r, c+5).Value
--->  document.getElementById("building").value = oWorksheet.Cells(r, c+6).Value
    Else
      MsgBox (FindTag & " not found")
    End If

    Set File_Path = nothing
    Set FindTag = nothing
    Set FoundCell = nothing
    oData.Close
    oExcel.Quit
    Set oWorkSheet = Nothing
    Set oData = Nothing
    Set oExcel = Nothing
    Set FSO = Nothing 
 End Sub

【问题讨论】:

    标签: excel drop-down-menu vbscript hta


    【解决方案1】:

    由于您提供的信息相当不完整,我只能就如何填充下拉列表给您一个通用的答案。

    HTML 中的下拉列表是<select> 元素,下拉列表中列出的项目是<option> 元素。 HTML 代码看起来有点像这样:

    <select id="fruit">
      <option value="green">Apple</option>
      <option value="yellow" selected>Banana</option>
      <option value="red">Strawberry</option>
    </select>
    

    selected 属性的存在表示选择了一个选项。

    要填充下拉列表,您需要将 &lt;option&gt; 元素添加到 &lt;select&gt; 元素,例如像这样:

    'sample data
    data = CreateObject("Scripting.Dictionary")
    data.Add "green" , "Apple"
    data.Add "yellow", "Banana"
    data.Add "red"   , "Strawberry"
    
    'get dropdown list element
    Set list = document.getElementById("fruit")
    
    'populate dropdown list with sample data
    For Each fruit In data.Keys
      Set opt = document.createElement("OPTION")
      opt.Text  = data(fruit)
      opt.Value = fruit
      list.Add opt
    Next
    

    如果这没有帮助,您需要提供有关源数据、要填充的元素以及结果的外观的更多信息。


    编辑:如果您想从已填充的下拉列表中选择一个选项,如果它与 Excel 电子表格中给定单元格的值匹配,您可以这样做:

    'get dropdown list element
    Set list = document.getElementById("fruit")
    
    'select matching element
    For Each opt In list.Options
      If opt.Text = oWorksheet.Cells(r, c+5).Value Then opt.Selected = True
    Next
    

    【讨论】:

    • 感谢您的回复,看来我需要提供更多信息,但我不确定如何措辞。您上面的代码(我相信)定义了下拉框的选择。我已经设置了包含所有选项的 html(对于 exable A B C),我试图获取上面的两行以从 excel 文档中获取现有数据并从下拉列表中选择匹配选项。假设我有选项 A、B 和 C。excel 文档当前在单元格中有 A。当您打开 HTA 时,我希望它自动在该下拉列表中选择 A。
    • 这完全符合我的期望。非常感谢您的帮助!
    最近更新 更多