3

我在下面的编码中收到一个错误,标记行中的 Object Required。请帮帮我。Temp文件是一个模板,详细信息来源于Combo代码,Combo代码是需要填写在模板中的数据,根据下面的代码发送电子邮件。

Sub Emails()
    Dim R_No As Integer

    Templ.Select
    Templ.Range("C11") = ""
    Templ.Range("D11") = ""
    Templ.Range("E11") = ""
    Temp1.Range("F11") = "" <-------- Error
    Temp1.Range("G11") = ""
    Templ.Range("C14") = ""
    Templ.Range("D14") = ""
    Templ.Range("E14") = ""
    Temp1.Range("F14") = ""
    Temp1.Range("G14") = ""
    Rows("10:11").Select
    Selection.EntireRow.Hidden = True
    Rows("13:14").Select
    Selection.EntireRow.Hidden = True

    R_No = 2
    Do Until Combo.Cells(R_No, 1) = ""
        If Combo.Cells(R_No, 1) = "Order" Then
            Combo.Cells(R_No, 13) = Combo.Cells(R_No, 2)
        Else
            Combo.Cells(R_No, 13) = Combo.Cells(R_No, 2) & "  &  " & Combo.Cells(R_No, 4)
        End If

        If Combo.Cells(R_No, 7) = Combo.Cells(R_No + 1, 7) Then
            If Combo.Cells(R_No, 1) = Combo.Cells(R_No + 1, 1) Then
                If Combo.Cells(R_No, 1) = "Order" Then
                    Rows("10:11").Select
                    Selection.EntireRow.Hidden = False
                    If Templ.Range("C11") = "" Then
                        Templ.Range("C11") = Combo.Cells(R_No, 2)
                        Templ.Range("D11") = Combo.Cells(R_No, 3)
                        Templ.Range("E11") = Combo.Cells(R_No, 5)
                        Temp1.Range("F11") = Combo.Cells(R_No, 6)
                        Temp1.Range("G11") = Combo.Cells(R_No, 9)
                    Else
                        Templ.Range("C11") = Templ.Range("C11") & Templ.Range("I2") & Combo.Cells(R_No, 2)
                        Templ.Range("D11") = Templ.Range("D11") & Templ.Range("I2") & Combo.Cells(R_No, 3)
                        Templ.Range("E11") = Templ.Range("E11") & Templ.Range("I2") & Combo.Cells(R_No, 5)
                        Templ.Range("F11") = Templ.Range("F11") & Templ.Range("I2") & Combo.Cells(R_No, 6)
                        Templ.Range("G11") = Templ.Range("G11") & Templ.Range("I2") & Combo.Cells(R_No, 9)
                    End If
                End If
                If Combo.Cells(R_No, 1) = "Receipt" Then
                    Rows("13:14").Select
                    Selection.EntireRow.Hidden = False
                    If Templ.Range("C14") = "" Then
                        Templ.Range("C14") = Combo.Cells(R_No, 2) & "-" & Combo.Cells(R_No, 4)
                        Templ.Range("D14") = Combo.Cells(R_No, 3)
                        Templ.Range("E14") = Combo.Cells(R_No, 5)
                        Temp1.Range("F14") = Combo.Cells(R_No, 6)
                        Temp1.Range("G14") = Combo.Cells(R_No, 9)
                    Else
                        Templ.Range("C14") = Templ.Range("C14") & Templ.Range("I2") & Combo.Cells(R_No, 2) & "-" & Combo.Cells(R_No, 4)
                        Templ.Range("D14") = Templ.Range("D14") & Templ.Range("I2") & Combo.Cells(R_No, 3)
                        Templ.Range("E14") = Templ.Range("E14") & Templ.Range("I2") & Combo.Cells(R_No, 5)
                        Templ.Range("F14") = Templ.Range("F14") & Templ.Range("I2") & Combo.Cells(R_No, 6)
                        Templ.Range("G14") = Templ.Range("G14") & Templ.Range("I2") & Combo.Cells(R_No, 9)
                    End If
                End If
            Else
                If Combo.Cells(R_No, 1) = "Order" Then
                    Rows("10:11").Select
                    Selection.EntireRow.Hidden = False
                    If Templ.Range("C11") = "" Then
                        Templ.Range("C11") = Combo.Cells(R_No, 2)
                        Templ.Range("D11") = Combo.Cells(R_No, 3)
                        Templ.Range("E11") = Combo.Cells(R_No, 5)
                        Temp1.Range("F11") = Combo.Cells(R_No, 6)
                        Temp1.Range("G11") = Combo.Cells(R_No, 9)
                    Else
                        Templ.Range("C11") = Templ.Range("C11") & Templ.Range("I2") & Combo.Cells(R_No, 2)
                        Templ.Range("D11") = Templ.Range("D11") & Templ.Range("I2") & Combo.Cells(R_No, 3)
                        Templ.Range("E11") = Templ.Range("E11") & Templ.Range("I2") & Combo.Cells(R_No, 5)
                        Templ.Range("F11") = Templ.Range("F11") & Templ.Range("I2") & Combo.Cells(R_No, 6)
                        Templ.Range("G11") = Templ.Range("G11") & Templ.Range("I2") & Combo.Cells(R_No, 9)
                    End If
                End If
                If Combo.Cells(R_No, 1) = "Receipt" Then
                    Rows("13:14").Select
                    Selection.EntireRow.Hidden = False
                    If Templ.Range("C14") = "" Then
                        Templ.Range("C14") = Combo.Cells(R_No, 2) & "-" & Combo.Cells(R_No, 4)
                        Templ.Range("D14") = Combo.Cells(R_No, 3)
                        Templ.Range("E14") = Combo.Cells(R_No, 5)
                        Temp1.Range("F14") = Combo.Cells(R_No, 6)
                        Temp1.Range("G14") = Combo.Cells(R_No, 9)
                    Else
                        Templ.Range("C14") = Templ.Range("C14") & Templ.Range("I2") & Combo.Cells(R_No, 2) & "-" & Combo.Cells(R_No, 4)
                        Templ.Range("D14") = Templ.Range("D14") & Templ.Range("I2") & Combo.Cells(R_No, 3)
                        Templ.Range("E14") = Templ.Range("E14") & Templ.Range("I2") & Combo.Cells(R_No, 5)
                        Templ.Range("F14") = Templ.Range("F14") & Templ.Range("I2") & Combo.Cells(R_No, 6)
                        Templ.Range("G14") = Templ.Range("G14") & Templ.Range("I2") & Combo.Cells(R_No, 9)
                    End If
                End If
            End If
        Else
            If Combo.Cells(R_No, 1) = "Order" Then
                Rows("10:11").Select
                Selection.EntireRow.Hidden = False
                If Templ.Range("C11") = "" Then
                        Templ.Range("C11") = Combo.Cells(R_No, 2)
                        Templ.Range("D11") = Combo.Cells(R_No, 3)
                        Templ.Range("E11") = Combo.Cells(R_No, 5)
                        Temp1.Range("F11") = Combo.Cells(R_No, 6)
                        Temp1.Range("G11") = Combo.Cells(R_No, 9)
                    Else
                        Templ.Range("C11") = Templ.Range("C11") & Templ.Range("I2") & Combo.Cells(R_No, 2)
                        Templ.Range("D11") = Templ.Range("D11") & Templ.Range("I2") & Combo.Cells(R_No, 3)
                        Templ.Range("E11") = Templ.Range("E11") & Templ.Range("I2") & Combo.Cells(R_No, 5)
                        Templ.Range("F11") = Templ.Range("F11") & Templ.Range("I2") & Combo.Cells(R_No, 6)
                        Templ.Range("G11") = Templ.Range("G11") & Templ.Range("I2") & Combo.Cells(R_No, 9)
                End If
            End If
            If Combo.Cells(R_No, 1) = "Receipt" Then
                Rows("13:14").Select
                Selection.EntireRow.Hidden = False
                If Templ.Range("C14") = "" Then
                        Templ.Range("C14") = Combo.Cells(R_No, 2) & "-" & Combo.Cells(R_No, 4)
                        Templ.Range("D14") = Combo.Cells(R_No, 3)
                        Templ.Range("E14") = Combo.Cells(R_No, 5)
                        Temp1.Range("F14") = Combo.Cells(R_No, 6)
                        Temp1.Range("G14") = Combo.Cells(R_No, 9)
                    Else
                        Templ.Range("C14") = Templ.Range("C14") & Templ.Range("I2") & Combo.Cells(R_No, 2) & "-" & Combo.Cells(R_No, 4)
                        Templ.Range("D14") = Templ.Range("D14") & Templ.Range("I2") & Combo.Cells(R_No, 3)
                        Templ.Range("E14") = Templ.Range("E14") & Templ.Range("I2") & Combo.Cells(R_No, 5)
                        Templ.Range("F14") = Templ.Range("F14") & Templ.Range("I2") & Combo.Cells(R_No, 6)
                        Templ.Range("G14") = Templ.Range("G14") & Templ.Range("I2") & Combo.Cells(R_No, 9)
                End If
            End If
            Templ.Range("C6") = "Dear " & Left(Combo.Cells(R_No, 7), InStr(1, Combo.Cells(R_No, 7), " ") - 1) & ","
            Templ.Range("A1:H48").Select
            ThisWorkbook.EnvelopeVisible = False
            ThisWorkbook.EnvelopeVisible = True

            With ThisWorkbook.Sheets("Templete").MailEnvelope
               .Item.Subject = "Reminder- Order(s) / Receipt(s) Pending Your Urgent Approval"
               .Item.To = Combo.Cells(R_No, 8)
               .Item.cc = " "
               If Combo.Cells(R_No, 10) <> "" Then
                    .Item.cc = Combo.Cells(R_No, 12)
               Else
               End If
               .Item.Send

               Templ.Range("C11") = ""
               Templ.Range("D11") = ""
               Templ.Range("E11") = ""
               Templ.Range("F11") = ""
               Templ.Range("G11") = ""
               Templ.Range("C14") = ""
               Templ.Range("D14") = ""
               Templ.Range("E14") = ""
               Templ.Range("F14") = ""
               Templ.Range("G14") = ""

               Rows("10:11").Select
               Selection.EntireRow.Hidden = True
               Rows("13:14").Select
               Selection.EntireRow.Hidden = True
            End With
        End If
        R_No = R_No + 1
    Loop
End Sub
4

2 回答 2

7

F11 语句具有 temp1 {ONE} 而不是 TEMPL。G11也一样

Templ.Range("E11") = ""
Temp1.Range("F11") = ""
于 2013-02-21T16:20:37.930 回答
0

正如cardmagik 指出的那样,您需要将Temp1(末尾带有数字一)替换为(末尾Templ带有字母L)。

此外,只是对您的代码的一些改进建议:

  • 始终Option Explicit在模块的开头使用 - 这样,如果您拼错了这个示例中的变量,您会被告知!
  • 要在开头清空单元格,只需编写Temp.Range("C11:G11,C14:G14").Value = ""而不是拼出每个单元格
  • 几乎所有的时间你都可以X.Select,然后Selection.DoSomething,你可以用X.DoSomething- 更容易阅读,更少出错和更快执行来替换它
  • 结合以上两点,可以化简
    行(“10:11”)。选择
    Selection.EntireRow.Hidden = True
    行(“13:14”)。选择
    Selection.EntireRow.Hidden = True
    
    Rows("10:11,13:14").EntireRow.Hidden = True
  • 你有很多重复的代码。例如,
    Templ.Range("C11") = Combo.Cells(R_No, 2)
    Templ.Range("D11") = Combo.Cells(R_No, 3)
    Templ.Range("E11") = Combo.Cells(R_No, 5)
    Temp1.Range("F11") = Combo.Cells(R_No, 6)
    Temp1.Range("G11") = Combo.Cells(R_No, 9)
    被调用了 3 次 - 如果您考虑第 14 行的非常相似的代码,甚至 6x!相反,使用两个参数创建一个Sub- 并将其称为 6x!这个子可能看起来像这样:
    Private Sub FillDefault(rowTarget As Long, rowSource as Long)
    Templ.Range("C" & rowTarget) = Combo.Cells(rowSource, 2)
    Templ.Range("D" & rowTarget) = Combo.Cells(rowSource, 3)
    Templ.Range("E" & rowTarget) = Combo.Cells(rowSource, 5)
    Templ.Range("F" & rowTarget) = Combo.Cells(rowSource, 6)
    Templ.Range("G" & rowTarget) = Combo.Cells(rowSource, 9)
    结束子
    
    然后用FillDefault 11, R_Noand调用它FillDefault 14, R_No
  • 你的嵌套IF...THEN...ELSE...语句在这个版本中完全没有意义——每个分支都在做同样的事情!!!- 除了所有其他步骤之外,您只需在一个分支中发送电子邮件!所以最好先完成其他步骤 - 然后发送电子邮件
  • 而不是IF cond1 Then x = 1 Else x = 2你可以使用x = Iif(cond1, 1, 2)- 这通常会导致代码缩短,尤其是。如果 x 是一个较长的术语
  • 你的工作表真的被称为Templete还是更确切地说Template
  • 引用行号时,最好使用 Long 而不是 Integer - Integers 只能工作到 32,768

总而言之,我重新编写了您的代码 - 并将其从高达 169 行减少到 60 行(尽管这可以进一步减少,但为了便于阅读):

Sub Emails()
    Dim R_No As Long

    ClearSheet

    R_No = 2
    Do Until combo.Cells(R_No, 1) = ""

        combo.Cells(R_No, 13) = combo.Cells(R_No, 2) & _
            IIf(combo.Cells(R_No, 1) <> "Order", "  &  " & combo.Cells(R_No, 4), "")

        Select Case combo.Cells(R_No, 1)
            Case "Order":
                Rows("10:11").EntireRow.Hidden = False
                Fill rowTarget:=11, rowSource:=R_No, blnAdd:=(Templ.Range("C11") = "")
            Case "Receipt":
                Rows("13:14").EntireRow.Hidden = False
                Fill rowTarget:=14, rowSource:=R_No, blnAdd:=(Templ.Range("C14") = "")
        End Select

        If combo.Cells(R_No, 7) <> combo.Cells(R_No + 1, 7) Then
            SendMail R_No
            ClearSheet
        End If
        R_No = R_No + 1
    Loop
End Sub

Private Sub ClearSheet()
    Templ.Range("C11:G11,C14:G14").Value = ""
    Rows("10:11,13:14").EntireRow.Hidden = True
End Sub

Private Sub Fill(rowTarget As Long, rowSource As Long, _
    Optional blnAdd As Boolean = False)
    Dim a As Variant, i As Integer
    a = Array(2, 3, 5, 6, 9)
    For i = 0 To 4
        Templ.Cells(rowTarget, i + 3) = _
            IIf(blnAdd, Templ.Cells(rowTarget, i + 3) & Templ.Range("I2"), "") & _
            combo.Cells(rowSource, a(i))
    Next
End Sub

Private Sub SendMail(R_No As Long)
    Templ.Range("C6") = "Dear " & Left(combo.Cells(R_No, 7), _
        InStr(1, combo.Cells(R_No, 7), " ") - 1) & ","
    Templ.Range("A1:H48").Select
    ThisWorkbook.EnvelopeVisible = True

    With ThisWorkbook.Sheets("Templete").MailEnvelope.Item
       .Subject = "Reminder- Order(s) / Receipt(s) Pending Your Urgent Approval"
       .To = combo.Cells(R_No, 8)
       .cc = " "
       If combo.Cells(R_No, 10) <> "" Then
            .cc = combo.Cells(R_No, 12)
       End If
       .Send
    End With
End Sub

于 2013-02-21T21:55:11.163 回答