0

谁能教我如何在 CurrentRegion 属性中选择多个列,以便我可以基于它创建条形图

以下是我到目前为止的进展..但是它不起作用

Public Function FourthTable()

Dim Source As Workbook
Dim Search As Range
Dim CR As Range

SRT = "Service Request Tickets (IIT)"

Set Source = Workbooks.Open("C:\Users\HP\Desktop\test.xlsx")

Source.Worksheets("Sheet1").Activate

Set Search = ActiveSheet.Cells.Find(SRT)

Source.Worksheets("Sheet1").Range(Search.Address).Offset(2, 0).CurrentRegion.Activate

ActiveCell.CurrentRegion.Columns.Range("A:A, 
C:C").Resize(ActiveCell.CurrentRegion.Rows.Count - 1).Select

ActiveSheet.Shapes.AddChart.Select
'ActiveChart.SetSourceData  
Source:=ActiveWorkbook.Sheets(1).Range(Search.Address).Offset(2, 
0).CurrentRegion.Resize(CR.Rows.Count - 1, CR.Columns.Count)
ActiveChart.ChartType = xl3DBarClustered
With ActiveChart
   .Perspective = 0
   .Elevation = 15
   .Rotation = 20
   .RightAngleAxes = True

End With

ActiveSheet.ChartObjects(1).Activate
ActiveSheet.ChartObjects(1).Cut
With Sheets("Sheet2")
.Select
.Range("A34").Select
End With

ActiveSheet.Paste
Source.Close SaveChanges:=True
End Function

我得到的错误信息是应用程序定义的或对象定义的错误

我要选择的列是 A 列和 C 列。我无法通过指示“A34”或“C34”等单元格的地址来选择列,因为表数据是使用程序工具生成的,因此是位置表数据的每次都不同

下图是我参考的表格数据

http://postimg.org/image/gu05epzm9/

4

1 回答 1

0

而不是这个:

  Source.Worksheets("Sheet1").Range(Search.Address).Offset(2, 0). _
       CurrentRegion.Activate

使用它,它会收缩选定区域以排除数据表的最后一行和最后一列:

  Dim tbl As Range
  Set tbl = Source.Worksheets("Sheet1").Range(Search.Address).Offset(2, 0). _
      CurrentRegion
  tbl.Resize(tbl.Rows.Count - 1, tbl.Columns.Count - 1).Select

在继续注释掉或删除的同时:

'ActiveChart.SetSourceData Source:=ActiveWorkbook.Sheets(1). _
     Range(Search.Address).Offset(2,0).CurrentRegion. _
     Resize(CR.Rows.Count - 1, CR.Columns.Count)
于 2013-08-04T04:05:42.827 回答