Below is code to format your data, but before I go any further with this, can you paste this code into your Prod_Month sheet, run it and confirm whether that's how your data should be arranged? If so, then I'll put the chart code together ...
Option Explicit
Sub CreateChartData()
Dim wsh As Worksheet
Dim strTargetName As String, strGroupName As String
Dim strLeaseCol As String, strWellCol As String, strMonthCol As String, strGasCol As String, strAPI_Col As String
Dim intFirstRow As Integer, intLastCol As Integer
Dim lngLastRow As Long, lngStartRow As Long, lngEndRow As Long
Dim intCountGroups As Integer
Dim i As Long, j As Long
'initialise:
strTargetName = "CHART DATA"
'you can set these dynamically if you like; I've just assumed they're static for now:
strLeaseCol = "D"
strWellCol = "E"
strAPI_Col = "F"
strMonthCol = "I"
strGasCol = "Y"
intFirstRow = 2
'establish boundaries:
lngLastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
intLastCol = Cells.SpecialCells(xlCellTypeLastCell).Column
'if target sheet doesn't exist - create it; if it does - clear it:
For Each wsh In Worksheets
If wsh.Name = strTargetName Then GoTo ClearTarget
Next wsh
ThisWorkbook.Worksheets.Add after:=Worksheets.Count
Worksheets(Worksheets.Count).Name = strTargetName
ClearTarget:
Worksheets(strTargetName).Cells.Clear
'1-600 in the first col:
For i = 1 To 600
Worksheets(strTargetName).Range("A" & i).Value = i
Next i
'sort the data by lease name, well number & month:
Range(Cells(intFirstRow, 1), Cells(lngLastRow, intLastCol)).Sort _
key1:=Range(strLeaseCol & intFirstRow), Order1:=xlAscending, _
key2:=Range(strWellCol & intFirstRow), Order2:=xlAscending, _
key3:=Range(strMonthCol & intFirstRow), Order2:=xlAscending, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortTextAsNumbers, DataOption3:=xlSortNormal, _
header:=xlYes
'generate the target data for each unique API:
lngStartRow = intFirstRow
lngEndRow = 0
intCountGroups = 0
For i = intFirstRow To lngLastRow
If Range(strAPI_Col & intFirstRow - 1).Offset(i - 1, 0) = Range(strAPI_Col & intFirstRow - 1).Offset(i, 0) Then
'keep looking:
Else
'next group found:
intCountGroups = intCountGroups + 1
strGroupName = Range(strLeaseCol & i).Text & Range(strWellCol & i).Text
lngEndRow = i
Worksheets(strTargetName).Range("A" & intFirstRow - 1).Offset(0, intCountGroups * 2 + 1).Value = strGroupName
Range(strMonthCol & lngStartRow & ":" & strMonthCol & lngEndRow).Copy
Worksheets(strTargetName).Range("A" & intFirstRow).Offset(0, intCountGroups * 2).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Range(strGasCol & lngStartRow & ":" & strGasCol & lngEndRow).Copy
Worksheets(strTargetName).Range("A" & intFirstRow).Offset(0, intCountGroups * 2 + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
lngStartRow = lngEndRow + 1
End If
Next i
Worksheets(strTargetName).Columns.AutoFit
End Sub
If this is not right, then let me know where I've gone wrong & I'll take another look. Cheers, MM.
Bookmarks