我开发了以下代码来创建一个名为Range" Table24"的图表。但是,有时候这个表没有有效数据,然后我希望我的数据集范围是一个0的单元格,并填充没有数据的图表。
这是针对5个图表中的第4个 - 使用调试模块我已经确定这是每次运行时崩溃我的excel文件的代码:
'//////////////////CHART 4 Creation //////////////////////////////
Set myChtRange = ws.Range("L43:R63")
' What range contains data for chart
If ws.Range("Table24").Rows.Count > 0 Then
Set myDataRange = ws.ListObjects("Table24").ListColumns(3).DataBodyRange
Else
Set myDataRange = ws.Range("K1")
End If
' Cover chart range with chart
Set objChart = .ChartObjects.Add( _
Left:=myChtRange.Left, Top:=myChtRange.Top, _
Width:=myChtRange.Width, Height:=myChtRange.Height)
' Put all the right stuff in the chart
With objChart.Chart
.ChartArea.AutoScaleFont = False
.ChartType = xlColumnClustered
.ChartStyle = 214
.SetSourceData Source:=myDataRange
.Parent.Name = "Chart4"
.HasTitle = True
.HasLegend = False
.ChartTitle.Characters.Text = "Most Tolerance Holds"
.ChartTitle.Font.Bold = True
.ChartTitle.Font.Size = 15
If ws.Range("Table24").Rows.Count > 0 Then
.SeriesCollection(1).XValues = ws.ListObjects("Table24").ListColumns(2).DataBodyRange
Else
.SeriesCollection(1).XValues = ws.Range("K1")
End If
With .Axes(xlCategory, xlPrimary)
.HasTitle = True
With .AxisTitle
.Characters.Text = " "
.Font.Size = 10
.Font.Bold = True
End With
End With
With .Axes(xlValue, xlPrimary)
.HasTitle = True
.DisplayUnit = none
.HasDisplayUnitLabel = False
.TickLabels.NumberFormat = "#,##0.0"
With .AxisTitle
.Characters.Text = "Lines"
.Font.Size = 15
.Font.Bold = True
End With
End With
End With
我试过了:
If ws.ListObjects("Table24").DataBodyRange.Rows.Count > 0 Then
If NOT ws.ListObjects("Table24").DataBodyRange Is Nothing Then
甚至IS清空
当表格如下所示时,我需要帮助创建正确的参数:
供参考指定范围"表24"确实存在。该表中没有数据。
答案 0 :(得分:4)
答案: 如果WorksheetFunction.CountA(范围("表24"))= 1那么
答案 1 :(得分:3)
listobject的databodyrange属性表示数据范围。如果列表对象中没有数据,则该范围没有任何内容
if ws.ListObjects("Table24").DataBodyRange is Nothing then
'Do something if there is no data
Else
'Do something if there is data
end if
答案 2 :(得分:2)
添加此功能(拍摄from here):
Function DBRRangeTest(rng1 As Range, Optional rng2 As Range)
' DataBodyRange Range Test
' Test if rng1 and rng2 intersect
If rng2 Is Nothing Then
' Either no argument was supplied or the supplied argument was empty
DBRRangeTest = False
Exit Function
End If
If (rng1.Parent.Name = rng2.Parent.Name) Then
Dim ints As Range
Set ints = Application.Intersect(rng1, rng2)
If (Not (ints Is Nothing)) Then
DBRRangeTest = True
End If
End If
End Function
然后,替换它:
If ws.Range("Table24").Rows.Count > 0 Then
Set myDataRange = ws.ListObjects("Table24").ListColumns(3).DataBodyRange
Else
Set myDataRange = ws.Range("K1")
End If
用这个:
Dim tbl As ListObject
Set tbl = ws.ListObject("Table24")
If DBRRangeTest(tbl.HeaderRowRange.Offset(1), tbl.DataBodyRange) Then
' If this test returns true, it means that there is a valid databodyrange _
' but we have no guarantee whether the cell is empty or not, because DataBodyRange _
' will return with an address if there *has* been a value in its range.
' So we'll test manually.
If tbl.HeaderRowRange.Offset(1).Value <> "" Then
Set myDataRange = tbl.ListColumns(3).DataBodyRange
Else
Set myDataRange = ws.Range("K1")
End If
Else
' The test returned false, which essentially is the same as _
' DataBodyRange Is Nothing.
Set myDataRange = ws.Range("K1")
End If
答案 3 :(得分:1)
我知道这已经很老了,但是对于任何人来说,检查空表的正确方法是使用 ListRows.Count。
如果表为空,则为零。换句话说,如果删除表中的所有行,则 ListRows.Count 将为零。
If Sheet1.ListObjects("Table").ListRows.Count = 0 Then
' empty
Else
' not empty
End If
答案 4 :(得分:0)
我将分享我使用的功能。如果ListObject(表)在标题行下方和总行上方为空,则返回TRUE。 (如果表未显示标题行或总行,它仍然有效。)
此功能检查4种可能性中的1种:
False
(不包含值,甚至不包含长度为零的vbNullString字符),则它将返回Empty
。 False
(包含任何值,包括长度为零的vbNullString字符),然后返回Empty
。False