I need the excel formula to add the cell values until blank cell.
I have tried with the below formula :
=IF(A4="",SUM(A4:INDEX(A$1:$A4,MATCH(TRUE,(A$1:$A4=""),A4))),A4)
But it shows the wrong result.
Please see the picture below to understand, what kind of result I am searching for.
With blocks of data in column A, in B2 enter:
=IF(A2="",SUM($A$1:A2)-SUM($B$1:B1),"")
and copy downwards:
Each value in column B is the sum of the A-block above it.Using column B allows us to avoid having to figure out where in column A to put a SUM() formula.
If you want the result on the same column, give a try with this macro,
Sub total()
Dim i As Long, temp As Long
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row + 1
If Cells(i, 1) <> "" Then
temp = temp + Cells(i, 1)
Else
Cells(i, 1) = temp
temp = 0
Cells(i, 1).Interior.Color = vbYellow
End If
Next i
End Sub
Related
i'm trying to do a sumif of cells in column K to the right whenever there's a cell value in column J = "subtotal". Right now my code isn't working and i'm not sure if it's because i wrote my sumif statement wrong. My range starts at row 13 and the last row varies. My condition to sum is to sum the cells in column K as long as the cells in column B are the same.
Please see below for my code! Would appreciate any help for this...I'm getting an optional error but i'm not 100% sure why.
enter code here
dim startrow as long
dim groupstartrow as long
groupstartrow = 13
with ws.name("sheet 1")
mergelastrow = .Cells(.rows.Count, 2).End(xlUp).Row
'every time a cell in column J says GL total,
'add a formula next to it in column K that sums cells from i from beginning of the range
'as long as i in column b is equal to the b on the same row
'as the cells being summed is my goal
For i = startrow + 1 To mergelastrow + 1
If Cells(i, 10) = "subtotal" Then
Range.Cells(i, 11) = "=SUM(" & Cells(groupstartrow, 11).Address & ":" & Cells(i - 1, 11).Address & ")"
groupstartrow = i + 1
End If
end with
end sub
i have a list of names(Column A), the numbers in columns B to F are result of a formula. I'm trying to create a FOR LOOP code that will check columns B to F, if all cells in B to F are zero then the code should ignore the current row and skip to the next row; if any of the cells in columns B to F is greater than 0, then the code should get the corresponding name in Column A.
Example: If any of the cells in B2, C2, D2, and E2 is greater than 1, then i should get the name/value of A2. if all cells in B2, C2. D2, and E2 are all zeros, then proceed to check next row and do the same thing.
here's the code i used to try to get the names that has any of the 4 column cell values greater than 1
For i = 2 To LastCalcAnalystRowIndex '//wsCalculations.Cells(Rows.Count, "CP").End(xlUp).Row
'//Get Component from cell in column "BP"
Analyst = wsCalculations.Cells(i, "CP").Value
Component = wsCalculations.Cells(i, "CN").Value
weeknumber = wsCalculations.Range("BR2").Value + 3
If wsCalculations.Cells(i, "B").Value = 0 And wsCalculations.Cells(, "C").Value = 0 _
And wsCalculations.Cells(i, "D").Value = 0 And wsCalculations.Cells(i, "E").Value = 0 _
And wsCalculations.Cells(i, "F").Value = 0 Then
Exit For
Else
wsCalculations.Cells(i, "A").Value = wsCalculations.Cells(i, "CP").Value
End If
Next
using the code above, i tried to get the names which all 4 column values are not equal to zero, but the result i get is just a copy of the original list. i highlighted the rows i want my code to skip. i also included the result i get and the result i want to get.
Below is a sample data. My original data has 54 rows. .
can anyone please tell me what im getting wrong?
There's no real need for VBA.
Note that I have used a Table with structured references. You can change it to a range with normal references if you prefer.
If you have O365, you can use a helper column and a formula.
Add a helper column which SUM's the cells in each row (and you can hide that column if necessary).
eg: G2: =SUM(Table3[#[Column2]:[Column6]])
Then, assuming the data is in a Table named Table3 use the formula:
=INDEX(FILTER(Table3,Table3[sumRow]>0),0,1)
If you have an earlier version of Excel, you can use:
I2: =IFERROR(INDEX(Table3[Column1],AGGREGATE(15,6,1/(Table3[sumRow]>0)*ROW(Table3)-ROW(Table3[#Headers]),ROWS($1:1))),"")
and fill down the length of the table.
Not the solution but could shorten your initial code
Why not create a hidden column* that does an =SUM of the entire row
Then get the value from that
instead of using code to get the value of each 5 cells then adding it up.
edit: changed the 'hidden cell' to 'hidden column' :P
Try
Sub test()
Dim rngDB As Range
Dim rng As Range, rngSum As Range
Dim wsCalculations As Worksheet
Dim vR() As Variant
Dim n As Long
Set wsCalculations = ActiveSheet
With wsCalculations
Set rngDB = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
End With
For Each rng In rngDB
Set rngSum = rng.Offset(, 1).Resize(1, 5)
If WorksheetFunction.Sum(rngSum) > 0 Then
n = n + 1
ReDim Preserve vR(1 To n)
vR(n) = rng
End If
Next rng
With wsCalculations
If n Then
.Range("h2").Resize(n) = WorksheetFunction.Transpose(vR)
End If
End With
End Sub
can anyone please tell me what im getting wrong?
actually your shown code isn't consistent with your wording, so it's impossibile to tell all that's wrong
but for sure that Exit For is a logical error, since it actually gets you out of the loop when it first meets five zeros
so as far as this logical error is concerned, you should change that piece fo code to the following:
With wsCalculations
For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
If WorksheetFunction.CountIf(.Cells(i, 2).Resize(, 5), 0) < 5 Then ' when a row is eligible for math
' do your math
End If
Next
End With
where I used WorksheetFunction.CountIf() function that enables you to deal with different conditions since your wording wasn't clear about this item, too ("greater than 0", "all cells...are zero", "greater than 1")
As shown above picture i need put number on column B that automatic increase if text on column A not same so the the result would be
Can I do that with ms-excel?
Put 1 on B1 and put below on B2 and drag down
=if(a2=a1, b1, b1+1)
Using vba, assuming your working on the 1st sheet, you can use code like this:
Sub uniqueIdx()
Dim idx As Integer: idx = 1
Dim c As Range
With Sheets(1)
For Each c In .Range("A1", .Cells(Rows.Count, 1).End(xlUp)):
.Cells(c.row, 2).Value = idx
If (c.Value <> c.Offset(1, 0).Value) Then idx = idx + 1
Next
End With
End Sub
I would like to create a macro that merges cells in column A with the cell in column B only if there is data in column A. Please help
Do you need something like this?
I assumed that columns A and B are filled and I concatenated values from cells A and B in cell A.
Sub concatenate()
Dim row As Integer
Range("A1").Select
row = 0
Do Until ActiveCell.Offset(row, 0).Value = ""
ActiveCell.Offset(row, 0).Value = ActiveCell.Offset(row, 0).Value + ActiveCell.Offset(row, 1).Value
row = row + 1
Loop
End Sub
Basically I have the following scenareo:
2 columns, with 600 rows of data.
I need to copy the data from column 2 and place it at the end of the content in column1 for the same rows. This would result in column 1 having its original content plus the additional content of column 2.
Any information in how I can do this will be greatly appreciated.
Thanks in advance!
Here's a VBA in a simple form. Just create a macro, add these lines to it. Then select your original column (what you're calling column 1), and run the macro.
a = ActiveCell.Value
b = ActiveCell(1, 2).Value
ActiveCell.Value = a + b
The bracketed cell reference is a relative statement - 1, 2 means "same row, one column to the right" so you can change that if you need. You could make it loop by expanding thusly:
Do
a = ActiveCell.Value
b = ActiveCell(1, 2).Value
ActiveCell.Value = a + b
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then
Exit Do
End If
Loop
That loop will carry on until it finds a blank cell, then it'll stop. So make sure you have a blank cell where you want to stop. You could also add extra characters into the line that combines.. so in the above example it's ActiveCell.Value = a + b, but you could make it ActiveCell.Value = a + " - " + b or anything else that may help.
This should take the values from column 2 and place them sequentially at the bottom of column 1.
Sub test()
Dim rng1 As Range
Dim rng2 As Range
Dim cl As Range
Dim r As Long
Set rng1 = Range("A1", Range("A1048576").End(xlUp))
Set rng2 = Range("B1", Range("B1048576").End(xlUp))
r = rng1.Rows.Count + 1
For Each cl In rng2
Cells(r, 1).Value = cl.Value
r = r + 1
Next
End Sub
Just keep it simple. Here is the code.
Sub copyCol()
Dim lastRow As Long
lastRow = Range("A65000").End(xlUp).Row
Range("B1:B" & lasrow).Copy Range("A" & lastRow).Offset(1, 0)
End Sub
As this question received a number of views (10,000+) I thought it was important to also share another and far simpler solution:
In cell C1 use the formula:
=(A1 & B1)
This will copy the content of cell A1 and B1 into cell C1. Drag the formula to all other rows (row 600 in my case).
Then copy the column and paste using 'values only'.
You will then have cells in column C containing the content of column A and column B in a single cell on a row-to-row basis.