我在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
EH:
MsgBox Err.Description
Debug.Print sSQLQry
Debug.Print ssqlstring
End Sub
答案 0 :(得分:1)
最好完全限定对象。注意范围和行之前的DOTS?
Sub SampleA() '<~~ UNTESTED
Dim ws As Worksheet
Set ws = Sheet1
With ws
With .Range("o4", .Range("o" & .Rows.Count).End(xlUp))
.Formula = "=IF(OR(ISBLANK(A4), ISBLANK(E4), ISBLANK(F4), ISBLANK(G4), ISBLANK(H4)), """", ""1"")"
End With
With .Range("p4", .Range("p" & .Rows.Count).End(xlUp))
.Formula = "=IF(OR(ISBLANK(A4), ISBLANK(E4), ISBLANK(F4), ISBLANK(G4), ISBLANK(H4)), """", ""1"")"
End With
End With
End Sub
更好的方法是使用变量和对象。见这个
Sub SampleB() '<~~ UNTESTED
Dim ws As Worksheet
Dim lRow As Long
Dim rng As Range
Dim sFormula As String
Set ws = Sheet1
sFormula = "=IF(OR(ISBLANK(A4), ISBLANK(E4), ISBLANK(F4), ISBLANK(G4), ISBLANK(H4)), """", ""1"")"
With ws
lRow = .Range("O" & .Rows.Count).End(xlUp).Row
Set rng = .Range("O4:O" & lRow)
With rng
.Formula = sFormula
.Value = .Value
End With
lRow = .Range("P" & .Rows.Count).End(xlUp).Row
Set rng = .Range("P4:P" & lRow)
With rng
.Formula = sFormula
.Value = .Value
End With
End With
End Sub