如何使用VB提取EXCEL信息

2024-12-02 19:04:33
推荐回答(2个)
回答1:

回答一样的不让提交,我这是一样的吗?借:)HE的代码修改下。Private Sub Command1_Click()
Dim a as single, b as single, c as single, d As Single
Dim objExcel As Excel.Application '定义Excel表格
Dim objWorkBook As Excel.Workbook '定义工作簿
Dim objSheet As Excel.Worksheet
Dim objRange As Excel.Range
Dim SheetName As StringDim EndRow as long
Set objExcel = CreateObject("Excel.Application")
Set objWorkBook = objExcel.Workbooks.Open("D:\" & "美术" & "成绩表1.xls", False, True) '这里的“美术”二字可以用考生类别的选项来编写
objExcel.Visible = Falseif OptionButton1=true then 'OptionButton1单选按钮不记得在VB里叫什么名字了。sheetname="美术"elsesheetname="音乐"end if
Set objSheet = objExcel.Worksheets(sheetname) '设置活动工作表EndRow=objsheet.range("B65536").end(xlup).row
Set objRange = objSheet.Range("B3:F" & EndRow)
Label6.Caption = objExcel.Application.WorksheetFunction.VLookup(Text1.Text, objRange, 2, 0) '请自己改Text1,姓名的文本框
Label7.Caption = objExcel.Application.WorksheetFunction.VLookup(Text1.Text, objRange, 3, 0)
Label8.Caption = objExcel.Application.WorksheetFunction.VLookup(Text1.Text, objRange, 4, 0)
Label9.Caption = objExcel.Application.WorksheetFunction.VLookup(Text1.Text, objRange, 5, 0)
a = Val(Label6.Caption)
b = Val(Label7.Caption)
c = Val(Label8.Caption)
d = Val(Label9.Caption)
Label3.Caption = a + b + c + d

Set objRange = Nothing
Set objSheet = Nothing
objWorkBook.Close (False)
Set objWorkBook = Nothing
objExcel.Quit
Set objExcel = Nothing
End Sub

回答2:

我没有VB,只会VBA,所以下面的代码没有经过验证,试试吧。

Private Sub Command1_Click()

Dim a, b, c, d As Single

Dim objExcel As Excel.Application '定义Excel表格
Dim objWorkBook As Excel.Workbook '定义工作簿
Dim objSheet As Excel.Worksheet
Dim objRange As Excel.Range

Set objExcel = CreateObject("Excel.Application")
Set objWorkBook = objExcel.Workbooks.Open("D:\" & "美术" & "成绩表1.xls", False, True) '这里的“美术”二字可以用考生类别的选项来编写
'objExcel.Visible = True
Set objSheet = objExcel.Worksheets("sheet1") '设置活动工作表
Set objRange = objSheet.Range("B3:F50")

Label6.Caption = objExcel.Application.WorksheetFunction.VLookup(Text1.Text, objRange, 2, 0) '请自己改Text1,姓名的文本框
Label7.Caption = objExcel.Application.WorksheetFunction.VLookup(Text1.Text, objRange, 3, 0)
Label8.Caption = objExcel.Application.WorksheetFunction.VLookup(Text1.Text, objRange, 4, 0)
Label9.Caption = objExcel.Application.WorksheetFunction.VLookup(Text1.Text, objRange, 5, 0)

a = Val(Label6.Caption)
b = Val(Label7.Caption)
c = Val(Label8.Caption)
d = Val(Label9.Caption)
Label3.Caption = a + b + c + d

Set objRange = Nothing
Set objSheet = Nothing
objWorkBook.Close (False)
Set objWorkBook = Nothing
objExcel.Quit
Set objExcel = Nothing

End Sub