Repeat Command in in excel macro - excel

I want to repeat this command in subsequent rows every 15th time.So the next one will be J348:M348
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("J318").Select
ActiveCell.FormulaR1C1 = "=R303C10-((R3C2-R4C2)/(R5C2/R6C2))"
Range("K318").Select
ActiveCell.FormulaR1C1 = "=R303C11-((R3C3-R4C3)/(R5C2/R6C2))"
Range("L318").Select
ActiveCell.FormulaR1C1 = "=RC[-5]"
Range("M318").Select
ActiveCell.FormulaR1C1 = "=RC[-5]"
Range("J318:M318").Select
Selection.AutoFill Destination:=Range("J318:M332"), Type:=xlFillDefault
Range("J318:M332").Select
ActiveWindow.SmallScroll Down:=0
Range("J332").Select
ActiveWindow.SmallScroll Down:=15
Range("J333:M333").Select
Selection.Copy
Range("J334").Select
ActiveSheet.Paste
Range("J335").Select
Application.CutCopyMode = False
Selection.Copy
Range("J334:M334").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _

For a straight migration of your code to a looped code:
I randomly started at row 5 and used a do while loop. You could just as easily use a for loop. Hopefully this gives you the idea of the looping so you don't have to do it manually.
Public Sub Every15thRow()
Dim i As Integer
Dim iFirst, iSecond, iThird, iFourth, iFifth As Integer
'in Ex: 318, 303, 332, 333, 334
Dim MyStopCriteria As Boolean
MyStopCriteria = False
'... whatever your code does before
'...
i = 5 'start with the 5th row
Do Until MyStopCriteria = True
iFirst = i + 15
iSecond = i
iThird = i + 29
iFourth = i + 30
iFifth = i + 31
Application.CutCopyMode = False
Range("J" & iFirst).Select
ActiveCell.FormulaR1C1 = "=R" & iSecond & "C10-((R3C2-R4C2)/(R5C2/R6C2))"
Range("K" & iFirst).Select
ActiveCell.FormulaR1C1 = "=R" & iSecond & "C11-((R3C3-R4C3)/(R5C2/R6C2))"
Range("L" & iFirst).Select
ActiveCell.FormulaR1C1 = "=RC[-5]"
Range("M" & iFirst).Select
ActiveCell.FormulaR1C1 = "=RC[-5]"
Range("J" & iFirst & ":M" & iFirst).Select
Selection.AutoFill Destination:=Range("J" & iFirst & ":M" & iThird), Type:=xlFillDefault
Range("J" & iFirst & ":M" & iFourth).Select
ActiveWindow.SmallScroll Down:=0
Range("J" & iThird).Select
ActiveWindow.SmallScroll Down:=15
Range("J" & iThird & ":M" & iThird).Select
Selection.Copy
Range("J" & iFourth).Select
ActiveSheet.Paste
Range("J" & iFourth).Select
Application.CutCopyMode = False
Selection.Copy
Range("J" & iFifth & ":M" & iFifth).Select
'...
'... whatever else your repeating code needs to do
'...
i = i + 15 'Add 15 rows
If i > 40 Then MyStopCriteria = True
Loop
'... whatever else your code does after repeating
End Sub

Related

Selecting Multiple Columns with and a Specific Row with For Loop VBA

I'm trying to select multiple columns for i rows depending on a For loop. The idea is to check whether a specific cell meets the criteria. If so, copy the formulas associated with that specific segment to the same row as that observation.
i.e:
for i = 13
If O(i) = segment A, copy and paste formula from $P$1 to P(i)
AND
Copy and paste formulas in T1:CV1 to T(i) : CV (i)
(Please keep in mind there are hidden columns between T and CV, I assume these won't have anything to do with the outcome since they are hidden but wanted to note regardless.)
So far, I've tried using the code : Range("T" & i : "CV" & i).Select . I know this is wrong but just wanted to give an idea. The full code is attached below. Any help is appreciated!
Sub mastersheet()
Dim i As Integer
Sheets("Master").Select
For i = 13 To 400
If Range("O" & i).Value = "A" Then
Range("P1").Select
Selection.Copy
Range("P" & i).Select
ActiveSheet.Paste
Range("T1:CV1").Select
Selection.Copy
Range("T" & i : "CV" & i).Select
ActiveSheet.Paste
ElseIf Range("O" & i).Value = "B" Then
Range("P2").Select
Selection.Copy
Range("P" & i).Select
ActiveSheet.Paste
Range("T2:CV2").Select
Selection.Copy
Range("T" & i : "CV" & i).Select
ActiveSheet.Paste
ElseIf Range("O" & i).Value = "C" Then
Range("P3").Select
Selection.Copy
Range("P" & i).Select
ActiveSheet.Paste
Range("T3:CV3").Select
Selection.Copy
Range("T" & i : "CV" & i).Select
ActiveSheet.Paste
ElseIf Range("O" & i).Value = "D" Then
Range("P4").Select
Selection.Copy
Range("P" & i).Select
ActiveSheet.Paste
Range("T4:CV4").Select
Selection.Copy
Range("T" & i : "CV" & i).Select
ActiveSheet.Paste
ElseIf Range("O" & i).Value = "E" Then
Range("P5").Select
Selection.Copy
Range("P" & i).Select
ActiveSheet.Paste
Range("T5:CV5").Select
Selection.Copy
Range("T" & i : "CV" & i).Select
ActiveSheet.Paste
ElseIf Range("O" & i).Value = "F" Then
Range("P6").Select
Selection.Copy
Range("P" & i).Select
ActiveSheet.Paste
Range("T6:CV6").Select
Selection.Copy
Range("T" & i : "CV" & i).Select
ActiveSheet.Paste
ElseIf Range("O" & i).Value = "G" Then
Range("P7").Select
Selection.Copy
Range("P" & i).Select
ActiveSheet.Paste
Range("T7:CV7").Select
Selection.Copy
Range("T" & i : "CV" & i).Select
ActiveSheet.Paste
ElseIf Range("O" & i).Value = "H" Then
Range("P8").Select
Selection.Copy
Range("P" & i).Select
ActiveSheet.Paste
Range("T8:CV8").Select
Selection.Copy
Range("T" & i : "CV" & i).Select
ActiveSheet.Paste
ElseIf Range("O" & i).Value = "I" Then
Range("P9").Select
Selection.Copy
Range("P" & i).Select
ActiveSheet.Paste
Range("T9:CV9").Select
Selection.Copy
Range("T" & i : "CV" & i).Select
ActiveSheet.Paste
End If
Next i
End Sub
Take a look at Select Case
Sub mastersheet1()
Dim i As Integer, ws As Worksheet, n As Integer
Set ws = Sheets("Master")
With ws
For i = 13 To 400
Select Case .Range("O" & i).Value2
Case "A": n = 1
Case "B": n = 2
Case "C": n = 3
Case "D": n = 4
Case "E": n = 5
Case "F": n = 6
Case "G": n = 7
Case "H": n = 8
Case "I": n = 9
Case Else: n = 0
End Select
If n > 0 Then
.Range("P" & n).Copy .Range("P" & i)
.Range("T" & n & ":CV" & n).Copy .Range("T" & i & ":CV" & i)
End If
Next
End With
End Sub
The problem with the copy/paste method is that it is quite slow and inefficient. I would rather use arrays. Here is an example:
Sub mastersheet()
Dim i As Integer
Dim arr As Variant 'This is for storing the array
Sheets("Master").Select
For i = 13 To 400
If Range("O" & i).Value = "A" Then
'This is faster than copy/pasting
Range("P" & i) = Range("P1")
arr = Range("T1:CV1")
Range("T" & i & ": CV" & i) = arr
End If
Next i
End Sub`
Please, try the next compact code. It does not need any selection:
Sub masterSheet()
Dim sh As Worksheet, i As Long, arr, arrL, arrNo, mtch
Set sh = Sheets("Master")
arrL = Split("A,B,C,D,E,F,G,H,I", ",") 'the array used to match the cell value
arrNo = Array(1, 2, 3, 4, 5, 6, 7, 8, 9) 'the array to return row to be copyed (based on mtch)
arr = sh.Range("O1:O400") 'place the range in an array, for faster iteration
Application.Calculation = xlCalculationManual 'calculate formula result only of the end
For i = 13 To 400
mtch = Application.match(arr(i, 1), arrL, 0) 'match the letter value
If IsNumeric(mtch) Then 'if a match exists:
sh.Range("P" & arrNo(mtch - 1)).Copy Destination:=sh.Range("P" & i) 'use the index from arrNo
sh.Range("T" & arrNo(mtch - 1) & ":CV" & arrNo(mtch - 1)).Copy sh.Range("T" & i) 'use the index from arrNo
End If
Next i
Application.Calculation = xlCalculationAutomatic 'now calculate copied formulas
MsgBox "Ready..."
End Sub

Trying to Copy rows selected with checkboxes to another workbook

I am a bit stuck: I have the below code for a spreadsheet which copies rows, selected with a checkbox, into a second sheet.
I now need to amend this code so that the copied rows are pasted into another workbook on a specific sheet.
I have tried Workbooks("").Worksheets("") and also using the whole C drive path but always get a run-time 9, subscript out of range error. I haven't had any luck in finding a solution online.
Both workbooks are saved on my desktop currently for ease:
Sub CopyRows()
For Each chkbx In ActiveSheet.CheckBoxes
If chkbx.Value = 1 Then
For r = 1 To Rows.Count
If Cells(r, 1).Top = chkbx.Top Then
With Worksheets("Sheet2")
LRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
.Range("A" & LRow & ":R" & LRow) = _
Worksheets("Sheet1").Range("A" & r & ":R" & r).Value
End With
Exit For
End If
Next r
End If
Next
End Sub
This recorded macro takes the data to where it needs to go:
Sub Transfer()
'
' Transfer Macro
'
'
Range("K2").Select
Selection.Copy
Windows("Destination.xls").Activate
Range("E7:E8").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("B2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E9").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("M2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E10").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("G2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E11").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("N2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E12").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("E2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E13").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("A2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E14").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("S2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E15").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("J2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E16").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("I2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E17").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("C2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E20").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Destination.xls").Activate
Range("E21").Select
ActiveSheet.Paste
Windows("WIP - Live.xlsm").Activate
End Sub
Code with error at destination workbook:
Sub CopyRows()
For Each chkbx In ActiveSheet.CheckBoxes
If chkbx.Value = 1 Then
For r = 1 To Rows.Count
If Cells(r, 1).Top = chkbx.Top Then
With Workbooks("Destination").Sheets("Sheet2")
LRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
.Range("A" & LRow & ":R" & LRow) = _
Worksheets("Sheet1").Range("A" & r & ":R" & r).Value
End With
Exit For
End If
Next r
End If
Next
End Sub
Solved: I have managed to get it working with the below code:
Sub CopyRows()
For Each chkbx In ActiveSheet.CheckBoxes
If chkbx.Value = 1 Then
For r = 1 To Rows.Count
If Cells(r, 1).Top = chkbx.Top Then
With Workbooks("Destination.xlsm").Sheets("Details")
LRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
.Range("A" & LRow & ":U" & LRow) = _
Worksheets("Sheet2").Range("A" & r & ":U" & r).Value
End With
Exit For
End If
Next r
End If
Next
End Sub
The error was being caused by the Sheet 2 name in the destination workbook. I had to amend the name to details and it started working. Frustratingly simple for how long I spent on it!
Many Thanks to ed2 and norie for the replies and help. It is much appreciated.
Try this:
First:
Change
Worksheets("Sheet1").Range("A" & r & ":R" & r).Value
to
Workbooks("WIP - Live.xlsm").Sheets("Sheet1").Range("A" & r & ":R" & r).Value
Then:
Change
With Worksheets("Sheet2")
to
Workbooks("Destination.xls").Sheets("Sheet2")
This assumes that both workbooks are already open when the macro is run. If not, you will need code to open one or both of them.

Reducing size of a macro

Our company has 36 departments and we use a master budgeting worksheet to develop the budget. The department numbers are not sequential and their budgets are all different. I put together the following macro to send the worksheets to the individual departments. The master is full of VLOOKUPs and other formulae, but the individual departments receive only the final results and a couple of columns for their changes. They can make changes to any number that is not highlighted in yellow. The macro works perfectly for only one department, but when I tried to copy it 35 times below itself so that I could send a worksheet to all departments, I received an error message that said my procedure was too large. I divided it in half and I still received the message!
Sub Macro1()
'
' Macro1 Macro
'' Prepares O&M budget Worksheet for uploading
' Dim sourceSheet as Worksheet
Workbooks.Open Filename:="F:\Rick\2020 Budget\2020 O&M Budget.xlsx"
Set sourcesheet = Worksheets("Dept Detail-O&M Book")
sourcesheet.Activate
' Dim N As Long
' Dim T As Long
' Dim LastRow As Long
lastrow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
Dim i As Long, Total As Long
Dim cell As Range
Application.EnableEvents = False
'
Application.Goto Reference:="Dept_01"
Selection.Copy
Workbooks.Open Filename:="Q:\O&M\Departmental Budgets\Dept 1 MOEC.xlsx"
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
ThisWorkbook.Activate
Application.CutCopyMode = False
Selection.Copy
Windows("Dept 1 MOEC.xlsx").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("R1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Application.CutCopyMode = False
n = Cells(Rows.Count, "R").End(xlUp).Row
Cells(n, "R").Formula = "=SUM(R1:R" & n - 1 & ")"
activecell.Select
Selection.Copy
activecell.Offset(0, 2).Select
ActiveSheet.Paste
Selection.Copy
activecell.Offset(0, 2).Select
ActiveSheet.Paste
Range("X9").Select
activecell.FormulaR1C1 = "=iferror(+RC[-2]/RC[-10],0)"
Range("X9").Select
T = Cells(Rows.Count, "X").End(xlUp).Row
Selection.AutoFill Destination:=Range("x9:x" & T)
With ActiveSheet
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Application.EnableEvents = False
For i = lastrow To 1 Step -1
If Range("B" & i).Value = "1010" Or _
Range("B" & i).Value = "1020" Or _
Range("B" & i).Value = "2172" Or _
Range("B" & i).Value = "2190" Or _
Range("B" & i).Value = "2200" Or _
Range("B" & i).Value = "2290" Or _
Range("B" & i).Value = "4020" Or _
Range("B" & i).Value = "4050" Or _
Range("B" & i).Value = "4060" Or _
Range("B" & i).Value = "4070" Or _
Range("B" & i).Value = "4090" Or _
Range("B" & i).Value = "4100" Or _
Range("B" & i).Value = "4110" Or _
Range("B" & i).Value = "4509" Or _
Range("B" & i).Value = "4510" Or _
Range("B" & i).Value = "4600" Or _
Range("B" & i).Value = "4610" Or _
Range("B" & i).Value = "4700" Or _
Range("B" & i).Value = "5710" Or _
Range("B" & i).Value = "5721" Or _
Range("B" & i).Value = "5723" Or _
Range("B" & i).Value = "5725" Or _
Range("B" & i).Value = "5729" Or _
Range("B" & i).Value = "5730" Or _
Range("B" & i).Value = "5731" Then
.Range("R" & i).Interior.Color = RGB(255, 255, 0)
.Range("T" & i).Interior.Color = RGB(255, 255, 0)
End If
Next i
Application.EnableEvents = True
End With
With ActiveSheet
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Application.EnableEvents = False
For i = lastrow To 1 Step -1
If Range("B" & i).Value = "5721" Or _
Range("B" & i).Value = "9000" Or _
Range("B" & i).Value = "9005" Or _
Range("B" & i).Value = "9010" Or _
Range("B" & i).Value = "9030" Then
.Range("R" & i).Interior.Color = RGB(255, 255, 0)
.Range("T" & i).Interior.Color = RGB(255, 255, 0)
End If
Next i
Application.EnableEvents = True
End With
Range("A1").Select
ActiveWorkbook.Save
ActiveWindow.Close
End Sub
Could someone offer suggestions on how to reduce the size of the macro and/or make it more efficient? Thanks!
I took a shot at cleaning this up (at least to make it run, for now) - I don't know enough about what you're doing to clean up that mid section, though. The problem undoubtedly was that long If statement.
Instead of all the Ors, put all your values in an array then test against that array with IsError:
Option Explicit
Sub Macro1()
Dim valuearr As Variant
Dim cell As Range
Dim sourcesheet As Worksheet
Dim lastrow As Long, i As Long, n As Long
Workbooks.Open Filename:="F:\Rick\2020 Budget\2020 O&M Budget.xlsx"
Set sourcesheet = Worksheets("Dept Detail-O&M Book")
sourcesheet.Activate
lastrow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
Application.EnableEvents = False
'This section needs to be cleaned up...
Application.Goto Reference:="Dept_01"
Selection.Copy
Workbooks.Open Filename:="Q:\O&M\Departmental Budgets\Dept 1 MOEC.xlsx"
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
ThisWorkbook.Activate
Application.CutCopyMode = False
Selection.Copy
Windows("Dept 1 MOEC.xlsx").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("R1").Select
Selection.End(xlDown).Select
Application.CutCopyMode = False
n = Cells(Rows.Count, "R").End(xlUp).Row
Cells(n, "R").Formula = "=SUM(R1:R" & n - 1 & ")"
ActiveCell.Copy
ActiveCell.Offset(0, 2).Paste
Selection.Offset(0, 2).Select
ActiveSheet.Paste
Range("X9").FormulaR1C1 = "=iferror(+RC[-2]/RC[-10],0)"
Range("X9").AutoFill Destination:=Range("x9:x" & Cells(Rows.Count, "X").End(xlUp).Row)
With ActiveSheet
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
valuearr = Array(1010, 1020, 2172, 2190, 2200, 2290, 4020, 4050, 4060, 4070, 4090, 4100, 4110, 4509, 4510, 4600, 4610, 4700, 5710, 5721, 5723, 5725, 5729, 5730, 5731, 9000, 9005, 9010, 9030)
For i = lastrow To 1 Step -1
If IsError(Application.Match(Range("B" & i).Value, valuearr, 0)) Then
.Range("R" & i).Interior.Color = RGB(255, 255, 0)
.Range("T" & i).Interior.Color = RGB(255, 255, 0)
End If
Next i
End With
Application.EnableEvents = True
ActiveWorkbook.Save
ActiveWindow.Close
End Sub

Error 13: Type Mismatch Row Limit Restriction?

When the excel sheet raw data has under 10,000 rows it runs, when it has 10,000 rows and over I get the error. Any idea? The error is pointed to the mu = Cells(joker, 12)
Columns("A:I").Select
Selection.ClearContents
Windows("New Registrations.xls").Activate
ActiveWindow.WindowState = xlNormal
Columns("A:I").Select
Selection.Copy
Windows("Polk Trend Report CYTD.xlsm").Activate
Range("A1").Select
ActiveSheet.Paste
Selection.Interior.ColorIndex = xlNone
Selection.Font.ColorIndex = 0
Sheets("Data").Select
Dim nz As Long
Dim joker As Long
Dim lambda As Long
nz = Cells(4, 12).Value
Dim mu As Long
For joker = 5 To nz + 4
lambda = Cells(joker, 11)
mu = Cells(joker, 12)
If lambda <> 0 And mu - lambda > 1 Then
Range("A" & lambda).Select
Selection.Copy
Range("A" & lambda + 1 & ":A" & mu - 1).Select
ActiveSheet.Paste
Else:
End If
Next joker
Range("N5:O" & nz + 4).Select
Selection.ClearContents
Dim iota As Long
Dim kappa As Long
iota = 7
Do While Cells(iota, 2).Value <> ""
If Cells(iota, 2) = "UNKNOWN" Then
kappa = Application.WorksheetFunction.Match(Cells(iota, 1).Value, Range("J1:J" & nz + 4), 0)
Cells(kappa, 14).Value = Cells(iota, 7).Value
Cells(kappa, 15).Value = Cells(iota, 5).Value
Range("A" & iota & ":I" & iota).Select
Selection.Delete Shift:=xlUp
iota = iota - 1
ElseIf Cells(iota, 2) = "Zone Total" Then
Range("A" & iota & ":I" & iota).Select
Selection.Delete Shift:=xlUp
iota = iota - 1
ElseIf Application.WorksheetFunction.And(Cells(iota, 5) = 0, Cells(iota, 7) = 0) Then
Range("A" & iota & ":I" & iota).Select
Selection.Delete Shift:=xlUp
iota = iota - 1
Else:
End If
iota = iota + 1
Loop
Range("A" & iota & ":I" & iota).Select
Selection.Delete Shift:=xlUp
Range("C5:I5").Select
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Set pvtTable = Worksheets("Total Dealer (Trend)").Range("O5").PivotTable
pvtTable.RefreshTable
Sheets("Total Dealer (Trend)").Select
Cells.Select
Selection.Columns.AutoFit
Sheets("Data").Select
Range("S40:T" & nz + 39).Select
Selection.Copy
Range("A2").Select
Sheets("Total Dealer (Trend)").Select
Range("B40").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Sheets("Data").Select
Range("U40:U" & nz + 39).Select
Selection.Copy
Range("A2").Select
Sheets("Total Dealer (Trend)").Select
Range("E40").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("B40:E" & nz + 39).Select
Selection.Sort Key1:=Range("E40"), Order1:=xlDescending, Header:=xlNo _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A1").Select
ActiveWindow.WindowState = xlMaximized
End Sub
I think you might have a format change taking place on your Worksheet after row 10,000. Say it was a date, now it's General or some other type conflict, and you are getting a data mismatch as a result of the Value of mu being set by
"L10000"
Check the format of the cells below 10,000. Especially Column "L"
this is an example error handler, hopefully you can just copy and paste this into your code as described and it should output the value of the failing cell when the error occurs, and then you can hopefully correct it. The following goes right at the top of your code
On Error GoTo MyProcedure_Error
Then the below goes above the end sub
MyProcedure_Exit:
On Error GoTo 0
Exit Sub
MyProcedure_Error:
Select Case Err.Number
'the "Case 9" statement below is left as an example to show how you could code a
'specific error message if a specifc module needed it
'Case 9
'MsgBox "The input file does not appear to be in the correct format, for importing into the " & _
'" Locations tab" & vbCrLf & "The expected format is " & Str(Import_Cols) & " columns, Pipe Delimited" & _
'vbCrLf, vbCritical, "Error in in procedure TrimColumn of Module DeveloperToolKit"
Case Else
MsgBox "An unexpected error has occured, the call value that has failed is." & _
vbCrLf & Cells(joker, 12) & _
vbCrLf & "Error Code = " & Str$(Err.Number) & _
vbCrLf & "Error Text = " & Err.Description, vbCritical, "Critical Error"
End Select
Resume MyProcedure_Exit

Loop in excel to traverse values in one column

I have following data
ID Balance Balance_Sum Max_Balance
1 1000 2300 1500
1 -200 2300 1500
1 1500 2300 1500
My next column will Calculate some value if(Max_Balance < Balance_sum).
If this calculated value is > 0 then I have to select the next maximum value i.e 1000.
How can I achieve this using formula. Calculations shown above are for 1 ID. I may have thousands of such ID group.
I can not use VBA cause my file size will get too large and it takes time to load.
I guess what you need is a part of a formula that computes a =MAX(...) "from here to the end of the list". This can be done using the =OFFSET(...) function. Let's have our list of values starting from A1, and the relative MAX starting at B1
B1: =MAX(OFFSET(A1;0;0;COUNT(A1:A$1000)))
whereby the last term of COUNT() is so high that it makes sure all rows are included. This works only as long as there are no blank rows in between.
You can copy this formula down and across, row 1000 is the only invariant here.
Sub abc()
j = 2
ActiveSheet.Range("a1").Select
ActiveSheet.Range("a65536").Select
lastrow = Selection.End(xlUp).Row
ActiveSheet.Range("a1").Select
For i = 2 To lastrow
Range("a" & i).Select
If Range("a" & i).Value = "East" Or Range("a" & i).Value = "west" Or Range("a" & i).Value = "north" Or _
Range("a" & i).Value = "south" Then
GoTo cont
Else
Rows(i).Select
Application.CutCopyMode = False
Selection.Cut
Sheets("Sheet2").Select
Range("a" & j).Select
ActiveSheet.Paste
j = j + 1
Sheets("sheet1").Select
Selection.Delete Shift:=xlUp
End If
cont:
Next i
'/////// column b ///////////
ActiveSheet.Range("a1").Select
For i = 2 To lastrow
Range("b" & i).Select
If Range("b" & i).Value >= "01-01-2013" And Range("b" & i).Value <= "30-06-2013" Then
GoTo cont2
Else
Rows(i).Select
Application.CutCopyMode = False
Selection.Cut
Sheets("Sheet2").Select
Range("a" & j).Select
ActiveSheet.Paste
j = j + 1
Sheets("sheet1").Select
Selection.Delete Shift:=xlUp
End If
cont2:
Next i
End Sub
Sub abc()
j = 2
Sheets("sheet1").Select
ActiveSheet.Range("a1").Select
ActiveSheet.Range("a65536").Select
lastrow = Selection.End(xlUp).Row
'/// column a
ActiveSheet.Range("a3:a" & lastrow).Select
Selection.AutoFilter
ActiveSheet.Range("$A$4:$A$" & lastrow).AutoFilter Field:=1, Criteria1:="="
Application.CutCopyMode = False
Selection.EntireRow.Select
' Range(Selection, Selection.End(xlToRight)).Select
rownum = Selection.Row
If rownum = 3 Then
Selection.AutoFilter
GoTo label1
End If
Selection.Copy
Sheets("Sheet2").Select
'lrow = ActiveSheet.Range("A65536").End(xlUp).Row
lrow = Cells.Find(what:="*", after:=[a1], searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
ActiveSheet.Range("a" & lrow + 1).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Application.CutCopyMode = False
Selection.EntireRow.Delete
Selection.AutoFilter
'column b///////////
label1:
ActiveSheet.Range("b65536").Select
lastrow = Selection.End(xlUp).Row
ActiveSheet.Range("b4:b" & lastrow).Select
Selection.AutoFilter
ActiveSheet.Range("$b$1:$b$" & lastrow).AutoFilter Field:=1, Criteria1:="="
Application.CutCopyMode = False
Range(Selection, Selection.End(xlToLeft)).Select
Selection.EntireRow.Select
'Range(Selection, Selection.End(xlToRight)).Select
rownum = Selection.Row
If rownum = 3 Then
Selection.AutoFilter
GoTo label2
End If
Selection.Copy
Sheets("Sheet2").Select
'lrow = ActiveSheet.Range("A65536").End(xlUp).Row
lrow = Cells.Find(what:="*", after:=[a1], searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
ActiveSheet.Range("a" & lrow + 1).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Application.CutCopyMode = False
Selection.EntireRow.Delete
' Selection.AutoFilter
'column c////////////
label2:
ActiveSheet.Range("c65536").Select
lastrow = Selection.End(xlUp).Row
ActiveSheet.Range("c4:c" & lastrow).Select
Selection.AutoFilter
ActiveSheet.Range("$c$1:$c$" & lastrow).AutoFilter Field:=1, Criteria1:="SG Plus", _
Operator:=xlOr, Criteria2:="=Select"
Application.CutCopyMode = False
'Range(Selection, Selection.End(xlToRight)).Select
' Selection.Copy
' Sheets("Sheet2").Select
' lrow = activehseet.Range("A65536").End(xlUp).Row
' ActiveSheet.Range("a" & lrow).Select
' ActiveSheet.Paste
' Sheets("Sheet1").Select
rownum = Selection.Row
If rownum = 3 Then
Selection.AutoFilter
GoTo label3
End If
Range("a4:a" & lastrow).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.EntireRow.Select
Selection.SpecialCells(xlCellTypeVisible).Select
Application.CutCopyMode = False
Selection.EntireRow.Delete
' Selection.AutoFilter
'column c again/////////////
label3:
ActiveSheet.Range("c65536").Select
lastrow = Selection.End(xlUp).Row
ActiveSheet.Range("c4:c" & lastrow).Select
Selection.AutoFilter
ActiveSheet.Range("$c$1:$c$" & lastrow).AutoFilter Field:=1, Criteria1:="="
Application.CutCopyMode = False
rownum = Selection.Row
If rownum = 3 Then
Selection.AutoFilter
GoTo label4
End If
Range(Selection, Selection.End(xlToRight)).Select
Range("a4:a" & lastrow).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.EntireRow.Copy
Sheets("Sheet2").Select
'lrow = ActiveSheet.Range("A65536").End(xlUp).Row
lrow = Cells.Find(what:="*", after:=[a1], searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
ActiveSheet.Range("a" & lrow + 1).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Application.CutCopyMode = False
Selection.EntireRow.Delete
' Selection.AutoFilter
'////////////////////////// over /////////////////////////////
label4:
ActiveSheet.Range("a1").Select
For i = 4 To lastrow
Range("a" & i).Select
If Range("a" & i).Value = "MidAmerica" Or Range("a" & i).Value = "Northeast" Or Range("a" & i).Value = "Southeast" Or _
Range("a" & i).Value = "West" Then
GoTo cont
Else
Rows(i).Select
Application.CutCopyMode = False
Selection.Cut
Sheets("Sheet2").Select
lrow = Cells.Find(what:="*", after:=[a1], searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
Range("a" & lrow + 1).Select
ActiveSheet.Paste
j = j + 1
Sheets("sheet1").Select
Selection.Delete Shift:=xlUp
End If
cont:
Next i
'/////// column b ///////////
ActiveSheet.Range("a1").Select
For i = 4 To lastrow
Range("b" & i).Select
If Range("b" & i).Value = "CA" Or Range("b" & i).Value = "AZ" Then
GoTo cont2
Else
Rows(i).Select
Application.CutCopyMode = False
Selection.Cut
Sheets("Sheet2").Select
lrow = Cells.Find(what:="*", after:=[a1], searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
Range("a" & lrow + 1).Select
ActiveSheet.Paste
j = j + 1
Sheets("sheet1").Select
Selection.Delete Shift:=xlUp
End If
cont2:
Next i
'///////////column c //////////
ActiveSheet.Range("a1").Select
For i = 4 To lastrow
Range("c" & i).Select
If Range("c" & i).Value = "SG" Then
GoTo cont3
Else
Rows(i).Select
Application.CutCopyMode = False
Selection.Cut
Sheets("Sheet2").Select
lrow = Cells.Find(what:="*", after:=[a1], searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
Range("a" & lrow + 1).Select
ActiveSheet.Paste
j = j + 1
Sheets("sheet1").Select
Selection.Delete Shift:=xlUp
End If
cont3:
Next i
'//////////column l/////////////
ActiveSheet.Range("a1").Select
For i = 4 To lastrow
Range("l" & i).Select
If Range("l" & i).Value <= "01/06/2014" And Range("l" & i).Value >= "01/01/2013" Then
GoTo cont4
Else
Rows(i).Select
Application.CutCopyMode = False
Selection.Cut
Sheets("Sheet2").Select
lrow = Cells.Find(what:="*", after:=[a1], searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
Range("a" & lrow + 1).Select
ActiveSheet.Paste
j = j + 1
Sheets("sheet1").Select
Selection.Delete Shift:=xlUp
End If
cont4:
Next i
'//////////column m/////////////
ActiveSheet.Range("a1").Select
For i = 4 To lastrow
Range("m" & i).Select
If Range("m" & i).Value = "12/01" Or Range("m" & i).Value = "12/05" Then
GoTo cont5
Else
Rows(i).Select
Application.CutCopyMode = False
Selection.Cut
Sheets("Sheet2").Select
lrow = Cells.Find(what:="*", after:=[a1], searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
Range("a" & lrow + 1).Select
ActiveSheet.Paste
j = j + 1
Sheets("sheet1").Select
Selection.Delete Shift:=xlUp
End If
cont5:
Next i
'//////////column q and r/////////////
ActiveSheet.Range("a1").Select
For i = 4 To lastrow
Range("q" & i).Select
If Range("q" & i).Value <> " " And Range("r" & i).Value <> " " And Range("u" & i).Value <> " " _
And Range("z" & i).Value <> " " And Range("aa" & i).Value <> " " And Range("ab" & i).Value <> " " _
And Range("b" & i).Value <> " " And Range("j" & i).Value <> " " Then
GoTo cont6
Else
Rows(i).Select
Application.CutCopyMode = False
Selection.Cut
Sheets("Sheet2").Select
lrow = Cells.Find(what:="*", after:=[a1], searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
Range("a" & lrow + 1).Select
ActiveSheet.Paste
j = j + 1
Sheets("sheet1").Select
Selection.Delete Shift:=xlUp
End If
cont6:
Next i
End Sub

Resources