求助高手,vb调用excel问题???

来源:百度知道 编辑:UC知道 时间:2024/05/31 06:10:09
我想设计一个用VB判断excel里的数据是不是正确,刚开始判断内容时没问题,如下:
Private Sub Command2_Click()
If VBExcel.Cells(1, 1).Value = "你好" Then
MsgBox "你对了!"
Else
MsgBox "你错了!"
End If
End Sub
但是去到判断格式时就发生错误,全部代码如下:
Dim VBExcel As Object

Private Sub Command1_Click()
OnTop = False
Set VBExcel = CreateObject("Excel.Application")
With VBExcel
.Workbooks.Open "F:\Excel\excel1.XLS"
VBExcel.Visible = True
End With
End Sub

Private Sub Command2_Click()
If VBExcel.Cells(1, 1).Value = "你好" Then
If VBExcel.Cells(1, 1).Value = fun1 Then
MsgBox "你对了!"
Else
MsgBox "你错了!"
End If
Else
MsgBox "你错了!"
End If
End Sub

Private Sub Command3_Click()
On Error Resume Next
VBExcel.DisplayAlerts = False
With VBExcel
.Workbooks.Close

Private Sub Command2_Click()
If VBExcel.Cells(1, 1).Value = "你好" Then
If fun1() Then
MsgBox "你对了!"
Else
MsgBox "你错了!"
End If
Else
MsgBox "你错了!"
End If
End Sub

Private Function fun1() As Boolean

If VBExcel.Cells(1, 1).Font.Name = "宋体" And VBExcel.Cells(1, 1).Font.Bold = True And VBExcel.Cells(1, 1).Font.Size = 10 Then
fun1 = True
Else
fun1 = False
End If

End Function

改这两个地方就可以了