怎样在excel中实现自动记录?vba也可以

2024-11-24 04:24:40
推荐回答(2个)
回答1:

以下是我用了实现从Excel导数据进ACCESS的代码,你参考一下,应该就可以做出来了:(Excel和Access两个文件放一个文件夹下)
Public Sub 客户正常供价批量维护()
If Cells(4, 6) = "" Then
MsgBox "请先选择需要维护价格的系统!", vbInformation
Cells(4, 6).Select
Exit Sub
Else

Dim i As Integer, j As Integer, k As Integer, sht As Worksheet 'i,j,k为整数变量;sht 为excel工作表对象变量,指向某一工作表
Dim cn As New ADODB.Connection '定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用
Dim rs As New ADODB.Recordset '定义记录集对象,保存数据表
Dim strCn As String, strSQL As String '字符串变量
Dim mdbFile As String

On Error GoTo add_err

mdbFile = ActiveWorkbook.Path & "\DY_DATA.mdb"

strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbFile '定义数据库链接字符串
cn.Open strCn '与数据库建立连接,如果成功,返回连接对象cn

Set rs = New ADODB.Recordset
rs.Open "dbl直营客户正常供价表", cn, adOpenKeyset, adLockOptimistic

k = Cells(4, 8) + 8
For i = 9 To k
If Cells(i, 7) = "" Then

Else
rs.AddNew
rs(1) = Cells(i, 2)
rs(2) = Cells(i, 7)
rs(3) = Cells(i, 8)

rs.Update
Cells(i, 7) = ""
Cells(i, 8) = ""

End If
Next
MsgBox "数据记录添加成功!", vbInformation
Cells(4, 6).Select
ActiveWorkbook.RefreshAll

add_exit:
Exit Sub
End If
add_err:
MsgBox Err() & vbCrLf & Error()
Resume add_exit
End Sub

回答2:

没有明白你说的什么?你的B列12,1,2,3是什么意思?