, , A C. . , .
Option Explicit
Private Sub CommandButton1_Click()
Dim thisWS As Worksheet
Dim firstRow As Double
Dim lastRow As Double
Dim workCol As Double
Dim dataRange As Range
Dim uniqueLast As Double
Dim uniqueCol As Double
Dim i As Double
Dim y As Double
Dim Temp As String
Dim found_Bool As Boolean
Set thisWS = ThisWorkbook.Worksheets("Sheet2")
workCol = thisWS.Range("A1").Column
firstRow = 1
uniqueLast = 1
uniqueCol = thisWS.Range("C1").Column
lastRow = thisWS.Cells(thisWS.Rows.Count, workCol).End(xlUp).Row
For i = firstRow To lastRow
Temp = Trim(UCase(thisWS.Range(Cells(i, workCol), Cells(i, workCol))))
Temp = Replace(Temp, "#", "")
found_Bool = False
For y = 1 To uniqueLast
If Temp = thisWS.Range(Cells(y, uniqueCol), Cells(y, uniqueCol)) Then
found_Bool = True
Else ' Do nothing
End If
Next y
If found_Bool = False Then
thisWS.Range(Cells(uniqueLast + 1, uniqueCol), Cells(uniqueLast + 1, uniqueCol)) = Temp
uniqueLast = uniqueLast + 1
Else
End If
Next i
End Sub
non unique .
, , VBA ( (), vba pivot, , .
'***************************************************
, , . , "". , , . , , , , , .
, , , , , , , , :
'***************************
'Add Sales Pivot Table
'Last DR is the last data row, you can see it done several times, in the code below, once you do it you will always do it
'CalcSheet is the name of the worksheet in the workbook I am working on
'The range here is defined in Range Format, you could use a named range or use .Range(Cells(row,col),Cells(row,col)) there are several ways
'I name the pivot table upon creation so I can manipulate it better
'I specify the target cell, upper left with which to begin the pivot table
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
CalcSheet.Range("K14:AY" & LastDR), Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:=CalcSheet.Range("CA37"), TableName:="SalesPVT", DefaultVersion _
:=xlPivotTableVersion15
, , :
With CalcSheet.PivotTables("SalesPVT").PivotFields("Salesperson")
.Orientation = xlRowField
.Position = 1
End With
With CalcSheet.PivotTables("SalesPVT").PivotFields("Customer")
.Orientation = xlRowField
.Position = 2
End With
With CalcSheet.PivotTables("SalesPVT").PivotFields("DD Rev")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "$#,##0"
End With
With CalcSheet.PivotTables("SalesPVT").PivotFields("Job Days")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0"
End With
CalcSheet.PivotTables("SalesPVT").PivotFields("Salesperson").AutoSort _
xlDescending, "Sum of DD Rev"
, , , () ? , , , :
'Find the last row of Pivot table Data
Dim LastPVTrow As Double
Dim FirstPVTrow As Double
Dim NumPVTrows As Double
Dim PivCol As Double
PivCol = CalcSheet.Range("CB37").Column
FirstPVTrow = CalcSheet.Range("CB37").Row
LastPVTrow = CalcSheet.Cells(Rows.Count, PivCol).End(xlUp).Row
NumPVTrows = LastPVTrow - FirstPVTrow
- , , :
' Avg Rev/Job Day Column
For i = 1 To NumPVTrows ' four columns in this table
CalcSheet.Range("CD" & (100 + i)).NumberFormat = "$#,##0"
If CalcSheet.Range("CC" & (FirstPVTrow + i)) <> 0 Then
CalcSheet.Range("CD" & (100 + i)) = CalcSheet.Range("CB" & (FirstPVTrow + i)) / CalcSheet.Range("CC" & (FirstPVTrow + i))
Else
CalcSheet.Range("CD" & (100 + i)) = 0
End If
Next i
' , , , ..
' , , ,
'copy pivot table to get rid of it
CalcSheet.PivotTables("SalesPVT").TableRange1.Copy
'Paste it as values with formatting
CalcSheet.Range("CA100").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'Delete Sales Pivot from the file
CalcSheet.PivotTables("SalesPVT").TableRange1.Delete
'Clear Work Space
CalcSheet.Range("CA1:CN500").Clear
, , , , , , , . : , , - , , , .
, ,
'***************************************
'Make the Customer Pivot and table
'***************************************
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
CalcSheet.Range("K14:AY" & LastDR), Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:=CalcSheet.Range("CA37"), TableName:="CustPVT", DefaultVersion _
:=xlPivotTableVersion15
With CalcSheet.PivotTables("CustPVT").PivotFields("Customer")
.Orientation = xlRowField
.Position = 1
End With
With CalcSheet.PivotTables("CustPVT").PivotFields("DD Rev")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "$#,##0"
End With
With CalcSheet.PivotTables("CustPVT").PivotFields("Job Days")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0"
End With
'Find the last row of Pivot table Data
FirstPVTrow = CalcSheet.Range("CA37").Row
LastPVTrow = CalcSheet.Cells(Rows.Count, PivCol).End(xlUp).Row
'LastPVTrow = CalcSheet.Range("CB37:CB500").Find((0), LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
NumPVTrows = LastPVTrow - FirstPVTrow
... .. ..,
, .
, , (, ) , , , , " " , , , . , " ", , , , ., , . , . , .