如何仅将范围为的填充行打印到mail | excel | vba|

如何仅将范围为的填充行打印到mail | excel | vba|,excel,vba,Excel,Vba,因为数据是使用SQL从数据库生成的 我想显示在某个范围内填充的特定数据行 由于数据是从数据库动态获取的,因此每次生成数据时,数据可能会有所不同 示例:-下面是从数据库中获取的数据可能包含一些空行 [资料][1]: 因此,问题是如何在一个范围内识别填充了多少行,只有这些行需要显示在电子邮件正文中(范围是A2:D16) 我的代码 Private Sub Mail_0_Click() 'Working in Excel 2000-2016 Dim rng As Range Dim Ou

因为数据是使用SQL从数据库生成的

我想显示在某个范围内填充的特定数据行

由于数据是从数据库动态获取的,因此每次生成数据时,数据可能会有所不同

示例:-下面是从数据库中获取的数据可能包含一些空行

[资料][1]:

因此,问题是如何在一个范围内识别填充了多少行,只有这些行需要显示在电子邮件正文中(范围是A2:D16)

我的代码

Private Sub Mail_0_Click()
'Working in Excel 2000-2016
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    Set rng = Sheets("Sheet").Range("A2:D16").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

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

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

    On Error Resume Next
    With OutMail
        .To = "example@.com"
        .CC = ""
        .BCC = ""
        .Subject = "Student Details"
        ''.HTMLBody = RangetoHTML(rng)
        .HTMLBody = "Hi," _
         & vbNewLine & vbNewLine & _
         "Please find your details " & _
         RangetoHTML(rng)
        .Send   'or use .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)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    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, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, 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]:

下面的循环遍历范围内的所有行,并确定行中是否有任何值。然后,它将范围设置为包括所有具有数据的行

Sub rngsel()
Dim rng As Range, cel As Range

For Each cel In Sheets("Sheet1").Range("A2:A16") 'loop over the first column in your range

    If Not Application.CountA(Range("A" & cel.Row & ":D" & cel.Row)) = 0 Then 'test if the entire row for each cell has any values with countif

        If rng Is Nothing Then  'test if this is the first row with values
            Set rng = Range("A" & cel.Row & ":D" & cel.Row) 'set the first row in the range
                        Else
                Set rng = Union(rng, Range("A" & cel.Row & ":D" & cel.Row)) 'add the found row to the already defined range

        End If

    End If

Next cel
End Sub

我建议看一个类似的问题来确定使用范围中的哪些行。然后从中获取输出,以确定最终范围。但是,如果按照上面的示例,空行始终位于末尾,我建议根据我所看到的,您的数据中没有任何空行,仅在末尾更改您选择的范围。您可以使用
UsedRange
获取您的范围。。如果我的上述假设是正确的correct@Plutian因为我是动态生成的。数据可能每次都不同,因此我需要找到填充给定范围“A2:D16”的行的范围。这个A2:D16将是常数范围,不会仅在该范围内改变,我需要找到填充数据的范围range@Zac-因为我是动态生成的。数据可能每次都不同,因此我需要找到填充给定范围A2:D16的行的范围。这个A2:D16将是常数范围,不会仅在该范围内改变,我需要找到填充数据范围的范围