Hello gurus,
I've run across a problem that crops up when the filtering of a table produces no results. I am using VBA to filter the table and then copy the DataBodyRange and that works perfectly as long as there are items returned. However, when there are no results in the DataBodyRange (No visible rows), it copies the entire dataset.
I've tried a few workarounds but I can't seem to figure out the status of various properties of the DataBodyRange object so that I can account for it in code. Here's an example:
Given the following dataset:
Name Date Animal
John Doe 1/1/2017 Goldfish
Mike Smith 2/1/2017 Bear
Frank Sun 3/1/2017 Goldfish
Here's an example of the sub:
.DataBodyRange - RTE 13 - Type Mismatch
.DataBodyRange.Rows.Count = 3
.DataBodyRange.Count = 9
Therefore, since "Not .DataBodyRange Is Nothing" is evaluated as true, the range gets copied.
Now I filter the dataset for Animal = vbNullString to purposefully produce no results. The following are still the same.
.DataBodyRange - RTE 13 - Type Mismatch
.DataBodyRange.Rows.Count = 3
.DataBodyRange.Count = 9
I've read somewhere that .DataBodyRange evaluates as Null when there are zero visible rows. However, I can't get that to prove out in any of the If statements:
The following code evaluates properly as False when there are no visible rows:
However, it also evaluates improperly as False when there ARE visible rows, so it is not going to enter the If Statement and perform the copy.
I've been all over and I can't seem to find the answer to this issue. I've tried creating a function to loop through and count. I've also tried using Error Handling to skip the procedure based on the RTE. I've not been able to implement either successfully.
Any and all help is greatly appreciated!
I've run across a problem that crops up when the filtering of a table produces no results. I am using VBA to filter the table and then copy the DataBodyRange and that works perfectly as long as there are items returned. However, when there are no results in the DataBodyRange (No visible rows), it copies the entire dataset.
I've tried a few workarounds but I can't seem to figure out the status of various properties of the DataBodyRange object so that I can account for it in code. Here's an example:
Given the following dataset:
Name Date Animal
John Doe 1/1/2017 Goldfish
Mike Smith 2/1/2017 Bear
Frank Sun 3/1/2017 Goldfish
Here's an example of the sub:
Code:
Sub test()
Dim TWB As String
TWB = ThisWorkbook.Name
Dim tbl As Object
Dim test As Variant
test = Workbooks(TWB).Sheets("Sheet1").ListObjects("Table1").DataBodyRange.Rows.Count
With Workbooks(TWB).Sheets("Sheet1")
Set tbl = .ListObjects("Table1")
With tbl
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.Copy
End If
End With
End With
End Sub
.DataBodyRange - RTE 13 - Type Mismatch
.DataBodyRange.Rows.Count = 3
.DataBodyRange.Count = 9
Therefore, since "Not .DataBodyRange Is Nothing" is evaluated as true, the range gets copied.
Now I filter the dataset for Animal = vbNullString to purposefully produce no results. The following are still the same.
.DataBodyRange - RTE 13 - Type Mismatch
.DataBodyRange.Rows.Count = 3
.DataBodyRange.Count = 9
I've read somewhere that .DataBodyRange evaluates as Null when there are zero visible rows. However, I can't get that to prove out in any of the If statements:
Code:
If Not .DataBodyRange Is Nothing Then
If .DataBodyRange = Null Then
If .DataBodyRange = vbNullString Then
If .DataBodyRange = 0 Then
The following code evaluates properly as False when there are no visible rows:
Code:
If .DataBodyRange Is Nothing Then
However, it also evaluates improperly as False when there ARE visible rows, so it is not going to enter the If Statement and perform the copy.
I've been all over and I can't seem to find the answer to this issue. I've tried creating a function to loop through and count. I've also tried using Error Handling to skip the procedure based on the RTE. I've not been able to implement either successfully.
Any and all help is greatly appreciated!