Select columns from CurrentRegion table as source data to create graph VBA

taykaisheng

New Member
Joined
Jul 30, 2013
Messages
10
Hello, I'm working on a program recently to automatic generate graph based on a set of data. However, I am facing some issues now as I need to select Column A and Column C to setSourceData for my graph as shown below
View image: table 1

I'm using CurrentRegion property to select the a table of data for chart creation

This is my progress so far..

Code:
Public Function FourthTable()

Dim Source As Workbook
Dim Search As Range
Dim CR As Range
Dim CR2 As Range
Dim CR3 As Range
Dim CR4 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)
    
    With Source.Worksheets("Sheet1").Range(Search.Address).Offset(2, 0).Select
    ActiveCell.CurrentRegion.Select
    End With
    
    Set CR = ActiveCell.CurrentRegion
    CR.Resize(CR.Rows.Count - 1, CR.Columns.Count).Select
    
    With Source.Worksheets("Sheet1").Range(Search.Address).Offset(2, 0).CurrentRegion
        Set CR2 = .Resize(.Rows.Count - 1)
    End With
    
     With Source.Worksheets("Sheet1").Range(Search.Address).Offset(2, 0).CurrentRegion
     Set CR3 = .Columns(3).Address
     End With
     
     With Source.Worksheets("Sheet1").Range(Search.Address).Offset(2, 0).CurrentRegion
     Set CR4 = .Columns(1).Address
     End With
     
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=ActiveWorkbook.Sheets(1).Range(CR3 & CR4)
    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

Please advice as I'm new in VBA
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top