vba过滤后检查标头下方的空单元格

问题描述 投票:0回答:1
在下面,FirstVisibleCelleinscope应该是在标题下方过滤后可见的数据,如果是空的,则循环应继续前进。不幸的是,他的宏观将标题(按照附件图像上的图像)复制到NE下一个项目。在有匹配的项目的情况下,宏可起作用,复制所有数据。

Set firstVisibleCellinscope = wsInscope.Range("A2:A" & lastRowInscope).SpecialCells(xlCellTypeVisible) If firstVisibleCellinscope Is Nothing Then GoTo NextIteration Set DatatoCopy = ActiveSheet.Range("A:K") DatatoCopy.Copy NextIteration: Next I

这可能比您要求的要多,但是这里有一些指导方针,我认为这将有助于使您的代码更加可靠和高效:

excel vba
1个回答
0
投票

要确保您访问要捕获的所有数据,请尝试将所有数据放在表中而不是范围。 将整个表列分配给数组与试图用地址引用范围的范围以及试图找到带有数据填充的最后一行的范围要容易得多。
  1. there是如何应用上述准则的一个示例:

    LLE的假装您有一个带有
  2. 书列表的工作表。 列出了3个属性/标题:
  3. Date

    title
author

.

您可以看到,有些单元格缺少数据。 应用上面列出的准则,第一步是将此范围转换为表。 这可以通过突出显示整个范围并按下ctrl +t

来完成。

enter image description here

next步骤,将名称的表更改为“书籍”

(无引号):

enter image description here注:

在此示例中,我也将工作表的名称更改为

“书”.

next,我们可以开始编写代码。 假设您要为每列提取每个填充的单元格,然后将数据发送到处理发送电子邮件的单独子例程。 可以像这样写:

Public Sub ExportData() Dim BooksTable As ListObject '- This is for storing the table. Dim Buffer As Variant '- This is the array for storing the data extracted from the column. Dim Column As ListColumn '- This is for storing each table ' column. Dim Header As String Dim Index As Long Dim ValidItems As Collection '- This collection will be used to only ' store valid data (non-empty) Set BooksTable = ThisWorkbook.Worksheets("Books").ListObjects("Books") For Each Column In BooksTable.ListColumns 'Extract the column header Header = Column.Name 'Extract the column data Buffer = Column.DataBodyRange.Value 'Loop through each row in the array and only add valid data to the collection' Set ValidData = New Collection With ValidData For Index = LBound(Buffer, 1) To UBound(Buffer, 1) If IsValidData(Buffer(Index, 1) Then .Add Buffer(Index, 1) End If Next Index End With 'Send header and data to email subroutine EmailData Header, ValidData Next Column End Sub 'Function for validating whether the data is empty or null Private Function IsValidData(ByVal Data As Variant) As Boolean If IsNull(Data) Then Exit Function If IsEmpty(Data) Then Exit Function If Data = vbNullString Then Exit Function IsValidData = True End Function 'Method for sending out emails. Private Sub EmailData(Header As String, ValidData As Collection) 'Define your email method here. End Sub enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.