Modify VBA code for different data structure

artz

Well-known Member
Joined
Aug 11, 2002
Messages
830
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I've found some VBA code that could do what I need if I could figure out how to modify it. After a number of attempts at trying to modify it to do what I need, I am posting it to the Forum.

Basically, the code takes several several rows and columns of data and creates a series of charts on a separate worksheet, Charts.

One obvious difference is that the x-values called in the code are in a row compared to a column as in my data.

Also, in the code, fixed lengths of the data fields are used. My data varies in the number of rows and columns of data. In the original worksheet, the X-values are in a row: E6 to BE6. My data has the x-data in starting column M, at M3. Y-values begin in column N, at N3.

Below is an excerpt from the original worksheet data:
Excel Workbook
ABCDEFGHIJK
1***********
2***********
3***********
4****20100107201001142010012120100128201002042010021120100218
5***********
6****7-Jan-1014-Jan-1021-Jan-1028-Jan-104-Feb-1011-Feb-1018-Feb-10
7*29.20Task 1ACWP1.02.03.04.05.06.07.0
8*29.20Task 1BCWP2.03.04.05.06.07.08.0
9*29.20Task 1BCWS3.04.05.06.07.08.09.0
10*29.20Task 1EAC4.05.06.07.08.09.010.0
11*29.21Task 2ACWP5.06.07.08.09.010.011.0
12*29.21Task 2BCWP6.07.08.09.010.011.012.0
13*29.21Task 2BCWS7.08.09.010.011.012.013.0
14*29.21Task 2EAC8.09.010.011.012.013.014.0
15*29.22Task 3ACWP9.010.011.012.013.014.015.0
16*29.22Task 3BCWP10.011.012.013.014.015.016.0
17*29.22Task 3BCWS11.012.013.014.015.016.017.0
18*29.22Task 3EAC12.013.014.015.016.017.018.0
19*29.23Task 4ACWP13.014.015.016.017.018.019.0
20*29.23Task 4BCWP14.015.016.017.018.019.020.0
21*29.23Task 4BCWS15.016.017.018.019.020.021.0
22*29.23Task 4EAC16.017.018.019.020.021.022.0
23*29.24Task 5ACWP17.018.019.020.021.022.023.0
Data



Again, below is the sub which creates the charts on the Charts worksheet:

Code:
Sub Refresh()

'Variables
'wsCharts and wsData are the two worksheets
' conSpacing is the constant for the spacing between graphs (20 rows)
' this is based on how excel creates graphs on my machine, it changes if
' excel isn't full screen (I think)
' i and j are just counters
' LastRow is the final data entry in the table in column C

    Dim wsCharts As Worksheet
    Set wsCharts = Worksheets("Charts")
    Dim wsData As Worksheet
    Set wsData = Worksheets("Data")
    Const conSpacing = 20, conStartInRow = 6
    Dim i, j, LastRow As Long
    
'Delete all existing charts on the page

    wsCharts.ChartObjects.Delete

'Find the last data entry in column C

    With wsData
        LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
    End With
    
'Add a blank chart
'Linemarkers is the chart type
'set a dummy data range, and tell the graph to plot by rows
'Put the chard in the charts worksheet
'Tell the sheet it has a title
'Set the x and y axis as Date and Hours
'Cut the sample chart

    Charts.Add
    ActiveChart.ChartType = xlLineMarkers
        ActiveChart.SetSourceData Source:=wsData.Range("E6:BE10"), PlotBy:= _
        xlRows
    ActiveChart.Location where:=xlLocationAsObject, Name:=wsCharts.Name
    With ActiveChart
        .HasTitle = True
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Hours"
    End With
    wsCharts.ChartObjects.Cut
    
'For 1 to the number of groups of four

    For i = 1 To (LastRow - conStartInRow) / 4
    
'If it is the first i, select cell A1, else select the cell according to the spacing
        
        If i = 1 Then
            wsCharts.Cells(1, 1).Select
        Else
            wsCharts.Cells((i - 1) * conSpacing, 1).Select
        End If
        
'Paste the chart to this location

        wsCharts.Paste
        
'Set the series 1 to 4, according to the 4 data ranges in the table
        
        For j = 0 To 3
    
            ActiveChart.SeriesCollection(j + 1).XValues = "=" & wsData.Name & "!R6C5:R6C57"
            
            ActiveChart.SeriesCollection(j + 1).Values = "=" & wsData.Name & "!R" & _
                                (i * 4) + (conStartInRow / 2) + j & "C5:" _
                                & "R" & (i * 4) + (conStartInRow / 2) + j & "C57"
        
            ActiveChart.SeriesCollection(j + 1).Name = "=" & wsData.Name & "!R" & _
                                    (i * 4) + (conStartInRow / 2) + j & "C3:" _
                                    & "R" & (i * 4) + (conStartInRow / 2) + j & "C4"
            
        Next

'Give the chart a title of the first label of the data series

        With ActiveChart
            .ChartTitle.Characters.Text = wsData.Cells(i * 4 + conStartInRow, 3)
        End With
    Next
    
    

End Sub

My data is laid out a little bit differently. As mentioned above, x-values are in column M with y-values starting in column N extending to column BE.

Also the data length in the original VBA code is fixed. My number of data rows is variable.

Another difference too, is that the original VBA creates multiple series on the same chart. I need one set of x-values (column M) and one y-series (starting at column N) for each chart.

A sample of my worksheet is shown below:

Excel Workbook
MNOPQRSTUVWXY
1*************
2DateAAAIGAXPBACCATDDDISGEGMHDHON
31/3/129.224.0748.2173.7928.3293.5646.1438.3118.221.0542.1455.23
41/4/129.4223.9248.2473.928.1694.4346.6438.8518.421.1542.7455.18
51/5/129.3323.9148.873.128.595.0946.3339.518.3922.1743.0955.24
61/6/129.1323.5448.2773.5528.5495.3345.6739.9118.4922.9243.254.83
71/9/129.42448.3974.0929.0796.6746.0639.7518.6922.8443.2355.29
81/10/129.4125.0748.6774.5629.9999.5246.7639.6318.5623.2443.5356.23
91/11/129.625.3748.9574.331.2699.246.9238.718.7124.4743.4656.11
101/12/129.925.4349.6575.0731.59101.4947.7138.7318.7624.6743.3956.83
111/13/129.7724.9549.7674.1630.73102.0248.0138.418.6724.2943.5156.34
121/17/129.7324.5650.2274.828.21102.9148.1538.4818.5824.243.7456.8
131/18/129.9925.2350.5674.6229.02104.2649.0539.0218.8524.5144.8857.83
141/19/1210.1525.5550.9575.1229.32105.754939.4418.9824.8245.4158.13
151/20/1210.1425.6550.0475.0829.63105.6449.0239.3118.982544.5157.02
161/23/1210.2225.4449.4475.0729.84106.3748.9539.2518.7724.9244.8857.26
171/24/1210.2425.3249.2374.9229.89106.2949.0139.2518.6724.7944.9657.27
181/25/1210.4525.3150.1775.3829.95109.0550.1839.5618.9624.9245.2657.75
191/26/1210.3325.1449.9874.8730.37111.3150.5339.3518.924.7244.9557.47
201/27/1210.425.2549.8574.1130.86111.2850.3139.2518.8624.3744.8757.9
211/30/1210.2925.249.1273.7330.22110.4150.5638.9918.7324.2344.7757.82
221/31/1210.1325.1150.1473.7530.71109.1250.4838.918.5524.0244.3957.68
Data


Could any of the VBA experts in the Forum take a stab at modifying the code above to create the series of charts based the VBA code above but using my data format?

Any help is greatly appreciated.

Thanks,

Art
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

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