Creation of a sum Loop from the bottom down - VBA - Excel - excel

Hello Stack overflow community and thanks in advance for all the help you could give me.
I am currently trying to create a loop to go through a column and create a sum of the data that is below that till the next blank line and in the next blank line create a new sum again doing the same process till that we get to two blanks following each other.
The below code does something similar but in the sum is upward and not downward.
Would anybody have a solution? Thanks a million for your help.
Dim cell As Range
Dim offset_rows As Long
Set cell = Range("O2")
Do While Not IsEmpty(cell.Offset(1, 0))
offset_rows = Range(cell, cell.End(xlDown)).Rows.Count - 1
Set cell = cell.End(xlDown).Offset(1, 0)
'cell.FormulaR1C1 = "=SUM(R[-" & offset_rows & "]C:R[-1]C)"
cell.FormulaR1C1 = "=SUM(R[" & offset_rows & "]C:R[1]C)"
Loop

I slightly tweaked the macro and it does now the sum downward.
Thanks again so much for your help, really appreciate it!
Sub SumRowsAbove()
Dim StartRow As Long
Dim SumRow As Long
Dim lRow As Long
Dim LastRow As Long
Dim BlankRowsCtr As Integer
Dim FormulaRow As Integer
' modify to your sheet name
With Sheets("Macroprep")
' find last row with data in column "O"
LastRow = .Cells(.Rows.Count, "O").End(xlUp).Row
StartRow = 3
BlankRowsCtr = 0
For lRow = 2 To LastRow + 1
If Not IsEmpty(.Cells(lRow, "O")) Then
SumRow = lRow
BlankRowsCtr = 0
Else
BlankRowsCtr = BlankRowsCtr + 1
' if 2 or more rows are empty >> exit sub
If BlankRowsCtr >= 2 Then Exit Sub
FormulaRow = StartRow - 1
.Cells(FormulaRow, "O").Formula = "=SUM(O" & StartRow & ":O" & SumRow & ")"
StartRow = lRow + 1
End If
Next lRow
End With
End Sub

Related

Copy a certain range from a row if conditions are met (code is done but i can't find the correct vba syntax)

So i have 7 columns, column 5 (E) is date. I am searching for all rows for the date, and copying them to sheet 2 (from 1) based on my imput. Problem is i need to only copy A,B,C,D,E but leave out the last 2 columns. What is the correct syntax for this?
Atm i have:
Rows(i).Copy Destination:=Sheets(2).Rows(Lastrowa)
but this copies my entire row instead of just the first 5 cells of the row. Full code below
Sub Check_Dtaes()
'And Format
Application.ScreenUpdating = False
On Error GoTo M
Dim i As Long
Dim ans As Date
Dim anss As Date
Dim Lastrow As Long
Dim Lastrowa As Long
ans = InputBox("Start Date Is")
anss = InputBox("End Date Is")
Lastrow = Sheets(1).Cells(Rows.Count, "E").End(xlUp).Row
Lastrowa = Sheets(2).Cells(Rows.Count, "E").End(xlUp).Row + 1
For i = 1 To Lastrow
If Cells(i, "E").Value >= ans And Cells(i, "E").Value <= anss Then
Rows(i).Copy Destination:=Sheets(2).Rows(Lastrowa)
Lastrowa = Lastrowa + 1
End If
Next
Sheets(2).Range("E1:E" & Lastrowa).NumberFormat = "dd/mm/yyyy"
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "You entered a inproper date"
Application.ScreenUpdating = True
End Sub
Thanks in advance
Range("A" & i & ":E" & i) // Credit to BigBen, thanks

Function.Match in a Loop

I am trying to match a value from a cell (grid_2.range "A1") and grid_2.range("B1") with a column P on a sheet named grid_2 ("Grid2") to copy all the row where there value is located. Therefore, I will need to check on my data and copy/paste the entire row to another sheet maned grid. But for some reason my code loops but only find the match and copy and paste once.
Sub new_copyPaste()
Dim targetSh As Worksheet
Dim i As Variant
Dim lastRow As Long
lastRow = grid.Cells(Rows.Count, "C").End(xlUp).Row + 1
For i = 3 To grid_2.Cells(Rows.Count, "P").End(xlUp).Row
Position = WorksheetFunction.Match(grid_2.Range("A1"), Worksheets("Grid2").Columns(16), 0)
If grid_2.Cells(i, 16).Value = grid_2.Range("A1") Then
Worksheets("Grid2").Rows(Position).Copy
grid.Range("A" & lastRow).PasteSpecial
End If
Next i
For i = 3 To grid_2.Cells(Rows.Count, "P").End(xlUp).Row
Position = WorksheetFunction.Match(grid_2.Range("B1"), Worksheets("Grid2").Columns(16), 0)
If grid_2.Cells(i, 16).Value = grid_2.Range("B1") Then
Worksheets("Grid2").Rows(Position).Copy
grid.Range("A" & lastRow).PasteSpecial
End If
Next i
End Sub
Maybe do you know what I am doing wrong?
I thought about using VLookup, but after researching, it seems that function match would be more appropriate.
I am open for suggestions :)
Match only returns the first match and is not needed here:
Sub new_copyPaste()
Dim lastRow As Long
Dim i As Long
For i = 3 To grid_2.Cells(Rows.Count, "P").End(xlUp).Row
If grid_2.Cells(i, 16).Value = grid_2.Range("A1") Then
Worksheets("Grid2").Rows(i).Copy
lastRow = grid.Cells(Rows.Count, "C").End(xlUp).Row + 1
grid.Range("A" & lastRow).PasteSpecial
End If
Next i
For i = 3 To grid_2.Cells(Rows.Count, "P").End(xlUp).Row
If grid_2.Cells(i, 16).Value = grid_2.Range("B1") Then
Worksheets("Grid2").Rows(i).Copy
lastRow = grid.Cells(Rows.Count, "C").End(xlUp).Row + 1
grid.Range("A" & lastRow).PasteSpecial
End If
Next i
End Sub

Copy rows between two values VBA and paste to new sheet loop

I am trying to figure out some code here, I have looked on a few sites now, including here and it almost works but it is most likely my datasheet that is causing the issue.
This: Search for two values and copy everything in between in a loop
and this: I need code to copy between two rows and paste into the another sheet with our giving any values?
Would probably work, however the first value cannot be found. Let me explain.
I have an exported report from a website, it groups the totals with a name (value 1) and then the word totals for: (word 2).
What I need it to do is only copy and paste where value 1 is met , and value 2 will always be "totals for:".
Problem is with this loop is that there are blanks between each group of data, so it finds the first "totals for:" but cannot find my first value because it is between about 20 blank cells. (19 groups of data - with a blank row between each group).
How can i retro fix the above codes so that it keeps going down the rows, regardless of blanks to find the first value, then find the second value. Copy that range to a new sheet, and repeat this with a new value 1?
Sub MoveRows()
Dim rownum As Integer
Dim colnum As Integer
Dim startrow As Integer
Dim endrow As Integer
rownum = 1
colnum = 1
With ActiveWorkbook.Worksheets("Sheet1")
Do
If .Cells(rownum, 1).Value = "LIFEC - Supp Life - Ch" Then
startrow = rownum
End If
rownum = rownum + 1
Loop Until .Cells(rownum, 1).Value = "Totals for:"
endrow = rownum
ActiveWorkbook.Worksheets("Sheet1").Range(startrow & ":" & endrow).Copy
End With
ActiveWorkbook.Worksheets("Sheet2").Paste
End Sub
Sub Star123()
Dim rownum As Long
Dim colnum As Long
Dim startrow As Long
Dim endrow As Long
Dim lastrow As Long
rownum = 1
colnum = 1
lastrow = Worksheets("Sheet1").Range("A65536").End(xlUp).Row
With ActiveWorkbook.Worksheets("Sheet1").Range("A1:A" & lastrow)
For rownum = 1 To lastrow
Do
If .Cells(rownum, 1).Value = "LIFEC - Supp Life - Ch" Then
startrow = rownum
End If
rownum = rownum + 1
If (rownum > lastrow) Then Exit For
Loop Until .Cells(rownum, 1).Value = "Totals for:"
endrow = rownum
rownum = rownum + 1
Worksheets("Sheet1").Range(startrow & ":" & endrow).Copy
Sheets("Sheet2").Select
Range("A1").Select
ActiveSheet.Paste
Next rownum
End With
End Sub
I attached the codes that almost work, but cannot find my first value.
You can use Find method which looks something like:
Dim s As Range, e As Range
With Sheet1 'or this can be any other sheet where you search
Set r = .Range("A:A").Find("Whatever you want found")
If Not r Is Nothing Then
Set e = .Range("A:A").Find("The other end", r)
If Not e Is Nothing Then
.Range(r, e).EntireRow.Copy Sheet2.Range("A1") 'or to whatever sheet
End If
End If
End With
You can then have this in a loop which replaces the strings you want found. HTH.

Find duplicate values within the same row (Excel)

I have a list of addresses and parts of the addresses for some records have been duplicated. e.g. some records contain "London" in both column D and column E.
I want to find and highlight any duplicate values across all columns, but within the same row.
So far I have written the code below, but I want it to work through every column containing values and not just the two columns I have named.
Dim Lastrow As Long
Dim i As Long
Lastrow = Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To Lastrow
If Range("D" & i).Value = Range("E" & i).Value Then
Range("E" & i).Interior.ColorIndex = 6
End If
Next i
I have tried to search for an answer, but I have only been able to find ways of highlighting entire duplicate rows or duplicate values in different columns and rows.
Thank you for taking the time to read this and for any help you can give.
If you really want a VBA Solution this does the trick:
Sub JustCall()
Call DuplicatedInRangeByRow(Range("A1:D5"))
End Sub
Sub DuplicatedInRangeByRow(RangeToLook As Range)
Const ColorHighlight = vbYellow
Dim ItemRange As Range
Dim TotalRows As Long: TotalRows = IIf(RangeToLook.Row > 1, RangeToLook.Rows.Count + RangeToLook.Row - 1, RangeToLook.Rows.Count)
Dim TotalCols As Long: TotalCols = IIf(RangeToLook.Column > 1, RangeToLook.Columns.Count + RangeToLook.Column - 1, RangeToLook.Columns.Count)
Dim CounterCols As Long
Dim CounterRows As Long
Dim StartCol As Long
Dim SheetForRange As Worksheet: Set SheetForRange = RangeToLook.Parent
For CounterRows = RangeToLook.Row To TotalRows
For CounterCols = RangeToLook.Column To TotalCols
StartCol = IIf(StartCol = 0, CounterCols, StartCol)
With SheetForRange
If CStr(.Cells(CounterRows, StartCol).Value) = CStr(.Cells(CounterRows, CounterCols).Value) And StartCol <> CounterCols Then .Cells(CounterRows, StartCol).Interior.Color = ColorHighlight: .Cells(CounterRows, CounterCols).Interior.Color = ColorHighlight
End With
Next CounterCols
StartCol = 0
Application.StatusBar = "Progress: " & CounterRows & " out of " & TotalRows & " Rows analyzed " & Format(CounterRows / TotalRows, "Percent")
Next CounterRows
End Sub
for conditional formatting you would use the following formula:
=COUNTIF($A1:$J1,A1)>1
Where $A1 and A1 refers to the most upper left cell in the range to which the formatting is being applied. And the $J1 is the upper right cell of the range.
Pay close attention to what is absolut and what is relative.

Compare only some characters in a cell to only some characters in another cell

Hi guys I am running a macro in Excel 2003 to match property addresses to their owners addresses so I end up with a report of absentee owners.
So in:
column A column C
10 Smith DR Smithville 10 Smith DVE, Smithfield, 49089 Antartica
This is how some of the raw data has been input but I need for this record and all the other slightly different records to be a match and therefore not selected by the macro
as it searches for absentee owners addresses then populates the selected records to sheet2.
In laymans terms if I could compare say only the first 6 characters in column A to the first 6 characters in column C then I think it would work the way I need it to.
Does anyone know how I can achieve this within my macro shown below
Sub test()
Dim i As Long, lr As Long, r As Long, ws As Worksheet, value As Variant,
val As Variant
Dim sval As Integer, lr2 As Long
Application.ScreenUpdating = False
Set ws = Worksheets("Sheet1")
lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lr
value = Split(Cells(i, 1).value, ", ")
For val = LBound(value) To UBound(value)
sval = InStr(1, Cells(i, 3).value, value(val), 1)
If sval = 0 Then Range("A" & i & ":" & "C" & i).Interior.Color = 65535
Next
Next
For r = 2 To lr
lr2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
If Range("A" & r).Interior.Color = 65535 Then
Rows(r).Copy Destination:=Sheets("Sheet2").Rows(lr2 + 1)
lr2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
End If
Next r
Sheets("Sheet2").Cells.Interior.ColorIndex = 0
Application.ScreenUpdating = True
MsgBox "Done Macro"
End Sub
Hopefully I have pasted the code in the correct format required here.
So any help and guidance would be much appreciated.
You can use the formula LEFT(). This will check the first 6 characters from the cell in column A to the first 6 characters in column C. If there's a match, it will add the value from column A to the next free cell in column A, Sheet2.
Sub First6Characters()
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
LastRowSheet2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow
If Left(Range("A" & i), 6) = Left(Range("C" & i), 6) Then
Sheets("Sheet2").Range("A" & LastRowSheet2).Value = Range("A" & i).Value
LastRowSheet2 = LastRowSheet2 + 1
End If
Next i
End Sub
Source: http://www.techonthenet.com/excel/formulas/left.php

Resources