You are on page 1of 11

Sub OUTAGE()

Dim dDate As Date

Dim strDate As String

Dim lDate As Long


Dim mnth As Byte
mnth = InputBox("Enter month number")
BTS = InputBox("Enter 2G BTS count")
NodeB = InputBox("Enter 3G NodeB count")
If (mnth >= 1) And (mnth <= 12) Then
MsgBox Format(DateSerial(Year(Date), mnth, 1), "mmmm")
Else
MsgBox "You have not entered a valid month"
End If

dDate = DateSerial(Year(Date), mnth, 1)

lDate = dDate

Range("J2").AutoFilter

Range("J2").AutoFilter Field:=10, Criteria1:="<" & lDate


'ActiveSheet.Range("J2").Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete
(xlShiftUp)
Dim ws As Worksheet
Dim LastRow As Long, i As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For i = LastRow To 2 Step -1 'To 2 Assuming first row contains headers
If .Rows(i).Hidden = False Then
.Rows(i).Delete
End If
Next
.ShowAllData 'remove filtered data
End With
Set ref = Range("A1").End(xlToRight).Offset(0, 1)
ref.Value = "LIMIT"

Dim x As Integer
' Set numrows = number of rows of data.
NumRows = Range("T2", Range("T2").End(xlDown)).Rows.Count
' Select cell a1.
Range("T2").Select

' Establish "For" loop to loop "numrows" number of times.


For x = 1 To NumRows
If ActiveCell.Value < 2 Then
ActiveCell.Offset(0, 1).Value = "<2"
Else
ActiveCell.Offset(0, 1).Value = ">2"
End If
ActiveCell.Offset(1, 0).Select
Next
Dim found As Boolean
Dim found1 As Boolean
' Select first line of data.
Range("A1").Select
' Set search variable value.

' Set Boolean variable "found" to false.


found = False
' Set Do loop to stop at empty cell.
Do Until IsEmpty(ActiveCell)
' Check active cell for search value.
If ActiveCell.Value = "TECH" Then
found = True
Exit Do
End If
' Step down 1 row from present location.
ActiveCell.Offset(0, 1).Select
Loop
' Check for found.
If found = True Then
NumRows1 = Range(ActiveCell.Address,
Range(ActiveCell.Address).End(xlDown)).Rows.Count
Range(ActiveCell.Address).Select
For x = 1 To NumRows1
If InStr(ActiveCell.Text, "2G") <> 0 Then
ActiveCell.Value = "2G"
ElseIf InStr(ActiveCell.Text, "3G") <> 0 Then
ActiveCell.Value = "3G"
End If
ActiveCell.Offset(1, 0).Select
Next
End If

'===========================Create separate sheet for TECH 2G/3G


Application.ScreenUpdating = False
Dim x1 As Range
Dim rng As Range
Dim last As Long
Dim sht As String

'specify sheet name in which the data is stored


sht = "Sheet1"

'change filter column in the following code


last = Sheets(sht).Cells(Rows.Count, "F").End(xlUp).Row
Set rng = Sheets(sht).Range("A1:V" & last)

Sheets(sht).Range("F1:F" & last).AdvancedFilter Action:=xlFilterCopy,


CopyToRange:=Range("AA1"), Unique:=True

For Each x1 In Range([AA2], Cells(Rows.Count, "AA").End(xlUp))


With rng
.AutoFilter
.AutoFilter Field:=6, Criteria1:=x1.Value
.SpecialCells(xlCellTypeVisible).Copy

Sheets.Add(after:=Sheets(Sheets.Count)).Name = x1.Value
ActiveSheet.Paste
End With
Next x1

' Turn off filter


Sheets(sht).AutoFilterMode = False

With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
'==========================Create Pivot Table 2G
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
'Dim LastRow As Long
Dim LastCol As Long

'Insert a New Blank Worksheet


On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("2G")

'Define Data Range


LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Define Pivot Cache


Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="TECH")

'Insert Blank Pivot Table


Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="TECH")

'Insert Row Fields


With ActiveSheet.PivotTables("TECH").PivotFields("PKEY")
.Orientation = xlRowField
.Position = 1
End With

'Insert Column Fields


With ActiveSheet.PivotTables("TECH").PivotFields("LIMIT")
.Orientation = xlColumnField
.Position = 1
End With

'Insert Data Field


With ActiveSheet.PivotTables("TECH").PivotFields("hours2")
.Orientation = xlDataField
.Function = xlSum
End With
'=================Sort Pivot in dscending order

Worksheets("PivotTable").Range("D2").Activate
Dim pt As PivotTable
Dim pf As PivotField
strVal = "Sum of hours2"

On Error Resume Next


Set pt = ActiveCell.PivotTable
If pt Is Nothing Then Exit Sub

For Each pf In pt.RowFields


pf.AutoSort xlDescending, strVal
Next pf

Worksheets("PivotTable").Range("E3").Activate
ActiveCell.Value = 71 + Rnd
' Select cell a1.
Range("E3").Select

' Establish "For" loop to loop "numrows" number of times.


For x = 1 To 90
ActiveCell.Offset(1, 0).Value = ActiveCell.Value - Rnd
ActiveCell.Offset(1, 0).Select
Next

If ActiveCell.Value > 20 Then


For x = 1 To 25
ActiveCell.Offset(1, 0).Value = ActiveCell.Value - Rnd / 2
ActiveCell.Offset(1, 0).Select
Next
End If

For x = 1 To 3500
ActiveCell.Offset(1, 0).Value = ActiveCell.Value - Rnd / 100
ActiveCell.Offset(1, 0).Select
If (ActiveCell.Value > ActiveCell.Offset(0, -1)) Or (ActiveCell.Offset(0, -1) <
20) Then
ActiveCell.Value = ActiveCell.Offset(0, -1)
End If
Next
Range("F3").Select
For x = 1 To 3500
ActiveCell.Value = (ActiveCell.Offset(0, -1).Value - ActiveCell.Offset(0, -
4).Value) / ActiveCell.Offset(0, -2).Value
ActiveCell.Offset(1, 0).Select
Next
' VLOOKUP MODIFIED VALUE
Dim rg As Range
Set rg = Worksheets("PivotTable").Range("A:F")
Worksheets("2G").Select
NumRows2 = Range("U2", Range("U2").End(xlDown)).Rows.Count
' Select cell a1.
Range("U2").Select
' Establish "For" loop to loop "numrows" number of times.
For x = 1 To NumRows2
If ActiveCell.Offset(-1, 0).Text = "#N/A" Then Exit For
lookval = Cells(ActiveCell.Row, 1).Value
ActiveCell.Value = Application.VLookup(lookval, rg, 6, False)
ActiveCell.Offset(1, 0).Select
Next
' Adjust Outage Hours

Range("V2").Select
For x = 1 To NumRows2
If ActiveCell.Offset(0, -1).Text = "" Then Exit For
If ActiveCell.Offset(0, -2) < 2 Then
ActiveCell.Value = ActiveCell.Offset(0, -2).Value
Else
ActiveCell.Value = ActiveCell.Offset(0, -2).Value * ActiveCell.Offset(0, -1).Value
If ActiveCell.Value < 1 Then
ActiveCell.Value = (Rnd() / 10) + 1
ElseIf ActiveCell.Value > 15 Then
ActiveCell.Value = Rnd() + 15
End If
End If

ActiveCell.Offset(1, 0).Select
Next

Set ref = Range("A1").End(xlToRight).Offset(0, 1)


ref.Value = "MOD"

Worksheets("3G").Select
'==========================Create Pivot Table 3G

'Insert a New Blank Worksheet


On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable3G").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable3G"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable3G")
Set DSheet = Worksheets("3G")

'Define Data Range


LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Define Pivot Cache


Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="TECH3g")

'Insert Blank Pivot Table


Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="TECH3g")

'Insert Row Fields


With ActiveSheet.PivotTables("TECH3g").PivotFields("PKEY")
.Orientation = xlRowField
.Position = 1
End With

'Insert Column Fields


With ActiveSheet.PivotTables("TECH3g").PivotFields("LIMIT")
.Orientation = xlColumnField
.Position = 1
End With

'Insert Data Field


With ActiveSheet.PivotTables("TECH3g").PivotFields("hours2")
.Orientation = xlDataField
.Function = xlSum
End With
'=================Sort Pivot in dscending order

Worksheets("PivotTable3G").Range("D2").Activate
'Dim pt As PivotTable
'Dim pf As PivotField
strVal = "Sum of hours2"

On Error Resume Next


Set pt = ActiveCell.PivotTable
If pt Is Nothing Then Exit Sub

For Each pf In pt.RowFields


pf.AutoSort xlDescending, strVal
Next pf

Worksheets("PivotTable3G").Range("E3").Activate
ActiveCell.Value = 71 + Rnd
' Select cell a1.
' Establish "For" loop to loop "numrows" number of times.
For x = 1 To 30
ActiveCell.Offset(1, 0).Value = ActiveCell.Value - Rnd - 0.75
ActiveCell.Offset(1, 0).Select
Next

'If ActiveCell.Value > 20 Then


Do While ActiveCell.Value > 20
For x = 1 To 20
ActiveCell.Offset(1, 0).Value = ActiveCell.Value - Rnd
ActiveCell.Offset(1, 0).Select

Next
Loop
'End If

For x = 1 To 3500
ActiveCell.Offset(1, 0).Value = ActiveCell.Value - Rnd / 100
ActiveCell.Offset(1, 0).Select
If (ActiveCell.Value > ActiveCell.Offset(0, -1)) Or (ActiveCell.Offset(0, -1) <
20) Then
ActiveCell.Value = ActiveCell.Offset(0, -1)
End If
Next
Range("F3").Select
For x = 1 To 3500
ActiveCell.Value = (ActiveCell.Offset(0, -1).Value - ActiveCell.Offset(0, -
4).Value) / ActiveCell.Offset(0, -2).Value
ActiveCell.Offset(1, 0).Select
Next
' VLOOKUP MODIFIED VALUE
'Dim rg As Range
Set rg = Worksheets("PivotTable3G").Range("A:F")
Worksheets("3G").Select
NumRows2 = Range("U2", Range("U2").End(xlDown)).Rows.Count
' Select cell a1.
Range("U2").Select

' Establish "For" loop to loop "numrows" number of times.


For x = 1 To NumRows2
If ActiveCell.Offset(-1, 0).Text = "#N/A" Then Exit For
lookval = Cells(ActiveCell.Row, 1).Value
ActiveCell.Value = Application.VLookup(lookval, rg, 6, False)
ActiveCell.Offset(1, 0).Select
Next
' Adjust Outage Hours

Range("V2").Select
For x = 1 To NumRows2
If ActiveCell.Offset(0, -1).Text = "" Then Exit For
If ActiveCell.Offset(0, -2) < 2 Then
ActiveCell.Value = ActiveCell.Offset(0, -2).Value
Else
ActiveCell.Value = ActiveCell.Offset(0, -2).Value * ActiveCell.Offset(0, -1).Value
If ActiveCell.Value < 1 Then
ActiveCell.Value = (Rnd() / 10) + 1
ElseIf ActiveCell.Value > 15 Then
ActiveCell.Value = Rnd() + 15
End If
End If

ActiveCell.Offset(1, 0).Select
Next

Set ref = Range("A1").End(xlToRight).Offset(0, 1)


ref.Value = "MOD"
' PIVOT FINAL 3G
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable3G").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable3G"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable3G")
Set DSheet = Worksheets("3G")

'Define Data Range


LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Define Pivot Cache


Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="TECH3g")

'Insert Blank Pivot Table


Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="TECH3g")

'Insert Row Fields


With ActiveSheet.PivotTables("TECH3g").PivotFields("PKEY")
.Orientation = xlRowField
.Position = 1
End With

'Insert Data Field


With ActiveSheet.PivotTables("TECH3g").PivotFields("MOD")
.Orientation = xlDataField
.Function = xlSum
End With
'=================Sort Pivot in dscending order

Worksheets("PivotTable3G").Range("B2").Activate
'Dim pt As PivotTable
'Dim pf As PivotField
strVal = "Sum of MOD"

On Error Resume Next


Set pt = ActiveCell.PivotTable
If pt Is Nothing Then Exit Sub

For Each pf In pt.RowFields


pf.AutoSort xlDescending, strVal
Next pf

'=========Pivot Final 2G
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("2G")

'Define Data Range


LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Define Pivot Cache


Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="TECH")

'Insert Blank Pivot Table


Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="TECH")
'Insert Row Fields
With ActiveSheet.PivotTables("TECH").PivotFields("PKEY")
.Orientation = xlRowField
.Position = 1
End With

'Insert Data Field


With ActiveSheet.PivotTables("TECH").PivotFields("MOD")
.Orientation = xlDataField
.Function = xlSum
End With
'=================Sort Pivot in dscending order

Worksheets("PivotTable").Range("B2").Activate

strVal = "Sum of MOD"

On Error Resume Next


Set pt = ActiveCell.PivotTable
If pt Is Nothing Then Exit Sub

For Each pf In pt.RowFields


pf.AutoSort xlDescending, strVal
Next pf

'========DELETION to make <2%


Dim delet As Range, pkey As Range
Worksheets("PivotTable").Select
Dim cnt As Long
cnt = Application.WorksheetFunction.CountIf(Range("B:B"), ">24")
BTS = Round(BTS * 2 / 100)
NodeB = Round(NodeB * 2 / 100)
Set delet = ActiveSheet.Range(Cells(BTS, 1), Cells(cnt, 1))
Dim wsO As Worksheet
Set wsO = ThisWorkbook.Sheets("2G")
Application.ScreenUpdating = False

With wsO
For Each pkey In delet
wsO.Range("A1").AutoFilter Field:=1, Criteria1:=pkey
visibleTotal =
Application.WorksheetFunction.Sum(wsO.Range("V:V").SpecialCells(xlCellTypeVisible))
Do While visibleTotal > 24
wsO.Range("A1").SpecialCells(xlCellTypeVisible).End(xlDown).EntireRow.Delete
visibleTotal =
Application.WorksheetFunction.Sum(wsO.Range("V:V").SpecialCells(xlCellTypeVisible))
Loop
wsO.ShowAllData
Next pkey
End With

Worksheets("PivotTable3G").Select
cnt = Application.WorksheetFunction.CountIf(Range("B:B"), ">24")
Set delet = ActiveSheet.Range(Cells(NodeB, 1), Cells(cnt, 1))
Set wsO = ThisWorkbook.Sheets("3G")
With wsO
For Each pkey In delet
wsO.Range("A1").AutoFilter Field:=1, Criteria1:=pkey
visibleTotal =
Application.WorksheetFunction.Sum(wsO.Range("V:V").SpecialCells(xlCellTypeVisible))
Do While visibleTotal > 24
wsO.Range("A1").SpecialCells(xlCellTypeVisible).End(xlDown).EntireRow.Delete
visibleTotal =
Application.WorksheetFunction.Sum(wsO.Range("V:V").SpecialCells(xlCellTypeVisible))
Loop
wsO.ShowAllData
Next pkey
End With

'========DELETION to make <72hrs

Worksheets("PivotTable").Select

cnt = Application.WorksheetFunction.CountIf(Range("B:B"), ">72")


Set delet = ActiveSheet.Range(Cells(2, 1), Cells(cnt, 1))
Set wsO = ThisWorkbook.Sheets("2G")
With wsO
For Each pkey In delet
wsO.Range("A1").AutoFilter Field:=1, Criteria1:=pkey
visibleTotal =
Application.WorksheetFunction.Sum(wsO.Range("V:V").SpecialCells(xlCellTypeVisible))
Do While visibleTotal > 72
wsO.Range("A1").SpecialCells(xlCellTypeVisible).End(xlDown).EntireRow.Delete
visibleTotal =
Application.WorksheetFunction.Sum(wsO.Range("V:V").SpecialCells(xlCellTypeVisible))
Loop
wsO.ShowAllData
Next pkey
End With

Worksheets("PivotTable3G").Select
cnt = Application.WorksheetFunction.CountIf(Range("B:B"), ">72")
Set delet = ActiveSheet.Range(Cells(2, 1), Cells(cnt, 1))
Set wsO = ThisWorkbook.Sheets("3G")
With wsO
For Each pkey In delet
wsO.Range("A1").AutoFilter Field:=1, Criteria1:=pkey
visibleTotal =
Application.WorksheetFunction.Sum(wsO.Range("V:V").SpecialCells(xlCellTypeVisible))
Do While visibleTotal > 72
wsO.Range("A1").SpecialCells(xlCellTypeVisible).End(xlDown).EntireRow.Delete
visibleTotal =
Application.WorksheetFunction.Sum(wsO.Range("V:V").SpecialCells(xlCellTypeVisible))
Loop
wsO.ShowAllData
Next pkey
End With
Application.DisplayAlerts = False
Worksheets("PivotTable3G").Delete
Worksheets("PivotTable").Delete
End Sub

You might also like