=Sum Formula VBA. Need loop - excel

Spreadsheet
I am trying to insert a new sum formula into column P that sums the corresponding values in column S. This process will repeat over as you will see in the image of my spreadsheet I have attached. Any ideas for creating a loop to do this?
Update: I figured out a loop that will do this here is my code.
Sub COMPLETE()
TotalRow = 0
SumEnd = 0
For cur_row = 3 To 174
If IsEmpty(Cells(cur_row, 19)) = True Then
If cur_row = 3 Then
TotalRow = 3
Counts = -1
Else
Cells(TotalRow, 25) = "=SUM(S" & (SumEnd - Counts) & ":S" & SumEnd & " )"
TotalRow = cur_row
Counts = -1
End If
Else:
Counts = Counts + 1
SumEnd = cur_row
End If
Next cur_row
End Sub

You can use pivot table to resume your data in that way:
Pivot table
Configurations
Is that what you mean? or did you mean a VBA code?
Regards

For the example you gave, put this formula in cell P3 and fill down:
=IF(LEN(S3)=0,SUM(INDIRECT("R"&ROW()+1&"C"&COLUMN(S3)&":R"&ROW()+MATCH(TRUE,INDEX(S4:S$17="",0),0)-1&"C"&COLUMN(S3),FALSE)),"")
Note that if your data extend past row 16, you need to change "S$17" to one cell below the last cell in column S.

Related

If column A equals a certain value, generate a value in column B. Check each cell in column A --> Generate a value in each cell in column B

I have a very simple question that I can't find the answer to.
Here is an example of what I'd like to do:
Sample Data
If a row in column A = 'Sandwiches', I would like column B to display 'It's a Sandwich'
If a row in column A = 'Wraps', I would like column B to display 'It's a Wrap'
etc. etc.
So far, I am able to do this for the first row. I'm having trouble creating a for loop to loop through all the available cells.
Here is what I have so far (was thinking of adding Else If statements for different values later):
Current If Statement
If you prefer the use of VBA code instead a formule as Ozgun Senyuva suggested. You can try this code:
Sub Food_and_thing()
Dim myLine As Double
Dim myFood As String
Set SavedData = Sheets("SavedData")
myLine = 2
With SavedData
Do While .Cells(myLine, 1) <> "" 'Assume that Category is in column A, and Generated Asset Name is in column B
myFood = " an other thing"
If .Cells(myLine, 1) = "Sandwiches" Then
myFood = " a Sandwich"
ElseIf .Cells(myLine, 1) = "Wraps" Then
myFood = " a Wrap"
ElseIf .Cells(myLine, 1) = "Salads" Then
myFood = " a salad"
End If
.Cells(myLine, 2) = "It's" & myFood
myLine = myLine + 1
Loop
End With
End Sub
Akash you do not need to write vba codes for this. You may use a lookup table and apply Vlookup formula as #BigBen said.
Apart from this, this formula can make you happy if you ignore some spelling errors. Enter this in cell B2 and paste along the way down.
="It's " & A2

For next sub returning same value down the colum in VBA?

I'm brand new to VBA and am trying to understand how to add/change my code, so that the value in each cell changes as the code calculates down the 2 columns of data I have. Right now, I have the same value going down 13000 rows. This is my code:
Sub SReturn()
For Index = 1 To 13000
If Range("H3").Cells(Index, 1) = Range("H2").Cells(Index, 1) Then
Range("I3").Cells(Index, 1) = Log(Range("F3") / Range("F2"))
Else: Range("I3").Cells(Index, 1) = " "
End If
Next Index
End Sub

Making a VBA FOR loop using vlookup function in Excel

I would like to use a Vlookup function 47 times to get the value for every data. I call the table I am filling "Table 1". "Table 1 starts from E3. I would like to use the vlookup to find the value for cell E3 and fill it in F3.
I call the table from which I return value by Vlookup "Table2". "Table 2 is located in sheet "CC Name" and has two columns A and B.
I have tried two FOR Loops. One FOR Loop for the Vlookup function to be repeated 47 times. Second FOR Loop for the Name of the vlookup function "ccName" to use the function to fill the value in "Table 1" for 43 times, but I get error every time I implement the Code.
Sub GLcreation()
For n = 3 To 50
For c = 3 To 50
ccName(c) = WorksheetFunction.Vlookup(Range("E" & n), Worksheets("CC Name").Range("A:B"), 2, 0)
Range("F" & n) = ccName(c)
Next c
Next n
End Sub
If you can Show me how to Code the correct for Loop, I appreciate your help.
As soon as there is a non-match the code will fail. It's safer to test for match before making the lookup. Here is a better solution:
Sub GLcreation()
Dim n As Integer
Dim wf As WorksheetFunction
Set wf = WorksheetFunction
For n = 3 To 50
If wf.CountIf(Worksheets("CC Name").Range("A:A"), Range("E" & n).Value) > 0 Then
Range("F" & n).Value = wf.Index(Worksheets("CC Name").Range("B:B"), wf.Match(Range("E" & n).Value, Worksheets("CC Name").Range("A:A"), 0))
End If
Next n
End Sub
By getting rid of c Loop, the correct Code is:
Range("F" & n) = WorksheetFunction.Vlookup(Range("E" & n), Worksheets("CC Name").Range("A:B"), 2, 0)

for each loop in excel macros vba jumping alternate rows

I have the below code in vba. The rows in sheet5 from columns a5 to a20 are:
a5=Sweden
a6=Spain
a7=Russia
a8=Italy
a9=Germany
a10=Finland
a11=Norway
a12=Switzerland
a13=France
a14=Belgium
Set fillcolumnrange = Sheet5.Range("A5:A20")
i = 1
For Each Row In fillcolumnrange.Rows
If Not Sheet5.Range("A" & i + 4) = "" Then
MsgBox Row(i)
End If
i = i + 1
Next Row
But this code is prompting only alternate values ie.
Sweden
Russia
Germany
Norway
France
Can anyone please help me out find the bug in the code
You were looping through the rows in your range and also advancing the variable i within your loop.
You can reference each variable that you are looping through.
Try this
Set fillcolumnrange = Sheet1.Range("A5:A20")
For Each cell In fillcolumnrange.Cells
If Not cell = "" Then
MsgBox cell
End If
Next cell
You've got a mixture of different types of loop.
Either do what Rick says.
Or use i:
Set fillcolumnrange = Sheet5.Range("A5:A20")
For i = 1 To fillcolumnrange.Rows.Count
If Not Sheet5.Range("A" & i + 4) = "" Then
MsgBox Sheet5.Cells(i + 4, 1)
End If
Next i
Or maybe a do-Loop
Set fillcolumnrange = Sheet5.Range("A5:A20")
i = 1
do until i = fillcolumnrange.Rows.Count + 4
If Not Sheet5.Range("A" & i + 4) = "" Then
MsgBox Sheet5.Cells(i + 4, 1)
End If
i=i+1
Loop
(EDIT now tested and seem to run ok)
Building on Rick's answer, here's the short version of the For Each loop:
For Each cell in fillcolumnrange.Cells
If len(cell) <> 0 Then MsgBox cell
Next

How do I get excel to merge cells only when other cells are filled?

I need to merge cells using a formula so that the cells only merge when cells on another tab are filled.
I have 2 tabs with the same amount of columns in each. I want cells a1-d1 to merge in tab 1 when cells a1-d1 in tab 2 are filled and for the value of d1 in tab 2 to be inputted into the newly merged cells in tab 1.
this is what I have:
Excel VBA Methods and Function (Excel Macros) overview
Since you want to change cells i do not believe that you can use a formula (even not a user defined one). Therefore i wrote an excel vba macro for your problem.
FirstRows(): Is the starting point. It loops over 10 rows and calls the other methods
CheckEmptyCellValues(curRow): This method checks for empty cells in tab2 (sheet 2 in excel)
MergeCells(curRow) takes the current row as a number (any integer from 1 to max amount of rows) and merges the cells from column 1 to 4 on Sheet 1 (the first sheet in excel)
Fully working demo tested with 4 columns and 10 rows
Sub FirstRows()
Sheets(1).Select
For curRow = 2 To 11
Merge = CheckEmptyCellValues(curRow)
If Merge = 4 Then
MergeCells (curRow)
cellValue = Sheets(2).Cells(curRow, 4).Value
Sheets(1).Cells(curRow, 1).Value = cellValue
End If
Next
End Sub
Sub MergeCells(curRow)
Sheets(1).Select
Range(Cells(curRow, 1), Cells(curRow, 4)).MergeCells = True
End Sub
Function CheckEmptyCellValues(curRow)
Merge = 0
Sheets(2).Select
For i = 1 To 4
If Len(Cells(curRow, i).Value) > 0 Then
Merge = Merge + 1
End If
Next
CheckEmptyCellValues = Merge
End Function
Below you can see the result. The values from sheet 2 haven been copied to sheet 1 (second image). In Sheet 1 the Cells in a row are merged (in row 2 from Cell A2 up to Cell D2 (A2-D2 is now just one cell) if in the first image (sheet 2) every cell (from column a to column d) in a row had a value.
Bugs in the modified code
There are a few things in the modifiend code that are not possible or could lead to a wrong understanding
Function CheckEmptyCellValues(curColumn)
Merge = 0
Sheets(2).Select
For i = A To d
If Len(Cells(curColumn, 11).Value) > 0 Then
Merge = Merge + 1
End If
Next
CheckEmptyCellValues = Merge
End Function
The line For i = A To d is not possible. If you want to use a loop you have to use numbers: For i = 1 To 4 this would repeat the code between For and Next4 times starting with 1
This line Cells(curColumn, 11).Value is technical correct but misleading. Excel uses the first value after (for the row-index and the second value for the column-index. Both values have to be a number: Cells(4,2).Value returns the Cell value from the 4th. row and the second Column (in the Excel Gui the Cell B4)
Try changing this line For i = A To d to this For i = 1 To 4 and see if that returns the wished result.
Bugs part 2
In your other modification you have some of the same bugs:
The loop For curColumn = A to d needs numbers instead of letters (unless A and d were a variable filled with a number but according to your code sample this is not the case
The line cellValue = Sheets(2).Cells(curColumn, d).Value has the same bug, if d is just the letter d and not something like d = 4 than you can not use it in a loop.
This is the code from your comment:
Sub FirstRows()
Sheets(1).Select
For curColumn = A To d
Merge = CheckEmptyCellValues(curColumn)
If Merge = d Then
MergeCells(curColumn)
cellValue = Sheets(2).Cells(curColumn, d).Value
Sheets(1).Cells(curColumn, d).Value = cellValue
End If
Next
End
Sub Sub MergeCells(curColumn)
Sheets(1).Select
Range(Cells(curColumn, 1), Cells(curColumn, d)).MergeCells = True
End Sub
Be carefull it is not running.

Resources