Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA Excel确定是否;表##”;有数据_Excel_Vba - Fatal编程技术网

VBA Excel确定是否;表##”;有数据

VBA Excel确定是否;表##”;有数据,excel,vba,Excel,Vba,我开发了以下代码,用于从命名范围“Table24”创建图表。但是,有时此表没有有效数据,然后我希望数据集的范围是一个包含0的单元格,并填充没有数据的图表 这是5个图表中的第4个-使用调试模块,我确定这是每次运行excel文件时都会崩溃的代码: '//////////////////CHART 4 Creation ////////////////////////////// Set myChtRange = ws.Range("L43:R63") ' What range contains da

我开发了以下代码,用于从命名范围“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
甚至连房间都是空的

当表格如下所示时,我需要帮助创建正确的参数:

作为参考,命名范围“Table24”确实存在。表中没有数据

回答: 如果WorksheetFunction.CountA(范围(“表24”))=1,则添加此函数(执行):

然后,将其替换为:

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
Dim tbl作为ListObject
Set tbl=ws.ListObject(“表24”)
如果DBRRangeTest(tbl.HeaderRowRange.Offset(1),tbl.DataBodyRange),则
'如果此测试返回true,则表示存在有效的DataCodeRange_
'但我们无法保证单元格是否为空,因为DataodyRange_
'将返回一个地址,如果其范围内有*个值。
“所以我们将手动测试。
如果tbl.HeaderRowRange.Offset(1).Value“”则
设置myDataRange=tbl.ListColumns(3.DataCodeRange)
其他的
设置myDataRange=ws.Range(“K1”)
如果结束
其他的
'测试返回false,这与_
“DataBodyRange什么都不是。
设置myDataRange=ws.Range(“K1”)
如果结束

listobject的DataCodeRange属性表示数据范围。如果列表对象中没有数据,则范围中没有任何数据

if ws.ListObjects("Table24").DataBodyRange is Nothing then
  'Do something if there is no data
Else
  'Do something if there is data
end if

我将分享我使用的函数。如果ListObject(表)在标题行下方和总行上方为空,则返回TRUE。(如果表格既不显示表头也不显示总行,则仍有效。)

此函数检查4种可能性中的1种:

  • 如果ListObject(table)变量“什么都不是”,它会生成一个易于理解的错误
  • 如果该表没有DataCodeRange(没有在标题下方和总行上方添加数据,也没有向表中添加空白行,只有在创建表时自动创建的行除外),则返回
    False
  • 如果该表有一个DataBodyRange,但标题下方和总行上方的每个单元格都是
    空的
    (不包含值,甚至不包含长度为零的vbNullString字符),则返回
    False
  • 如果(a)表格有一个DataBodyRange且(b)标题下方和总行上方至少有一个单元格不为
    (包含任何值,包括零长度vbNullString字符),则返回
    False
  • 函数IsEmptyTable()
    我知道这很旧,但对于阅读本文的人来说,检查空表的正确方法是使用ListRows.Count

    如果表为空,则该值为零。换句话说,如果删除表中的所有行,则ListRows.Count将为零

    If Sheet1.ListObjects("Table").ListRows.Count = 0 Then
        ' empty
    Else
        ' not empty
    End If
    

    如果向表中添加了任何空行,那么DataBodyRange属性将包含一个范围(它是-not-Nothing),并且此逻辑可能不会按预期的方式运行。如果这样做的目的是通过引用DataBodyRange避免抛出错误,那么这是可以的。
    Public Function IsEmptyTable(ByVal target As Excel.ListObject) As Boolean        
    
        Const APPNAME As String = "IsEmptyTable()"
    
        If target Is Nothing Then
            ' The parameter doesn't contain a ListObject (table).
            Err.Raise 2310, _
                      APPNAME, _
                      APPNAME & String$(2, vbNewLine) & "Parameter 'target' must refer to a valid ListObject (table)."
    
        ElseIf target.DataBodyRange Is Nothing Then
            ' The table doesn't have a databody range and therefore has no data below
            '   the header row and above the total row.
            IsEmptyTable = True
    
        Else
            ' The table has a DataBodyRange.
            '   Check it for any non-empty cells (including zero-length
            '   vbNullString).  Return TRUE if any are found.
            IsEmptyTable = Excel.WorksheetFunction.CountA(target.DataBodyRange) = 0
    
        End If
    End Function
    
    If Sheet1.ListObjects("Table").ListRows.Count = 0 Then
        ' empty
    Else
        ' not empty
    End If