ok - Excel VBA -
is running very slow, and weird behaviour - Is something else running?
- the text I'm typing sometimes gets all "rearranged" automatically (ie. first letter I type will be at the end all of a sudden)
- the text of the current line I'm on turns all red
- In the middle of typing something, a window will pop up "Syntax Error" even before I'm done typing.
I have turned off the addins (I have TM1 - which is a pain in the rear end)
I have optimised the code i am using, and have been coding for a long time and there is NOTHING that should be taking this long...
HELP!!!
Sub CreateCopy3()
Dim x As Long
Dim sumFilterNo As Long
Dim m As Long
Dim DelMe As Long
Dim nCount As Long
Dim lRowC_DoW
Dim newSh As String
Dim mp As Long
Dim shDoW
Dim shData As String
Dim shCons As String
Dim shXX As String
Dim shDoWXX As String
Dim sFilter As String
Dim sFilterCol As String
Dim sFilterColNumber As Long
Dim shName As String
Dim sFilterBy As String
Dim lRowC As Long
Dim lRowC_Sum As Long
Dim lRowC_new As Long
Dim niceName As String
Dim l As Long
Dim RptFilteredBy As String
Dim lLastRow As Long, lLastColumn As Long
Dim lRealLastRow As Long, lRealLastColumn As Long
Dim arrAgent() As String
Dim j As Long
Application.ScreenUpdating = False
shDoWXX = "DOW XX"
shXX = "ZZ"
shData = "Data"
shCons = "Consolidated"
Sheets("Summary").Select
sFilter = Range("B2").Value
sFilterBy = Range("B3").Value
lRowC = ActiveSheet.UsedRange.Rows.Count - 11
Select Case sFilter
Case "AGENT_CODE"
shName = "Agent"
sFilterCol = "J"
sumFilterNo = 1
niceName = "Agent Code"
sFilterColNumber = 1
Case "ACCOUNT_MANAGER"
sFilterCol = "F"
shName = "AM"
sumFilterNo = 5
niceName = "Account Manager"
sFilterColNumber = 30
Case "Regional_Sales_Manager"
sFilterCol = "G"
sumFilterNo = 6
shName = "SM"
sFilterColNumber = 31
niceName = "Reg. Sales Manager"
Case "Customer"
shName = "Customer"
sFilterCol = "I"
sumFilterNo = 9
niceName = "Customer"
sFilterColNumber = 33
Case "Region"
shName = "Region"
sFilterCol = "C"
sumFilterNo = 2
niceName = "Region"
sFilterColNumber = 29
Case "Top_Level_Region"
sumFilterNo = 1
shName = "Top Region"
sFilterCol = "B"
niceName = "Top Level Region"
sFilterColNumber = 28
Case Else
MsgBox "No Selection - operation cancelled"
Exit Sub
End Select
RptFilteredBy = niceName & " filtered by " & Range("B3").Value
Range("B9").Value = RptFilteredBy
Application.DisplayAlerts = False
Worksheets(shData).Activate
lRowC = ActiveSheet.UsedRange.Rows.Count
Sheets("Summary").Select
'Range("A13:Z" & lRowC).Clear
If ActiveSheet.AutoFilterMode = True Then
' Range("A3:AZ3").Select
Selection.AutoFilter
End If
Range("A13:Z" & lRowC).Clear
Worksheets(shCons).Activate
If ActiveSheet.AutoFilterMode = False Then
Range("A3:AZ3").Select
Selection.AutoFilter
End If
If ActiveSheet.AutoFilterMode = True Then
Range("A3:AZ3").Select
Selection.AutoFilter
End If
If ActiveSheet.AutoFilterMode = False Then
Range("A3:AZ3").Select
Selection.AutoFilter
End If
ActiveSheet.Range("$A$3:$AZ$" & lRowC).AutoFilter Field:=sFilterColNumber, Criteria1:= _
sFilterBy, Operator:=xlAnd
Range("G11").Select
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Summary").Select
Range("A12").Activate
Range("B1").FormulaR1C1 = "=COUNTA(R[12]C[-1]:R[" & lRowC & "]C[-1])"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveSheet.Range("$A$13:$A$" & lRowC + 10 & "").RemoveDuplicates Columns:=1, Header:=xlNo
Range("B13").Select
'**************** remove errors **********************
If ActiveSheet.AutoFilterMode = True Then
Range("A12:AZ12").Select
Selection.AutoFilter
End If
Application.StatusBar = "Calculations for summary page"
lRowC_Sum = Range("B1").Value + 12
If lRowC_Sum < 13 Then lRowC_Sum = 13
Range("B13").Activate
Range("B13:C" & lRowC & ",E13:M1" & lRowC & "").FormulaR1C1 = _
"=INDEX(Consolidated!R3C1:R" & lRowC & "C73,MATCH(RC1,Consolidated!C1,0),MATCH(R5C,Consolidated!R3C1:R3C53,0))"
'
Range("B13:Z" & lRowC).Value = Range("B13:Z" & lRowC).Value
Range("D13:D" & lRowC).FormulaR1C1 = "=""VS""&LEFT(RC[-3],4)"
Range("d13:d" & lRowC).Value = Range("d13:d" & lRowC).Value
Range("O13:O" & lRowC).FormulaR1C1 = "=COUNTIF(Consolidated!C1,RC1)"
Range("Q13:Q" & lRowC).FormulaR1C1 = "=SUMIF(Consolidated!C1,RC1,Consolidated!C[-4])"
Range("R13:R" & lRowC).FormulaR1C1 = "=SUMIF(Consolidated!C1,RC1,Consolidated!C[-4])"
Range("P13:P" & lRowC).FormulaR1C1 = "=SUM(RC[1]:RC[2])"
Range("S13:S" & lRowC).FormulaR1C1 = "=RC[-1]/RC[-4]"
Range("T13:T" & lRowC).FormulaR1C1 = "=SUMIF(Consolidated!C1,RC1,Consolidated!C[-4])"
Range("U13:U" & lRowC).FormulaR1C1 = "=SUMIF(Consolidated!C1,RC1,Consolidated!C[-3])"
Range("V13:V" & lRowC).FormulaR1C1 = "=RC[-1]/RC[-2]"
Range("W13:W" & lRowC).FormulaR1C1 = "=SUMIF(Consolidated!C1,RC1,Consolidated!C[-6])"
Range("X13:X" & lRowC).FormulaR1C1 = "=SUMIF(Consolidated!C1,RC1,Consolidated!C[-5])"
Range("Y13:Y" & lRowC).FormulaR1C1 = "=RC[-1]/RC[-2]"
Range("O10").FormulaR1C1 = "=SUM(R[3]C:R[" & lRowC_Sum & "]C)"
Range("P10").FormulaR1C1 = "=SUM(R[3]C:R[" & lRowC_Sum & "]C)"
Range("Q10").FormulaR1C1 = "=SUM(R[3]C:R[" & lRowC_Sum & "]C)"
Range("R10").FormulaR1C1 = "=SUM(R[3]C:R[" & lRowC_Sum & "]C)"
Range("S10").FormulaR1C1 = "=SUM(RC[-2]/RC[-4])"
Range("T10").FormulaR1C1 = "=SUM(R[3]C:R[" & lRowC_Sum & "]C)"
Range("U10").FormulaR1C1 = "=SUM(R[3]C:R[" & lRowC_Sum & "]C)"
Range("V10").FormulaR1C1 = "=SUM(RC[-1]/RC[-2])"
Range("W10").FormulaR1C1 = "=SUM(R[3]C:R[" & lRowC_Sum & "]C)"
Range("X10").FormulaR1C1 = "=SUM(R[3]C:R[" & lRowC_Sum & "]C)"
Range("Y10").FormulaR1C1 = "=SUM(RC[-1]/RC[-2])"
Range("X13").Select
Range("B13:DA" & lRowC_Sum).NumberFormat = "#,###;[Red](#,###)"
Range("S13:S" & lRowC_Sum).Style = "Percent"
Range("V13:V" & lRowC_Sum).Style = "Percent"
Range("Y13:Y" & lRowC_Sum).Style = "Percent"
Range("N13:N" & lRowC_Sum).NumberFormat = "0"
Range("K13:K" & lRowC_Sum).NumberFormat = "0"
Application.Calculation = xlCalculationAutomatic
Range("B1").FormulaR1C1 = "=COUNTA(R[12]C[1]:R[" & lRowC & "]C[1])"
lRowC = Range("B1").Value
Range("A12:AZ12").Select
'**************** remove errors **********************
If ActiveSheet.AutoFilterMode = False Then
Range("A12:AZ12").Select
Selection.AutoFilter
End If
On Error Resume Next
ActiveSheet.Range("$A$12:$AZ" & lRowC_Sum).AutoFilter Field:=2, Criteria1:="#N/A"
On Error GoTo 0
Application.Calculation = xlCalculationManual
Range("A12").Select
Do
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then Exit Do
Loop Until ActiveCell.EntireRow.Hidden = False
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
If ActiveSheet.AutoFilterMode = True Then
Selection.AutoFilter
End If
If ActiveSheet.AutoFilterMode = False Then
Selection.AutoFilter
End If
On Error Resume Next
ActiveSheet.Range("$A$12:$AZ$" & lRowC_Sum).AutoFilter Field:=13, Criteria1:="0"
On Error GoTo 0
Do
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then Exit Do
Loop Until ActiveCell.EntireRow.Hidden = False
Range("G2").Select
'**************** errors removed **********************
Application.StatusBar = "Formatting...."
Range("B1").FormulaR1C1 = "=COUNTA(R[12]C[1]:R[" & lRowC & "]C[1])"
lRowC = Range("B1").Value
Application.StatusBar = ""
MsgBox "Summary Reports Created for " & vbCrLf & niceName & " " & sFilterBy
Application.ScreenUpdating = False
End Sub
Aucun commentaire:
Enregistrer un commentaire