|
楼主 |
发表于 2023-2-5 13:07
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
能否不要移动到b列,A列 knd_no代码输入完毕后直接查询出knd_name
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
i = Target.Row
If i > 2 And Range("A" & i) <> "" Then
Dim conn As Object, xRs As Object
Set conn = CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;UID=sa;PWD=密码;Initial Catalog=dataSQL;Data Source=192.168.1.1"
Set xRs = CreateObject("ADODB.RecordSet")
sSq1 = "Select KND_NAME from KND WHERE KND_NO='" & Range("A" & i) & "'"
xRs.Open sSq1, conn, 1, 3
Sheet1.Range("B" & i).CopyFromRecordset xRs
xRs.Close
conn.Close
Set xRs = Nothing
Set conn = Nothing
End If
End Sub
|
|