VBA list. Help me please - excel

I have an Excel list of IPOs made in Europe from years 2000-2020 and a second list of all the loans of years 1999-2020 made in Europe. I need to find which of these IPOs from the first list received a loan from this second list of data (loan received maximum one year before the IPO). How can I do it? I wrote this code for VBA, someone can help me to adjust it please? Is there a simpler way to do it?
code:
Sub
i = 2
Do Until Cells(i, 1).Value = ""
j = 2
Cells(i, 5).Value = 0
Do Until Cells(j, 3).Value = "" Or Cells(i, 5).Value = 1
If Cells(i, 2).Value = Cells(j, 4).Value Then
If Cells(i, 1).Value > Cells(j, 3).Value And Cells(i, 1).Value < Cells(j, 3).Value And Cells(i, 1).Value > Cells(j, 3).Value - 365 Then
Cells(i, 5).Value = 1
End If
j = 1 + i
Loop
i = i + 1
Loop
End Sub

I can achieve your result w/o VBA using this formula
=IF(ISNA(VLOOKUP(B7,$F$4:$G$7,1,FALSE)),0,IF(AND((C7-(VLOOKUP(B7,$F$4:$G$7,2,FALSE))<365),(C7-(VLOOKUP(B7,$F$4:$G$7,2,FALSE))>0)),1,0))
In case you want to maintain the sequence of your column, then you have to use index-match function to replace Vlookup

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.

Visual Basic - for..next and formulas

I need to make Excel VBA script which inserts formula depending on cells values.
My current version returns only FALSE value
For i = 1 To 10 Step 1
If Cells(i, 2).Value > Cells(i, 1).Value Then Cells(i, 3).Value = Cells(i, 3).Formula = "=cells(i,1).value+cells(i,2).value"
If Cells(i, 2).Value < Cells(i, 1).Value Then Cells(i, 3).Value = Cells(i, 3).Formula = "=cells(i,2).value-cells(i,1).value"
Next i
I geuss you would like to do something like that
For i = 1 To 10 Step 1
If Cells(i, 2).Value > Cells(i, 1).Value Then Cells(i, 3).FormulaR1C1 = "=RC[-2]+RC[-1]"
If Cells(i, 2).Value < Cells(i, 1).Value Then Cells(i, 3).FormulaR1C1 = "=RC[-2]-RC[-1]"
Next i
First, it returns FALSE because you compare the formula of the cell with your string. Those are not equal, hence it returns FALSE. This would be solved by leaving out the =Cells(i,3).Formula part.
Second, it is important to be aware that certain VBA functionality cannot be used in your worksheet and vice versa. When you type '=Cells' in a cell you will note there is no Worksheet function called Cells. That's why the following will return '#Field!'
For i = 1 To 10 Step 1
If Cells(i, 2).Value > Cells(i, 1).Value Then
Cells(i, 3).Formula = "=cells(i,1).value+cells(i,2).value"
End if
If Cells(i, 2).Value < Cells(i, 1).Value Then
Cells(i, 3).Formula = "=cells(i,2).value-cells(i,1).value"
End if
Next i
The easiest thing to do is performing the calculation within VBA:
For i = 1 To 10 Step 1
If Cells(i, 2).Value > Cells(i, 1).Value Then
Cells(i, 3).Value = Cells(i, 1).Value + Cells(i, 2).Value
End if
If Cells(i, 2).Value < Cells(i, 1).Value Then
Cells(i, 3).Formula = Cells(i, 2).Value - Cells(i, 1).Value
End if
Next i
Some recommendations:
Instead of addressing a cell value multiple times, assign the value to a variable.
Try avoiding writing formulas to worksheets. This will slow down your code big time. If you must, then consider storing all the formulas in an array first and only write them to the worksheet when the entire array has been filled.
In this case you can avoid using VBA altogether.
Consider checking if the value of the cell is numeric before performing calculations.
If you insist on using a worksheet function, you could use Storax's suggestion.

Excel Auto Sort then Summarize bulk data?

I am want to be able to take "bulk" material lists and have them automatically summarized to "sum" like-with-like items.
For example, would there be an efficient way to accomplish the following?
Is there an efficient way to get from:
To
I currently select all of the data from the first photo and do a SORT in the following order by the following:
1) Species
2) Size (W)
3) Size (H)
4) Length
5) PCS
Then I select the "Sort anything that looks like a number, as a number" option. . .
But after I do that sort, I have to manually calculate the total quantity of "like" items in order to fully summarize my list to end up with how the second photo looks like. . .
Is there something that can automatically summarize the data from Photo 1 to make it look exactly like Photo 2?
Your help will make my life a LOT easier! Thank you for your time!
Try this:
What this does is starting in row 2, checks all the other rows for a match based on Size (W), Size (H), Length and Species. If those all match, then PCS and Order are summed together and one of the rows are deleted.
Sub Consolidate()
Dim i As Long
For i = 8 To Cells(Rows.Count, 1).End(xlUp).Row
For j = Cells(Rows.Count, 1).End(xlUp).Row To 8 Step -1
If i <> j Then
If Cells(i, 6).Value = Cells(j, 6).Value And _
Cells(i, 8).Value = Cells(j, 8).Value And _
Cells(i, 9).Value = Cells(j, 9).Value And _
Cells(i, 10).Value = Cells(j, 10).Value And _
Cells(i, 11).Value = Cells(j, 11).Value Then
Cells(i, 2).Value = Cells(i, 2).Value + Cells(j, 2).Value
Rows(j).Delete
End If
End If
Next j
Next i
End Sub

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:

Sum of all the iterations of a variable in VBA

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

Resources