帮忙看看,这个第一次允许还马马虎虎,第二次允许就报错 我的最终需要是将 ComboBox_BTNK 等下拉框取 excel里对应的值,程序中需要打开excel以获得内容,能否不打开excel文件获取信息? Private Sub CMD_InputExcelData_Click() Dim ExcelPath As Variant, ExcelName As String, m As Integer Dim ExcelApp As Excel.Application Dim ExcelSheet As Excel.Worksheet On Error GoTo Err_Control ExcelPath = Excel.Application.GetOpenFilename("Excel Files (*.xls), *.xlsx") '获取打开文件全路径 If ExcelPath = False Then Exit Sub '未选择文件时退出 For m = Len(ExcelPath) To 1 Step -1 If Mid(ExcelPath, m, 1) = "\" Then Exit For Next m ExcelName = Right(ExcelPath, Len(ExcelPath) - m) Set ExcelApp = CreateObject("excel.application") ExcelApp.Visible = True ExcelApp.Workbooks.Open (ExcelPath) Set ExcelSheet = ExcelApp.Workbooks(ExcelName).Sheets(1) '指定excel中唯一的一个sheet Dim FCATs As Variant, Fcodes As String Dim FcatSearch As Range, i As Integer FCATs = Array("BTNK", "CDHR", "COMM", "EVLT", "FRPR", "HRTZ") '这个缩略了,实际有好几十个 For i = LBound(FCATs) To UBound(FCATs) Set FcatSearch = ExcelSheet.Range("B:B").Find(FCATs(i), , , xlWhole) Fcodes = "ComboBox_" & FCATs(i) Names.Add Name:=Fcodes, RefersTo:="=" & FcatSearch.Offset(, 1).Address '命名excel名称以方便调用 '这个还有其他更方便的调用方法吗?通过名称好像比较偏门。 Next i ComboBox_BTNK.Value = Names("ComboBox_BTNK").RefersToRange.Value ComboBox_CDHR.Value = Names("ComboBox_CDHR").RefersToRange.Value ComboBox_COMM.Value = Names("ComboBox_COMM").RefersToRange.Value ComboBox_EVLT.Value = Names("ComboBox_EVLT").RefersToRange.Value ComboBox_FRPR.Value = Names("ComboBox_FRPR").RefersToRange.Value ComboBox_HRTZ.Value = Names("ComboBox_HRTZ").RefersToRange.Value ExcelApp.ActiveWorkbook.Close SaveChanges:=False ExcelApp.Quit Set ExcelApp = Nothing Exit Sub Err_Control: Debug.Print Err.Number MsgBox Err.Description End Sub |