【发布时间】:2023-03-26 21:57:01
【问题描述】:
我正在测试的 Excel VBA 中有 2 个 Me.Range 代码:
With Me.Range("o4", Range("o" & Rows.Count).End(xlUp))
.Formula = "=IF(OR(ISBLANK(A4), ISBLANK(E4), ISBLANK(F4), ISBLANK(G4), ISBLANK(H4)), """", ""1"")"
'.Value = .Value
End With
With Me.Range("p4", Range("p" & Rows.Count).End(xlUp))
.Formula = "=IF(OR(ISBLANK(A4), ISBLANK(E4), ISBLANK(F4), ISBLANK(G4), ISBLANK(H4)), """", ""1"")"
'.Value = .Value
End With
它应该做的是将公式一直复制到我在 Excel 中的最后一行。然而,出于某种无法解释的原因。第一个工作正常,但另一个完全相反。除了范围之外,这两者都是 100% 相同的。为什么?
完整的宏
Sub syncSQL()
On Error GoTo EH
Dim sht As Worksheet
Dim conn As New ADODB.Connection
Dim iRowNo As Integer, lastRow As Long, last50 As Range
Dim proj, inv, desc, edt, dt, time, details, stat, rmks, loc, okng, astk, pstk, pic, flag As String
Dim sconnect As String, ssqlstring As String
Dim rs As New ADODB.Recordset
Dim sSQLQry As String, sSQLdel As String, sSQLupd As String
Dim ReturnArray
Dim sForm As String
Dim lRow As Long
Dim rng As Range
Dim ws As Worksheet
Set ws = Sheet7
sForm = "=IF(OR(ISBLANK(A4), ISBLANK(E4), ISBLANK(F4), ISBLANK(G4), ISBLANK(H4)), """", ""1"")"
With Me.Range("o4", Range("o" & Rows.Count).End(xlUp))
.Value = .Value
End With
With Worksheets("Entry Form")
'Open a connection to SQL Server
sconnect = "driver={SQL Server};server=server;database=SQLIOT;uid=admin;pwd=admin"
conn.Open sconnect
'Skip the header row
iRowNo = 4
'Loop until empty cell
Do Until .Cells(iRowNo, 1) = ""
If .Cells(iRowNo, "o").Value = 0 Then
proj = "'" & .Cells(iRowNo, 1) & "'"
inv = "'" & .Cells(iRowNo, 2) & "'"
desc = "'" & .Cells(iRowNo, 3) & "'"
edt = "'" & Format(.Cells(iRowNo, 4), "yyyy-mm-dd") & "'"
dt = "'" & Format(.Cells(iRowNo, 5), "yyyy-mm-dd") & "'"
time = "'" & TimeSerial(Hour(.Cells(iRowNo, 6)), Minute(.Cells(iRowNo, 6)), Second(.Cells(iRowNo, 6))) & "'"
details = "'" & .Cells(iRowNo, 7) & "'"
stat = "'" & .Cells(iRowNo, 8) & "'"
rmks = "'" & .Cells(iRowNo, 9) & "'"
loc = "'" & .Cells(iRowNo, 10) & "'"
okng = "'" & .Cells(iRowNo, 11) & "'"
astk = "'" & .Cells(iRowNo, 12) & "'"
pstk = "'" & .Cells(iRowNo, 13) & "'"
pic = "'" & .Cells(iRowNo, 14) & "'"
flag = "'" & .Cells(iRowNo, 15) & "'"
'Replace single quote with 2 single quotes
details = "'" & Replace(.Cells(iRowNo, 7), "'", "''") & "'"
sSQLQry = "select * from [SQLIOT].[dbo].[ZDIE_MAINT_ENTRY] where [Project No] = " & proj & " And [Inv No] = " & inv & " And [Description] = " & desc & " And [Entry Date] = " & edt & " And [Date] = " & dt & " and [Time] = " & time & " and [Problem + Repair Details] = " & details & " and [Status] = " & stat & " and [Remarks] = " & rmks & " and [Location] = " & loc & " and [Measurement (OK/NG)] = " & okng & " and [Accumulative Stroke] = " & astk & " and [Preventive Stroke] = " & pstk & " and [PIC] = " & pic & " and [Flag] = " & flag & ""
rs.Open sSQLQry, conn, adOpenForwardOnly, adLockReadOnly
'insert new record if doesn't exist
If rs.EOF Or rs.BOF Then
'generate & execute sql, import excel rows to sql table
ssqlstring = "insert into [SQLIOT].[dbo].[ZDIE_MAINT_ENTRY]([Project No], [Inv No], [Description], [Entry Date], [Date], [Time], [Problem + Repair Details], [Status], [Remarks], [Location], [Measurement (OK/NG)], [Accumulative Stroke], [Preventive Stroke], [PIC], [Flag]) values (" & proj & ", " & inv & ", " & desc & ", " & edt & ", " & dt & ", " & time & ", " & details & ", " & stat & ", " & rmks & ", " & loc & ", " & okng & ", " & astk & ", " & pstk & " , " & pic & " , " & flag & ")"
conn.Execute ssqlstring
'update flag status to from 0 -> 1
'.Cells(iRowNo, "o").Value = 1
End If
rs.Close
End If
iRowNo = iRowNo + 1
Loop
With ws
lRow = .Range("o" & .Rows.Count).End(xlUp).Row
Set rng = .Range("o4:o" & lRow)
With rng
.Formula = sForm
End With
lRow = .Range("p" & .Rows.Count).End(xlUp).Row
Set rng = Range("p4:p" & lRow)
With rng
.Formula = sForm
End With
End With
'With Me.Range("o4", Range("o" & Rows.Count).End(xlUp))
' .Formula = "=IF(OR(ISBLANK(A4), ISBLANK(E4), ISBLANK(F4), ISBLANK(G4), ISBLANK(H4)), """", ""1"")"
'.Value = .Value
'End With
' With Me.Range("p4", Range("p" & Rows.Count).End(xlUp))
' .Formula = "=IF(OR(ISBLANK(A4), ISBLANK(E4), ISBLANK(F4), ISBLANK(G4), ISBLANK(H4)), """", ""1"")"
'.Value = .Value
' End With
'MsgBox "Data imported."
conn.Close
Set conn = Nothing
End With
Exit Sub
呃:
MsgBox Err.Description
Debug.Print sSQLQry
Debug.Print ssqlstring
结束子
【问题讨论】:
-
只有当列中已有内容时,您的代码才能填充列。换句话说,如果说 O 列仅填充了单元格 4,那么这是唯一一个将添加公式的单元格。您可能应该使用不同的列来确定要填充的行。