'strSQL = "SELECT * INTO [ODBC;Driver={SQL Server};" & "Server=KLOVE;Database=SOLIEUE12;" & "User ID=sa].MBA FROM [khong xoa$A4:Z27]"
strSQL = "SELECT * INTO newMBA FROM E:\Klove\untility\Book1_.xls...[khong xoa$]"
Sub updateData()
For i = 1 To Selection.Rows.Count
fID = Selection(i, 1).Value
fName = "N'" & Selection(i, 2).Value & "'"
fPass = "N'" & Selection(i, 3).Value & "'"
fAge = Selection(i, 4).Value
lenh = "update Name set Name=" & fName & ", Pass=" & fPass & ", Age=" & fAge & " where ID=" & fID
With ThisWorkbook.rsPubs
.ActiveConnection = ThisWorkbook.cnPubs
.Open lenh
End With
Next
End Sub
", Pass=" & fPass & ", age=
", Pass=' " & fPass & " ', age=
Ý của bạn là nếu empty thì thay bằng 0 nhưng của mình nếu empty thì vẫn là empty không thể thay bằng 0 bạn ạ!À, nếu Excel xử lý như vậy thì ta xử bằng code cũng được mà, ví dụ nhe:
If Selection(i, 4).Value <> "" Then
fAge = Selection(i, 4).Value
Else
fAge = 0
End If
Sub updateData()
Set cnPubs = New ADODB.Connection
Set rsPubs = New ADODB.Recordset
cnPubs.Open connstr
For i = 1 To Selection.Rows.Count
fID = Selection(i, 1).Value
fName = "N'" & Selection(i, 2).Value & "'"
fPass = "N'" & Selection(i, 3).Value & "'"
fAge = Selection(i, 4).Value
'kiem tra fAge co Empty khong
fAge = IIf(Len(fAge) = Empty, "", ", age=" & fAge)
lenh = "update Name set Name=" & fName & ", Pass=" & fPass & " " & fAge & " where ID=" & fID
With rsPubs
.ActiveConnection = cnPubs
.Open lenh
End With
Next
End Sub
DIỄN ĐÀN GIẢI PHÁP EXCEL Group 1
DIỄN ĐÀN GIẢI PHÁP EXCEL Group 2