I need to select the columns from C4 : last column -1 in the excel sheet and then i need to deselect few columns from the selected range and then group the columns which are selected as single column. The actions which needs to be performed is on pivot table sheet. Please refer the screenshot below
Please use a invoke vba…you can pass the required start and wnd dynamically
Also to know read the data into datatable and decide the atart and end using whatever logic you want to define and then pass the start and end to the vba
Could create this vba for passing cell numbers you need
Sub GroupColumns(cellNumbers As String)
Dim cellArray() As String
Dim cellRef As String
Dim columnRange As Range
' Split the input string into an array of cell references
cellArray = Split(cellNumbers, ",")
' Loop through each cell reference and group columns in the pivot table
For Each cellRef In cellArray
' Convert cell reference to column range
Set columnRange = Range(cellRef).EntireColumn
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Years")
.LabelRange.Cells(1, 2).Group Start:=(Excel.Application.WorksheetFunction.EoMonth(columnRange, -2) + 1), End:=Excel.Application.WorksheetFunction.EoMonth(columnRange, 11), Periods:=Array(False, False, False, False, True, False, True)
.Position = 1
End With
Next cellRef
End Sub
I have tried this below macro but it is giving error at step Selection.Group
Sub FinalMacro(RangeToSelect As String, DeselectRange As String)
Range(“C4:” & RangeToSelect).Select
For Each cell In DeselectRange
cell.Select
Selection.Interior.ColorIndex = xlNone ’ Remove any cell color
Next cell
Selection.Group
Range(“C4”).Select
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Loadg Date2”).PivotItems( _
“Group1”).Caption = “Backlog”
Range(“C4”).Select
ActiveSheet.PivotTables(“PivotTable1”).PivotSelect “Backlog”, xlDataAndLabel, _
True
ActiveSheet.PivotTables(“PivotTable1”).PivotFields(“Loadg Date2”).ShowDetail = _
False
End Sub