为什么过滤条件不过滤任何东西?

时间:2019-05-24 17:01:40

标签: excel vba

我有以下代码,该代码正在运行,但未过滤任何数据。只是在邮件中扔标题行。

Sub Mail_Selection_Range_Outlook_Body()

Dim Rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim xAddress As String
Dim a As String


With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail
    .Display
    .To = "ME@ME.COM"
    .CC = ""
    .BCC = ""
    .Subject = "This is the Subject line"
    .htmlbody = "Hi All" & "<br>"
For i = 1 To Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
    ActiveWorkbook.Sheets(2).Activate
    a = Sheets("Sheet2").Range("A" & i).Value
    If ActiveSheet.Range("A" & i).Value Like "GBL?*" Then
        Set Rng = Nothing
        .htmlbody = .htmlbody & a & Chr(9) & "Projects" & RangetoHTML(Range("A1:H78")) & "<br>"
    End If
Next i
.Display
End With
On Error GoTo 0

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


Function RangetoHTML(Rng As Range)
' By Ron de Bruin.
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook


ActiveWorkbook.Sheets(1).Activate

If ActiveSheet.AutoFilterMode Then
    ActiveSheet.AutoFilterMode = False
End If

ActiveWorkbook.Sheets(1).Activate
ActiveSheet.Range("A1:H1").AutoFilter Field:=2, Criteria1:="=" & a, Operator:=xlFilterValues

Set Rng = Range("A1", ActiveSheet.Range("H" & Rows.Count).End(xlUp))

TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

'Copy the range and create a new workbook to past the data in
Rng.Copy
Set TempWB = Workbooks.add(1)
With TempWB.Sheets(1)
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial xlPasteValues, , True, False
    .Cells(1).PasteSpecial xlPasteFormats, , True, False
    .Cells(1).Select
    Application.CutCopyMode = False
    On Error Resume Next
    .DrawingObjects.Visible = True
    .DrawingObjects.Delete
    On Error GoTo 0
End With

'Publish the sheet to a htm file
With TempWB.PublishObjects.add( _
     SourceType:=xlSourceRange, _
     Filename:=TempFile, _
     Sheet:=TempWB.Sheets(1).Name, _
     Source:=TempWB.Sheets(1).UsedRange.Address, _
     HtmlType:=xlHtmlStatic)
    .Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                      "align=left x:publishsource=")

'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function

当前输出如下所示

但是预期输出应该具有这样的表

在每个项目名称下。

为什么不正确地将标准1作为单元格值?

1 个答案:

答案 0 :(得分:0)

在您的代码中,变量aSub Mail_Selection_Range_Outlook_Body中分配为a = Sheets("Sheet2").Range("A" & i).Value

但是,在RangeToHtml中,在这里使用它-ActiveSheet.Range("A1:H1").AutoFilter Field:=2, Criteria1:="=" & a, Operator:=xlFilterValues,但未将其分配给任何内容。变量的范围仅在Mail_Selection_Range_Outlook_Body中,因此条件不起作用。


该怎么办?

  • 使用Option Explicit避免使用未声明的变量。一旦未声明某些内容,VBEditor会立即告知;

  • 确保正确命名变量。 a不是一个好名字;

  • 尝试调试并查看变量的值。使用 F8 分步完成调试,Debug.Print a可以帮助您查看当前值;

  • 尝试使用尽可能少的代码为自己编写某种MCVE。然后,调试可能会更容易。