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

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

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.

How to insert data rows with Select Case

My VBA script errors when I try to insert data from Sheet1 into Sheet2. Script code delivers only "Case 2-3" ROW numbers, first "Case" does not input into Sheet 2. Wondering what else should be included in VBA script to finalize processes?
My VBA Script:
Sub CopyFromSheet1()
Dim i As Long
For i = 1 To Sheet1.Cells(Sheet1.Rows.Count, 6).End(xlUp).Row ' Last Cell of Column F
Select Case CStr(Sheet1.Cells(i, 3).Value) ' Looks at the Value in Column C
Case "Due From"
Sheet2.Cells(22, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL1"
Sheet2.Cells(23, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL2"
Sheet2.Cells(24, 3).Value = Sheet1.Cells(i, 6).Value
End Select
Next i
End Sub
What about this: Do you everything your doing, but just for Column C. And then Do it all again for column D?
Sub CopyFromSheet1()
Dim i As Long
Dim col as Long
for col = 3 to 4
For i = 1 To Sheet1.Cells(Sheet1.Rows.Count, 6).End(xlUp).Row ' Last Cell of Column F
Select Case CStr(Sheet1.Cells(i, col).Value)
Case "Due From"
Sheet2.Cells(22, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL1"
Sheet2.Cells(23, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL2"
Sheet2.Cells(24, 3).Value = Sheet1.Cells(i, 6).Value
End Select
Next i
next col
End Sub

Copy rows with specific columns based on entered date to other sheet

I have an existing VBA code that copies rows if an identifier column is marked with 'X'. Now I want it to be based off a date range entered by the user. Can somebody please help me convert the existing code to my required one? Thanks!
Sub CopyRow()
Application.ScreenUpdating = False
Dim x As Long, MaxRowList As Long, MaxRowList2 As Long, S As String, wsSource As Worksheet, wsTarget As Worksheet, S2 As Long
Set wsSource = ThisWorkbook.Worksheets("Sheet 1 - RAW")
Set wsTarget = ThisWorkbook.Worksheets("Staging")
iCol = 1
MaxRowList = wsSource.Cells(Rows.Count, iCol).End(xlUp).Row
MaxRowList2 = wsTarget.Cells(Rows.Count, iCol).End(xlUp).Row
S2 = 8
wsTarget.Range("A8:H22").ClearContents
For x = 4 To MaxRowList
If InStr(1, wsSource.Cells(x, 19), "X") Then
wsTarget.Cells(S2, 1).Value = wsSource.Cells(x, 1).Value
wsTarget.Cells(S2, 4).Value = wsSource.Cells(x, 2).Value
wsTarget.Cells(S2, 5).Value = wsSource.Cells(x, 10).Value
wsTarget.Cells(S2, 6).Value = wsSource.Cells(x, 16).Value
wsTarget.Cells(S2, 7).Value = wsSource.Cells(x, 18).Value
wsTarget.Cells(S2, 8).Value = wsSource.Cells(x, 17).Value
S2 = S2 + 1
End If
Next
Application.ScreenUpdating = True
End Sub
You need to modify your if Statement.
If InStr(1, wsSource.Cells(x, 19), "X") Then
Will become
If wsSource.Cells(x, ColumnThatContainsTheDate).value > OlderDate and wsSource.Cells(x, 19).value < NewerDate Then
Now, your problem will become how do you want the user to select the dates? A form that he could dynamically select (using a calendar), just a plain input box or based on a value of a cell? Do you want everything that is older or newer to this date or do you want between two dates? Just adjust the statement to whatever suits your needs.

Highlight rows based pf column criteria VBA

Im trying to write a VBA script to compare two = rows and have the spreadsheet highlight the duplicate rows only if certain criteria is met, such as (Value of row, column a = Value of row-1, column) AND Value of row, column b > Value of row-1, column b) Then entirerow of the greater value in column b.font.color = vbRed.
Here is a section of the table I'm running...
Table Selection
Here is the code I am using...
Sub RemoveDuplicates()
Dim i As Long, R As Long
'Dim DeviceName As Range, SerialNumber As Range, LastContact As Range
Application.ScreenUpdating = False
R = Cells(Rows.Count, 1).End(xlUp).Row
'Set DeviceName = Columns(2)
'Set SerialNumber = Columns(3)
'Set LastContact = Columns(7)
For i = R To 2 Step -1
'If Cells(i, "F").Value > Cells(i - 1, "F").Value Then
'Code above doesn't work
If Cells(i, 3).Value = Cells(i - 1, 3).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value Then
'If Cells(i, 3).Value = Cells(i - 1, 3).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value And Cells(i, 5).Value > Cells(i - 1, 5).Value Then
'Code above doesn't work
Cells(i, 1).EntireRow.Font.Color = vbRed
End If
Next i
Application.ScreenUpdating = True
End Sub
I can get the duplicates to highlight, but when I try to introduce the greater than check, the system gets janky.
try a conditional formatting rule.
With worksheets("sheet1").usedrange.offset(1, 0).entirerow
.FormatConditions.Delete
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=and($a2=$a1, $b2=$b1, $f2>$f1)")
.font.Color = vbRed
End With
End With

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