1.连接数据库,查询数据
Private Sub CommandButton1_Click()
'
'Dim conn As ADODB.Connection
'Set conn = New ADODB.Connection
'
' Dim rs As ADODB.Recordset
'
' Set rs = New ADODB.Recordset
'conn.ConnectionString = "Provider=SQLOLEDB;Server=192.168.*.*;Database=testdb;Uid=sa;Pwd=*****"
'conn.Open
'MsgBox ("连接成功!" & vbCrLf & "数据库状态:" & conn.State & vbCrLf & "数据库版本:" & conn.Version)
' MsgBox ("连接成功!" & conn.State)
' rs.Open "select id,name from table ", conn
'
'指定页面单元格赋值
' Sheet3.Range("A1:B1").Value = Array("id77777", "caption88888")
'
'设置表头
' Range("A1:B1").Value = Array("id", "name")
''将数据输出到工作表
' Range("A2").CopyFromRecordset rs
''关闭连接
' rs.Close: Set rs = Nothing
' conn.Close: Set conn = Nothing
End Sub
2.单元格取值赋值
Private Sub CommandButton2_Click()
'指定页面单元格取值
' MsgBox Sheet3.Range("A3").Value
'指定页面单元格赋值
' Sheet3.Range("A1:B1").Value = 43434343
End Sub
******************************************************
3.单元格选中单击事件,获取指定行指定列
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'MsgBox "777888999"
获取指定行指定列
'If Target.Column = 3 And Target.Row = 5 Then
'If Target.Column = 3 Then
' MsgBox "你选中了:" & Target.Text & "行:" & Target.Row
' End If
'End Sub
***************************************************8
' Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' If Target.Column = 1 And Target.Row = 1 Then
' MsgBox "你选中了:" & Target.Text
' End If
' If Target.Column = 3 And Target.Row = 5 Then
' MsgBox "你选中了:" & Target.Text
' End If
' End Sub