|
本帖最后由 jygzcj 于 2024-9-20 10:55 编辑
Sub CheckTableDataExists()
On Error GoTo Errhandle
Dim rngName As Range
Dim LastRow As Long
Dim MaxRow As Long
Dim LR(1 To 16) As Long
Dim i As Long
Dim j As Long
Dim DataArr()
With Sheet1
If .FilterMode Then
.ShowAllData
End If
For i = 1 To 16
LR(i) = .Cells(Rows.Count, i).End(xlUp).Row
Next
For i = 1 To 16
If MaxRow > LR(i) Then
MaxRow = MaxRow
Else
MaxRow = LR(i)
End If
Next
If MaxRow < 2 Then Exit Sub
DataArr = .Range(.Cells(1, 15), .Cells(MaxRow, 15))
For i = 1 To UBound(DataArr)
If DataArr(i, 1) = "" Or IsEmpty(DataArr(i, 1)) Then
MsgBox "此列不允许留空,当前探测有空白,请确认并更改后执行!", vbInformation
Exit Sub
End If
DataArr(i, 1) = Trim(DataArr(i, 1))
Next
.Range(.Cells(1, 15), .Cells(MaxRow, 15)).NumberFormatLocal = "@"
.Cells(1, 15).Resize(UBound(DataArr), 1) = DataArr
Set rngName = .Range(.Cells(1, 1), .Cells(MaxRow, 16))
End With
rngName.Name = "TempRange"
Dim strFileName As String
strFileName = ThisWorkbook.FullName
Dim cnn As Object
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFileName & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes"";"
Dim nSQL As String
Dim mSQl As String
Dim nJoin As String
nSQL = " SELECT A.* ,B.id from [TempRange] A left join "
mSQl = "[odbc;Driver={SQL Server};UID=consign_user;DataBase=consign;PWD=#202308;Server=10.125.4.33].[CheryReconcileRecord] B"
nJoin = " on A.对账单号=B.对账单号 and A.采购订单号=B.采购订单号 and A.对账单行项目=B.对账单行项目 where B.id is null"
Sheet2.Cells.Clear
Sheet2.Cells(1, 1).CopyFromRecordset (cnn.Execute(nSQL & mSQl & nJoin))
Exit Sub
Errhandle:
MsgBox "数据更新出错,未更新成功,请检查数据格式是否正确!", vbInformation
End Sub
|
|