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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,215,691
Messages
6,126,219
Members
449,303
Latest member
grantrob

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