Sum of all the iterations of a variable in VBA - excel

I have the following code
Dim i As Integer
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
Range("J3").Value = Cells(i, 5).Value
End If
Next i
I want the value of J3 to represent the sum of all the iterations and not just the last iteration if i. Can it be done?

While there are certainly better methods of adding up cells, for your particular method this should work.
Dim i As long, lTotal as long
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
lTotal = Cells(i, 5).Value + lTotal
End If
Next i
Range("J3").Value = lTotal
Keep a running total of of your loop, then use the running total as your cell's value after you've finished the loop

Change this line
Range("J3").Value = Cells(i, 5).Value
To:
Range("J3").Value = Range("J3").Value + Cells(i, 5).Value

Related

VBA expected end of statement

I am trying to edit my excel table with VBA but an error appears while compiling. It doesnt recognize line 2 and line 10.
Sub IfThenElse()
Dim i As Integer = 23
While Not IsNull(Cells(i, 35).Value)
If Cells(i, 35).Value > 1E+16 Then
Cells(i, 4).Value = Cells(i, 35).Value / 10
Else
Cells(i, 4).Value = Cells(i, 35).Value
i = i + 1
End If
End While
End Sub
You cannot declare a variable and set a value at the same time Dim i As Integer = 23
Row counts are of type Long not Integer, Excel has more rows than Integer can handle.
Dim i As Long
i = 23
While … End While is no valid syntax, you need to use Do While … Loop (see Do...Loop statement).
It is very unlikely that a cell value is Null if you are looking for an empty cell use IsEmpty or check for vbNullString
Do While Not IsEmpty(Cells(i, 35).Value) 'or Do While Not Cells(i, 35).Value = vbNullString
If Cells(i, 35).Value > 1E+16 Then
Cells(i, 4).Value = Cells(i, 35).Value / 10
Else
Cells(i, 4).Value = Cells(i, 35).Value
i = i + 1
End If
Loop
Not sure what exactly you are doing but i = i + 1 might need to come after End If.

For Loop in Worksheet change malfunction

Please I have an issue, everytime a change occcurs on the sheet it affects all the rows instead of the row (i) concerned. Confused. Don't for-loops work for worksheet_change ? Pls help. Thanks.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long
'create a variable for last row of column C, LR
LR = Cells(Rows.Count, "C").End(xlUp).Row
For i = 2 To LR
If Cells(i, 6) = "Yes" And Cells(i, 7).Value = "Full" Then
Target.Value = Cells(i, 3).Value
Cells(i, 9).ClearContents
Cells(i, 10).Value = Cells(i, 8).Value + Cells(i, 9).Value
End If
If Not Intersect(Target, Range("G" & i & ":G" & LR)) Is Nothing And Range("F" & i) = "Yes"
And Target.Value = "Full" Then
Application.EnableEvents = False
Cells(i, 8).Value = Cells(i, 3).Value
Cells(i, 9).ClearContents
Cells(i, 10).Value = Cells(i, 8).Value + Cells(i, 9).Value
Application.EnableEvents = True
End If
If Not Intersect(Target, Range("G" & i & ":G" & LR)) Is Nothing And Range("F" & i) = "Yes" And
Target.Value = "Portion" Then
Application.EnableEvents = False
Cells(i, 8).Value = Cells(i, 3).Value
Cells(i, 10).Value = Cells(i, 8).Value + Cells(i, 9).Value
Application.EnableEvents = True
End If
Next i
End Sub
It seems you need to launch this event for the columns A-E. So, you can start your macro with:
IF Target.Column <= 5 THEN
...
END IF 'at the end of your macro
Like this, when you launch code like Cells(i, 8).Value = ..., Cells(i, 10).Value = ..., ... this macro will be called but it will be stopped immediately.
Apparently you are checking on column, maximum 10, which is in the range of the cells you are changing within your macro. Let's go for another approach:
At the very beginning of your macro, put this line:
Application.EnableEvents = False
At the very end of your macro, put this line:
Application.EnableEvents = True
(and remove the other occurences).
This will make sure you don't call your macro while running it.

VBA code not entering else if and end if loop

I have the below loop written, however I am having issues with the code entering the else if loop and to have the loop until (end if) the ranges equal each other. Essentially I want the loop to add 2 to the cells that contain "Corporates" first and afterwards add 1 to the cells that have a the number 2.
Dim x As Long
Dim V As Variant
V = Cells(Rows.Count, 1).End(xlUp).Row
JV = Range("J" & Rows.Count).End(xlUp).Row
For x = 1 To V
If Cells(x, 2).Value = "Corporates" Then
Cells(x, 7).Value = Cells(x, 7).Value + 1
ElseIf Cells(x, 12).Value < "3%" And Cells(x, 2).Value = "Corporates" Then
Cells(x, 7).Value = Cells(x, 7).Value + 1
End If
Range("J" & JV + 3).Value = Range("J" & JV + 2).Value
Next x
End Sub
Use a nested If instead of ElseIf
I'm guessing that < "3%" should be < 0.03 ("3%" is text, not a number).
If Cells(x, 2).Value = "Corporates" Then
If Cells(x, 12).Value < 0.03 Then
Cells(x, 7).Value = Cells(x, 7).Value + 2
Else
Cells(x, 7).Value = Cells(x, 7).Value + 1
End If
End If

My code does not place the values every row, instead skips rows based on source sheet layout

I have a piece of code that searches through Sheet("Sub Tasks") and if the number in column A is a decimal, it then makes cells in Sheet("PBS") Column D = to the offset of the decimal cell found in column A. However, I would like the code to work by making row 2,3,4,5,6, and so on (+1) in sheet PBS display the info from Sub tasks.
But at the moment, the info is displayed in the same layout as it is in Sheet("Sub Tasks"). What am i missing from the code.
Dim Lastrow3 As Long, r As Long, n As Long, cell As Range, ws As Worksheet, Lastrow1 As Long
Lastrow3 = Sheets("Sub Tasks").Range("B" & Rows.Count).End(xlUp).Row
Lastrow1 = Sheets("PBS ").Range("D" & Rows.Count).End(xlUp).Row
n = 2
With Worksheets("Sub Tasks")
For Each cell In Sheets("Sub Tasks").Range("A2:A" & Lastrow3)
If IsNumeric(cell.value) Then
If cell.value = Int(cell.value) Then
Worksheets("PBS ").Cells(n, "C").value = cell.value
Worksheets("PBS ").Cells(n, "D").value = cell.Offset(0, 1).value
Worksheets("PBS ").Cells(n, "B").value = cell.Offset(0, 8).value
Worksheets("PBS ").Cells(n, "A").value = cell.Offset(0, 7).value
Worksheets("PBS ").Cells(n, "H").value = cell.Offset(0, 23).value
Worksheets("PBS ").Cells(n, "E").value = cell.Offset(0, 3).value
Else
End If
Else
End If
n = n + 1
Next cell
End With
``````````````````````````
move n = n + 1 to another place.
n = 2
With Worksheets("Sheet1")
For Each cell In Sheets("Sheet1").Range("A2:A" & Lastrow3)
If IsNumeric(cell.Value) Then
If cell.Value = Int(cell.Value) Then
Worksheets("Sheet3").Cells(n, "C").Value = cell.Value
Worksheets("Sheet3").Cells(n, "D").Value = cell.Offset(0, 1).Value
Worksheets("Sheet3").Cells(n, "B").Value = cell.Offset(0, 8).Value
Worksheets("Sheet3").Cells(n, "A").Value = cell.Offset(0, 7).Value
Worksheets("Sheet3").Cells(n, "H").Value = cell.Offset(0, 23).Value
Worksheets("Sheet3").Cells(n, "E").Value = cell.Offset(0, 3).Value
n = n + 1
Else
End If
Else
End If
Next cell
End With

Excel: How do I 'gather' values to display in another cell

I have two columns, Column A has a set of a few standard values and column B has all unique values. I'm only just experimenting with more complex ways of compiling data than the beginner level so I'm a bit at a loss.
I need to either have a lookup or create a macro that will list only the values in A (once each) but also display which values in B correspond to those in A
for example
A | B
va1|abc
va1|bcd
Va2|xyz
va3|zab
will show (in a single cell) the following
va1: abc, bcd
va2: xyz
va3: zab
Please help!
Option Explicit
Sub Test()
Dim i As Long, j As Long, k As Long
k = 1
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Application.CountIf(Range("C:C"), Cells(i, 1).Value) = 0 Then
Cells(k, 3).Value = Cells(i, 1).Value
For j = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(j, 1).Value = Cells(k, 3).Value And _
InStr(Cells(k, 4).Value, Cells(j, 2).Value) = 0 Then
If Cells(k, 4).Value = "" Then
Cells(k, 4).Value = Cells(j, 2).Value
Else
Cells(k, 4).Value = Cells(k, 4).Value & ", " & Cells(j, 2).Value
End If
End If
Next j
k = k + 1
End If
Next i
For i = 1 To Cells(Rows.Count, 3).End(xlUp).Row
Cells(i, 3).Value = Cells(i, 3).Value & ": " & Cells(i, 4).Value
Cells(i, 4).ClearContents
Next i
End Sub
Edited for single cell
In case your requirement is to "have the grouped data", and not exactly "have one single string per A", you can do this with a "pivot table" putting A and B in the row labels, like in the following picture:

Resources