Data generated cell location error

AidsA

New Member
Joined
Mar 8, 2016
Messages
1
I’m trying to generate 3 worksheets when the button is clicked in the Deal sheet.
The first sheet is “PivotData” this is what you get when you filter the data in Deal sheet
Second is “LoginRate” this is a Pivot table based on the PivotData sheet.
Last is “Performance” it is also a pivot table based on the PivotData sheet.

My problem is that, I noticed that the transfer data from Deal sheet to PivotData sheet, are located in the second row.
I'm trying to move this up, but there are problems occurring in Pivot table.
How can I move it to the first row?

Also, how can I copy the format of the data from Deal sheet to PivotData sheet? (fonts, color, etc.)

Code:
Private Sub CommandButton1_Click()

'Overwrite sheets
Application.DisplayAlerts = False
 On Error Resume Next
 Sheets("PivotData").Delete
 Sheets("LoginRate").Delete
 Sheets("Performance").Delete
 Application.DisplayAlerts = True
 On Error GoTo 0
 
 
Dim sht1 As Worksheet
    Dim sht2 As Worksheet
    Dim newrange As Range, rw As Range
    Dim colctr As Integer
    Dim rowctr As Integer
    Dim sentinel As Integer
    Dim Sht As Worksheet
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable
    Dim StartPvt As String
    Dim SrcData As String
    Dim StartData As String
     
    sentinel = 0
    rowctr = 1
    Set sht1 = ThisWorkbook.Sheets("Deal")
    

     With ThisWorkbook
        Set sht2 = .Sheets.Add(After:=.Sheets(.Sheets.Count))
        sht2.Name = "PivotData"
    End With
    
    Worksheets("PivotData").Columns("AA").NumberFormat = "0.00%"
    Worksheets("PivotData").Columns("AB").NumberFormat = "0.00%"
    Worksheets("PivotData").Columns("AC").NumberFormat = "0.00%"
    Worksheets("PivotData").Columns("AD").NumberFormat = "0.00%"
    Worksheets("PivotData").Columns("AE").NumberFormat = "0.00"
    Worksheets("PivotData").Columns("AF").NumberFormat = "0.00"
    Worksheets("PivotData").Columns("AG").NumberFormat = "0.00"
    Worksheets("PivotData").Columns("AH").NumberFormat = "0.00"
    Worksheets("PivotData").Columns("AI").NumberFormat = "0.00%"
    Worksheets("PivotData").Columns("AJ").NumberFormat = "0.00"
    Worksheets("PivotData").Columns("AK").NumberFormat = "0.00"
    Worksheets("PivotData").Columns("AL").NumberFormat = "0.00"
    Worksheets("PivotData").Columns("AM").NumberFormat = "0.00"
    
    'filter
    Set newrange = sht1.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
    
    
    With sht2
        .Rows(2 & ":" & .Rows.Count).Delete
    End With
    
    For Each rw In newrange.Rows
            For colctr = 1 To 39
              sht2.Cells(rowctr + 1, colctr).Value = rw.Cells(colctr).Value
           Next colctr
            rowctr = rowctr + 1
            Cells(rowctr, colctr).Copy Cells(rowctr, colctr)

          Next rw


'LOGINRATE-----------------------------------------------

'Range of data
  SrcData = sht2.Name & "!" & Range("A2:AM10000").Address(ReferenceStyle:=xlR1C1)

'Create new worksheet
    With ThisWorkbook
        Set Sht = .Sheets.Add(After:=.Sheets(.Sheets.Count))
        Sht.Name = "LoginRate"
    End With
    
'Where to put the Pivot data
  StartPvt = Sht.Name & "!" & Sht.Range("A1").Address(ReferenceStyle:=xlR1C1)



'Create Pivot Cache from source data
  Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=SrcData)

'Create table from cache
  Set pvt = pvtCache.CreatePivotTable( _
    TableDestination:=StartPvt, _
    TableName:="PivotTable1")


'ROWS
Set pvt = ActiveSheet.PivotTables("PivotTable1")


pvt.PivotFields("Account/Deal").Orientation = xlRowField
pvt.PivotFields("Primary Team").Orientation = xlRowField
pvt.PivotFields("Enterprise ID").Orientation = xlRowField

With ActiveSheet.PivotTables("PivotTable1").PivotFields("Account/Deal")
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Primary Team")
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Enterprise ID")
.PivotItems("(blank)").Visible = False
End With

'VALUES
Dim pfa As String
Dim pfa_Name As String
Dim pfa1 As String
Dim pfa1_Name As String
Dim pfa2 As String
Dim pfa2_Name As String

pfa = "LogInCount"
pfa_Name = "Sum of LogIn Count"
pfa1 = "ExpectedLogin"
pfa1_Name = "Sum of Expected Login"
pfa2 = "LogInRate"
pfa2_Name = "Average of Login Rate"

Set pvt = ActiveSheet.PivotTables("PivotTable1")
'pvt.AddDataField pvt.PivotFields("LogIn Count"), pf_Name, xlSum
'pvt.AddDataField pvt.PivotFields("Expected Login"), pf1_Name, xlSum
pvt.AddDataField pvt.PivotFields("Login Rate"), pfa2_Name, xlAverage


'Column Number Format
    Worksheets("LoginRate").Columns("B").NumberFormat = "0.00%"
    
        
'Auto update - changes the output automatically
pvt.ManualUpdate = False


'PERFORMANCE-----------------------------------------------

 Dim shet As Worksheet
Dim pvtPPCache As PivotCache
Dim pvtP As PivotTable
Dim StartpvtP As String
Dim SourceData As String

'Range of data
'  SourceData = ActiveSheet.Name & "!" & Range("A2:AM1048576").Address(ReferenceStyle:=xlR1C1)

'Create new worksheet
  With ThisWorkbook
        Set shet = .Sheets.Add(After:=.Sheets(.Sheets.Count))
        shet.Name = "Performance"
   End With
    
'Where to put the Pivot data
  StartpvtP = shet.Name & "!" & shet.Range("A1").Address(ReferenceStyle:=xlR1C1)

'Create Pivot Cache from source data
  Set pvtPPCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=SrcData)
    
'Create table from cache
      Set pvtP = pvtPPCache.CreatePivotTable( _
    TableDestination:=StartpvtP, _
    TableName:="PivotTable1")

'ROWS
Set pvtP = ActiveSheet.PivotTables("PivotTable1")

pvtP.PivotFields("Account/Deal").Orientation = xlRowField
pvtP.PivotFields("Primary Team").Orientation = xlRowField
pvtP.PivotFields("Enterprise ID").Orientation = xlRowField
    
'Delete (blanks) rows
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Account/Deal")
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Primary Team")
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Enterprise ID")
.PivotItems("(blank)").Visible = False
End With

'VALUES
Dim pf As String
Dim pf_Name As String
Dim pf1 As String
Dim pf1_Name As String
Dim pf2 As String
Dim pf2_Name As String
Dim pf3 As String
Dim pf3_Name As String
Dim pf4 As String
Dim pf4_Name As String
Dim pf5 As String
Dim pf5_Name As String
Dim pf6 As String
Dim pf6_Name As String
Dim pf7 As String
Dim pf7_Name As String
Dim pf8 As String
Dim pf8_Name As String
Dim pf9 As String
Dim pf9_Name As String
Dim pf10 As String
Dim pf10_Name As String
Dim pf11 As String
Dim pf11_Name As String

pf = "Efficiency"
pf_Name = "Average of Efficiency"
pf1 = "Utilization"
pf1_Name = "Average of Utilization"
pf2 = "ProdHours"
pf2_Name = "Average of Prod Hours %"
pf3 = "AHT"
pf3_Name = "Average of AHT (mins)"
pf4 = "QualityReport"
pf4_Name = "Average of Quality Report"
pf5 = "CWI"
pf5_Name = "Average of Completed Work Items"
pf6 = "ShrinkageHrs"
pf6_Name = "Average of Shrinkage (hrs)"
pf7 = "Shrinkage"
pf7_Name = "Average of Shrinkage %"
pf8 = "Admin"
pf8_Name = "Average of Admin Task (hrs)"
pf9 = "Break"
pf9_Name = "Average of Break Time (hrs)"
pf10 = "TTS"
pf10_Name = "Average of Total Time on System"
pf11 = "ProductiveTime"
pf11_Name = "Average of Productive Time"

Set pvtP = ActiveSheet.PivotTables("PivotTable1")
pvtP.AddDataField pvtP.PivotFields("Efficiency"), pf_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Utilization"), pf1_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Prod Hours %"), pf2_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Average Handling Time (mins)"), pf3_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Quality Report"), pf4_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Completed Work Items"), pf5_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Shrinkage (hrs)"), pf6_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Shrinkage %"), pf7_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Admin Task (hrs)"), pf8_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Break Time (hrs)"), pf9_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Total Time on System"), pf10_Name, xlAverage
pvtP.AddDataField pvtP.PivotFields("Productive Time"), pf11_Name, xlAverage

'Columns Number Format
    Worksheets("Performance").Columns("B").NumberFormat = "0.00%"
    Worksheets("Performance").Columns("C").NumberFormat = "0.00%"
    Worksheets("Performance").Columns("D").NumberFormat = "0.00%"
    Worksheets("Performance").Columns("E").NumberFormat = "0.00"
    Worksheets("Performance").Columns("F").NumberFormat = "0.00"
    Worksheets("Performance").Columns("G").NumberFormat = "0.00"
    Worksheets("Performance").Columns("H").NumberFormat = "0.00"
    Worksheets("Performance").Columns("I").NumberFormat = "0.00%"
    Worksheets("Performance").Columns("J").NumberFormat = "0.00"
    Worksheets("Performance").Columns("K").NumberFormat = "0.00"
    Worksheets("Performance").Columns("L").NumberFormat = "0.00"
    Worksheets("Performance").Columns("M").NumberFormat = "0.00"
    
    
'Remove Error
With ActiveSheet.PivotTables("PivotTable1")
 .DisplayErrorString = True
 .ErrorString = "0"
 End With
 
 'Auto update - changes the output automatically
 pvtP.ManualUpdate = False


 End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,216,494
Messages
6,130,977
Members
449,611
Latest member
Bushra

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