Good afternoon,
I am very new to VBA, so thanks in advance for any assistance provided. I worked through a process of creating a pivot table with appropriate column headers based on a SQL query linked to the excel workbook. This is to populate data in a linear fashion instead of vertical as there can be multiple occurrences for each unique number and I will need each piece. Is there a way to create the proper formulas based on the value in the header? Below is what I have so far, and the commented section was created through record macro function:
I am very new to VBA, so thanks in advance for any assistance provided. I worked through a process of creating a pivot table with appropriate column headers based on a SQL query linked to the excel workbook. This is to populate data in a linear fashion instead of vertical as there can be multiple occurrences for each unique number and I will need each piece. Is there a way to create the proper formulas based on the value in the header? Below is what I have so far, and the commented section was created through record macro function:
Code:
Range("E1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Count"
Range("E2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-4],[@[accnt_num]])"
Sheets.Add.Name = "Pivot"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"PropertyJurisdictions!R1C1:R1048576C5", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="Pivot!R3C1", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Pivot").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("accnt_num")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("jurisdictionname")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").RowAxisLayout xlTabularRow
Range("C3").Select
ActiveCell.FormulaR1C1 = "=MAX(Table_Query_from_Reporting9[Count])"
For K = 4 To 13
If Cells(3, K - 1).Value > 2 Then
Cells(3, K).FormulaR1C1 = "=RC[-1]-1"
End If
Next K
'Range("D4").Select
'ActiveCell.FormulaR1C1 = _
' "=IF(AND(R[1]C[-3]="""",R[2]C[-3]=""""),R[2]C[-2],"""")"
'Range("C4").Select
'ActiveCell.FormulaR1C1 = "=IF(R[1]C[-2]="""",R[1]C[-1],"""")"
'Range("C4:D4").Select
'Selection.Copy
'Range("C4:D49").Select
'ActiveSheet.Paste
'Application.CutCopyMode = False
'Selection.Copy
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
End Sub
Last edited by a moderator: