0

我首先要补充一点,我对 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
4

1 回答 1

0

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

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表示选择了一个选项。

要填充下拉列表,您需要向元素添加<option>元素<select>,例如:

'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
于 2013-09-18T16:16:47.633 回答