使用Rest API调用使用VBA在Excel表单中填充动态下拉菜单

时间:2018-12-18 16:05:19

标签: excel-vba rest dropdown populate

显式选项 私有m_empList作为字符串

'动态填充下拉列表 Private Sub Workbook_SheetSelectionChange(ByVal Sh作为对象,ByVal目标作为范围)

    ' Since we're changing the Validation each time there is a new Selection;
    ' It's the Active Cell that matters, not the Target range

  If ActiveCell.Row <= 1 Then
    Exit Sub
  End If
    If ActiveCell.Column = 2 And ActiveCell.Row > 1 Then
        If m_empList = "" Then
            m_empList = GetEmployeeList(ActiveCell, Sh.Name)
        End If
       MakeCombo Target, m_empList

结束功能

'Rest API调用以获取部门的所有员工 私有函数GetEmployeeList(目标为范围,名称为字符串)为字符串 昏暗的objRequest作为对象 Dim jsonDictionary作为新词典 Dim TempJsonString作为字符串,JsonString作为字符串 Dim strResponse作为字符串 Dim jsonItems作为新集合 Dim jsonObject作为对象,项目作为对象 Dim myValidationStr作为字符串 '让一个部门的所有员工 设置objRequest = CreateObject(“ WinHttp.WinHttpRequest.5.1”) objRequest.Open“ POST”,“ http://localhost:8080/getAllEmployees

jsonDictionary("dept") = "IT"
jsonItems.Add jsonDictionary
JsonString = JsonConverter.ConvertToJson(ByVal jsonItems)
'Send Request.
objRequest.send JsonString
'And we get this response
strResponse = objRequest.responseText

If Trim(strResponse & vbNullString) <> vbNullString Then
 Set jsonObject = JsonConverter.ParseJson(strResponse)
 Dim i As Long
 With Sheets("employeesheet")  'copy all the employees in employeesheet
    .Range("A1:A1000").ClearContents
    i = 1
    For Each item In jsonObject("EmployeeList")
        i = i + 1
        .Range("A" & i).Value = item("empName")
    Next item
 End With

End If

GetEmployeeList = "=employeesheet!A1:A" & i 'copy all the employees in employeesheet

结束功能

'在下拉列表中设置Employee列表 子MakeCombo(ByRef目标为范围,ByRef组合列表为字符串) 昏暗的cboTemp作为OLEObject 昏暗的工作表 暗淡Tgt作为范围 昏暗的TgtMrg作为范围 调光范围 昏暗的TgtW作为双 昏暗的AddW只要 昏暗的AddH只要

设置ws = ActiveSheet 关于错误继续 '额外的宽度以覆盖下拉箭头 AddW = 15 '覆盖单元格的额外高度 AddH = 5

如果Target.Rows.Count> 1则转到To exitHandler

设置Tgt = Target.Cells(1,1) 设置TgtMrg = Tgt.MergeArea 出错时转到errHandler

设置cboTemp = ws.OLEObjects(“ TempCombo”)     关于错误继续   如果cboTemp.Visible = True,则     使用cboTemp       .Top = 10       左= 10       .ListFillRange =“”       .LinkedCell =“”       .Visible = False       .Value =“”     结束于   如果结束

出现错误时转到errHandler

With cboTemp
  .Visible = True
  .Left = Tgt.Left
  .Top = Tgt.Top
  .Width = Tgt.Width '+ AddW
  .Height = Tgt.Height + AddH
  .ListFillRange = comboList
  .LinkedCell = Tgt.Address
End With
cboTemp.Activate
Me.ActiveSheet.TempCombo.DropDown

exitHandler:   Application.EnableEvents =真   Application.ScreenUpdating =真   退出子 errHandler:   恢复exitHandler

结束子

0 个答案:

没有答案