【问题标题】:Excel VBA Me.Range gives contradictory results. Why?Excel VBA Me.Range 给出了矛盾的结果。为什么?
【发布时间】: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,那么这是唯一一个将添加公式的单元格。您可能应该使用不同的列来确定要填充的行。

标签: vba excel


【解决方案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

【讨论】:

  • 它不起作用....现在它们都在上升。我希望他们下到我的最后一排。
  • 你在“O4”和“P4”中有什么东西吗?
  • 我不知道。现在它的行为是随机的。就像我现在报告这个 O 栏有效但 P 栏停在 P4 之后一样......有时我认为这个程序讨厌我......
  • 现在当我手动将 P 列的公式复制到最后一行时,它可以工作了。
  • [1]。您是否将Set ws = Sheet1 更改为相关工作表? [2] 到 O4 和 P4 单元格检查是否有东西。
猜你喜欢
  • 1970-01-01
  • 2015-08-25
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多