首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

Excel VBA 怎么实现改变单元格内容后自动更新到数据库?不知道代码哪里有有关问题

2012-01-13 
Excel VBA 如何实现改变单元格内容后自动更新到数据库?不知道代码哪里有问题,请指教Private Sub Worksheet

Excel VBA 如何实现改变单元格内容后自动更新到数据库?不知道代码哪里有问题,请指教
Private Sub Worksheet_Change(ByVal Target As Range)


Dim i, j As Integer
Dim k As Range
Dim a As Boolean
Dim CR As Range



i = Target.Row
j = Target.Column
If i >= 3 Then
   

  For Each k In Target.Cells
   
  a = IsNumeric(RTrim(k.Offset(0, -j).Value))
  If a = False Then
  Exit Sub
  Else
   
  Update_leads_feedback RTrim(k.Offset(0, -j).Value), Trim(k.Offset(-(i - 1), 0).Value), k.Value
  End If
  Next
 End If
 End Sub


Sub Update_leads_feedback(ByVal iLeadsID As Long, iColumnName As String, iComment As String)
  Dim conn As New ADODB.Connection
  Dim cmd As New ADODB.Command
  conn.Open cnnstr()
  cmd.ActiveConnection = conn
  cmd.CommandText = "SP_Leads_ForSalse_FeedBack_Update"
  cmd.CommandType = adcmdstoreproc
  Dim owner As ADODB.Parameter
  Dim LeadsID As ADODB.Parameter
  Dim ColumnName As ADODB.Parameter
  Dim Comment As ADODB.Parameter
   
  owner = cmd.CreateParameter(, adVarChar, adParamInput, 255, login())
  LeadsID = cmd.CreateParameter(, adInteger, adParamInput, 500, iLeadsID)
  ColumnName = cmd.CreateParameter(, adVarChar, adParamInput, 255, iColumnName)
  Comment = cmd.CreateParameter(, adVarChar, 255, iComment)

  With cmd.Parameters
  .Append owner
  .Append LeadsID
  .Append ColumnName
  .Append Comment
   
  End With
  cmd.Execute
  conn.Close
  Set conn = Nothing
End Sub





[解决办法]
应该是 k.Offset(0, -j) 超出了有效单元格的范围吧!

[解决办法]
是 Cells(k.Cells.Row, 1).Value 的数值太大吧! CInt() 溢出了。

CInt()函数 参数的值域是:-32768 ~ 32767

超出范围就溢出了。

热点排行