If the cells.value does not contains "*30G*" Then - excel

I'm facing issue with a condition within a VBA code.
If Cells(i, 10).Value = Blank And Cells(i, 26).Value <= 61 _
And Cells(i, 1).Value <> "*30G*" Then
I would like to ignore the row which contains the following string in his cells value (col A). Basically, the Cells(I, 1).Value are ID number. The objective is to send email only for the #ID that does not contains or starting with "30G". Otherwise, the code is running well, I'm stuck with this new constraint.

If the cells.value does not contains ā€œ30Gā€ Then
Use InStr function
Is this what you are trying?
If Len(Trim(Cells(i, 10).Value)) = 0 And _
Cells(i, 26).Value <= 61 And _
InStr(1, Cells(i, 1).Value, "30G", vbTextCompare) = 0 Then
End If

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.

VBA list. Help me please

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

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.

Populate Listbox with "cell.value < Date(now())"

it should be simple to make, but it doesn't work. My Column 6 from data list with dates is in that format "mmm.yyyy".
I become always all rows in my list, but i want only those with date older than today.
Only that part of statement work And ws.Cells(i, 6).Value <> vbNullString
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Set ws = E1G
AbgeListField.Clear
AbgeListField.ColumnCount = 2
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
'If statement to get Cells(i, 6).value and check it with Date(now).
If Format(Cells(i, 6), "mmm.yyyy").Value < Format(Now(), "mmm.yyyy") _
And ws.Cells(i, 6).Value <> vbNullString Then
AbgeListField.AddItem ws.Cells(i, 1).Value
AbgeListField.List(i - 1, 1) = ws.Cells(i, 2).Value
AbgeListField.List(i - 1, 2) = ws.Cells(i, 3).Value
End If
Next i
End Sub
Format vs Value
Maybe I misunderstood but this code should work fine:
If ws.Cells(i, 6).Value < Now() _
And ws.cells(i, 6).value <> vbNullString Then
It couldn't work with format, because format turns a value into a string. Then you would have to use the CDate conversion function to get it back to a date, but what's the use of that when you already have a date.
There are three ways of returning or setting a cell value e.g.
let's use 3rd October 2018 in cell 'A1'
Cells("A1").Value returns '3.10.2018' (Maybe different depending on the system settings)
Cells("A1").Value2 returns '43376'
Cells("A1").Text returns in your example 'Oct 2018'
If you use 'Value' it will always, regardless of your formatting, use the 'Value', not the 'Text'.
If you got lost on the way you can always convert Now() and ws.Cells(i, 6).Value to a double type to make sure numbers are being compared:
If CDbl(ws.Cells(i, 6).Value) < CDbl(Now()) _
And ws.cells(i, 6).Value <> vbNullString Then

For each cell in column range table

I need help fixing my code and adding in the cell ranges.
I am trying to change the values in the cells to being the correct values if they are spelt incorrectly. But the table will be added to so I need to make it a flexible code. The code currently stops at the beginning sub with error code 424. I am fairly new to VBA and am stuck.
Sub Consolidates()
Dim datasheet As Worksheet
Set datasheet = ThisWorkbook.Sheets("sheet1")
lr = datasheet.Cells(Rows.Count, 9).End(xlUp).Row
For x = 2 To lr
If cell.Value = "B" Or "BR" Or " Then
cell.Value = "BR"
ElseIf cell.Value = "CL" Or "CR" _
Then cell.Value = "CR"
ElseIf cell.Value = "" Then
End If
Next x
End Sub
you could use something like follows
Option Explicit
Sub Consolidates()
Dim stringsToSearch As String, stringToSubstitute As String
Dim stringsToSearchArr As Variant, stringToSubstituteArr As Variant
' here define the "table"
stringsToSearch = "B,CL" '<--| type here the strings to be searched for
stringToSubstitute = "BR,CR" '<--| type here the corresponding strings to change searched ones into
stringsToSearchArr = Split(stringsToSearch, ",") '<--| turn "stringsToSearch" into an array
stringToSubstituteArr = Split(stringToSubstitute, ",") '<--| turn "stringToSubstitute" into an array
With ThisWorkbook.Sheets("sheetTest") '<--| change "sheetTest" with your actual sheet name
With .Range("I2:I" & .Cells(.Rows.Count, 9).End(xlUp).Row) '<--| consider all cells in column "I" from row 2 to last non empty one
For i = LBound(stringsToSearchArr) To UBound(stringsToSearchArr) '<--| loop through the "table"
.Replace What:=stringsToSearchArr(i), Replacement:=stringToSubstituteArr(i), LookAt:=xlWhole, MatchCase:=True '<--| find current string and replace it with its corresponding replacement
Next i
End With
End With
End Sub
Cell needs a reference to which cell. Also you can't use the or statement like that. Below a simple way to get it done.
For x = 1 To lr
If Cells(x, 9).Value = "B" Or Cells(x, 9).Value = "BR" Then
Cells(x, 9).Value = "BR"
ElseIf Cells(x, 9).Value = "CL" Or Cells(x, 9).Value = "CR" Then
Cells(x, 9).Value = "CR"
End If
Next x
You should consider a select statement
For x = 1 To lr
Select Case Cells(x, 9).Value
Case "B", "BR"
Cells(x, 9).Value = "BR"
Case "CL", "CR"
Cells(x, 9).Value = "CR"
End Select
Next x
Since it is case sensitive you could add an Lcase which could save you some time
For x = 1 To lr
Select Case LCase(Cells(x, 9).Value)
Case "b", "br"
Cells(x, 9).Value = "BR"
Case "cl", "cr"
Cells(x, 9).Value = "CR"
End Select
Next x

Resources