Using Left Function as part of a loop - VBA - excel

I have a functioning loop and needed to use the LEFT function as part of the conditions set.
If Cells(i, 4).Value = "Active" And (Cells(i, 16).Value = "" Or Left(Cells(i, 16), 3).Value = "TBD") Then
I hit an error with the above line stating "OBJECT REQUIRED."
This is the full loop:
i = 2
For i = 2 To lastrow
'(Left(cellVal, 4) = "Name")
If Cells(i, 4).Value = "Active" And (Cells(i, 16).Value = "" Or Left(Cells(i, 16), 3).Value = "TBD") Then
Cells(i, 17).Interior.ColorIndex = 12
End If
Next i
Not sure what I'm doing wrong?

Left(Cells(i, 16), 3).Value
should be
Left(Cells(i, 16).Value, 3)

Related

macros can't see a worksheet - 'runtime error 9'

I have 2 sheets and 1 macros that pastes values from one to another. The macros is working. I copied it and changed it a bit. But it can't run -
'run time error 9'
which is visibility issue.
All sheets are in same excel file.
original macros code, it works:
Sub original()
For j = 18 To 28
Worksheets("Express_vnzp").Select
srok = Cells(26, j).Value
stav = Cells(31, j).Value
komis = Cells(28, j).Value
stavka_privlech = Cells(29, j).Value
For i = 10 To 12
PD = Cells(i, 17).Value
Worksheets("Ðàñ÷åòû").Select
Cells(3, 2).Value = stav
Cells(4, 2).Value = srok
Cells(5, 2).Value = komis
Cells(7, 2).Value = stavka_privlech
Cells(15, 2).Value = PD
marzha2 = Cells(23, 2).Value
Worksheets("Express_vnzp").Select
Cells(i, j).Value = marzha2
Next
Next
End Sub
I copied and changed i,j - not working.
Sub erj()
For j = 3 To 4
Worksheets("creditcard").Select
srok = Cells(26, j).Value
stav = Cells(31, j).Value
komis = Cells(28, j).Value
stavka_privlech = Cells(29, j).Value
For i = 5 To 6
PD = Cells(i, 17).Value
Worksheets("ras").Select
Cells(3, 2).Value = stav
Cells(4, 2).Value = srok
Cells(5, 2).Value = komis
Cells(7, 2).Value = stavka_privlech
Cells(15, 2).Value = PD
marzha2 = Cells(23, 2).Value
Worksheets("creditcard").Select
Cells(i, j).Value = marzha2
Next
Next
End Sub
gives 'runtime error', its visibility issue.

if statments with "AND , OR"

please i need help on this code: it compares different values in different column at the same row level and executes the "then statement". But the code i wrote doesn't real function as i expected.
Sub Z_status()
Dim wsO As Worksheet
Set wsO = Sheets("Sending List")
Dim i As Long
Dim Lastrow As Long
With wsO
Lastrow = Cells(Rows.Count, 5).End(xlUp).Row
'Lastrow_2 = Cells(Rows.Count, 6).End(xlUp).Row
'Lastrow_3 = Cells(Rows.Count, 3).End(xlUp).Row
'Lastrow_4 = Cells(Rows.Count, 8).End(xlUp).Row
For i = Lastrow To 2 Step -1
'For j = Lastrow_2 To 2 Step -1
'For k = Lastrow_3 To 2 Step -1
'For l = Lastrow_3 To 2 Step -1
Cells(1, 7).Value = "Expected state"
If (Cells(i, 5).Value = "MTS" Or Cells(i, 5).Value = "MTO") And (Cells(i, 6).Value = "1/1/1900" Or Cells(i, 6).Value > Date) And (Cells(i, 3).Value = 0) And (Cells(i, 8).Value = 0) Then
Cells(i, 7).Value = "Z1"
ElseIf (Cells(i, 5).Value = "MTS" Or Cells(i, 5).Value = "MTO") And (Cells(i, 6).Value = "1/1/1900" Or Cells(i, 6).Value > Date) And (Cells(i, 3).Value = 0) And (Cells(i, 8).Value > 0 Or Cells(i, 8).Value = 0) Then
Cells(i, 7).Value = "Z3"
ElseIf (Cells(i, 5).Value = "MTS" Or Cells(i, 5).Value = "MTO") And (Cells(i, 6).Value = "1/1/1900" Or Cells(i, 6).Value > Date) And (Cells(i, 3).Value > 0) And (Cells(i, 8).Value > 0 Or Cells(i, 8).Value = 0) Then
Cells(i, 7).Value = "Z5"
ElseIf (Cells(i, 5).Value = "Obsolete") And (Cells(i, 6).Value < Date) And (Cells(i, 3).Value > 0) And (Cells(i, 8).Value > 0 Or Cells(i, 8).Value = 0) Then
Cells(i, 7).Value = "Z7"
ElseIf (Cells(i, 5).Value = "Obsolete") And (Cells(i, 6).Value < Date) And (Cells(i, 3).Value = 0) And (Cells(i, 8).Value = 0) Then
Cells(i, 7).Value = "Z9"
End If
Next i
' Next j
' Next k
' Next l
End With
End Sub
1 Your conditions for Z3 and z5 are identical
2 You can write
(Cells(i, 8).Value > 0 Or Cells(i, 8).Value = 0)
as (Cells(i, 8).Value >= 0 )
Your ifs can be written more clearly as
If (Cells(i, 5).Value = "MTS" Or Cells(i, 5).Value = "MTO") And (Cells(i, 6).Value = "1/1/1900" Or Cells(i, 6).Value > Date) Then
If (Cells(i, 8).Value = 0) Then
Cells(i, 7).Value = "Z1"
Else
If (Cells(i, 8).Value > 0) Then
Cells(i, 7).Value = "Z5"
End If
End If
Else
If (Cells(i, 5).Value = "Obsolete") Then
If (Cells(i, 6).Value < Date) And (Cells(i, 8).Value >= 0) Then
If (Cells(i, 3).Value > 0) Then
Cells(i, 7).Value = "Z7"
Else
If (Cells(i, 3).Value = 0) Then
Cells(i, 7).Value = "Z9"
Else
'This case is undefined
End If
End If
Else
'This case is undefined
End If
Else
'this case is undefined
End If
End If
Hopefully you can work out the errors more easily in this form

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

Range wrong number of arguments or invalid property assignment

I'm trying to copy selected cells to another sheet, but I'm always getting error message: Wrong number of arguments or invalid property assignment
This code checks if "Cells(i, 20)" is less or greater than "Cells (i, 4)" by 10%. If it's not, it deletes the row, if it is it should copy selected cells to another sheet starting 48 row.
Maybe someone could point out, what I'm doing wrong here? Here's how my code looks like:
Sub CopyHighLow()
Sheets("ProductionHighLow").Select
i = 2
j = 48
produced = 0
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> ""
produced = Cells(i, 20)
ordered = Cells(i, 4)
If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then
Cells(i, 22).Delete Shift:=xlUp
i = i - 1
Else
Range(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)).Select
Selection.Copy Destination:=Sheets("Rytinis").Range(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5))
j = j + 1
End If
i = i + 1
Wend
End Sub
UPDATE here is working modified version:
Sub CopyHighLow()
Sheets("ProductionHighLow").Select
i = 2
j = 48
produced = 0
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> ""
produced = Cells(i, 20)
ordered = Cells(i, 4)
If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then
Cells(i, 22).Delete Shift:=xlUp
i = i - 1
Else
Set RangeUnionCopy = Union(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20))
Set RangeUnionPaste = Union(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5))
RangeUnionCopy.Copy Destination:=Sheets("Rytinis").Range(RangeUnionPaste.Address)
j = j + 1
End If
i = i + 1
Wend
End Sub
Problem Explanation
Your problem relies in this line
Range(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5))
The Range object cannot handle more than 2 named cells (this way). You may see it directly in the compiler.
More info at its official documentation
Approach solution:
I would use Union prior to this, like so:
Set RangeUnion = Union(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20))
RangeUnion.Copy Destination:=Sheets("Rytinis").Range(RangeUnion.Address)
This should work for what you are aiming for.
Corrected code using Union:
Sub CopyHighLow()
Dim i, j, produced, ordered
Sheets("ProductionHighLow").Select
i = 2
j = 48
produced = 0
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> ""
produced = Cells(i, 20)
ordered = Cells(i, 4)
If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then
Cells(i, 22).Delete Shift:=xlUp
i = i - 1
Else
Union(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)).Select
Selection.Copy Destination:=Sheets("Rytinis").Cells(j, 1)
j = j + 1
End If
i = i + 1
Wend
End Sub
You need to tell it what sheet it copies from.
Sub CopyHighLow()
Sheets("ProductionHighLow").Select
i = 2
j = 48
produced = 0
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> ""
produced = Cells(i, 20)
ordered = Cells(i, 4)
If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then
Cells(i, 22).Delete Shift:=xlUp
i = i - 1
Else
ActiveSheet.Range(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)).Select
Selection.Copy Destination:=Sheets("Rytinis").Range(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5))
j = j + 1
End If
i = i + 1
Wend
End Sub

Enter the workbook name to target sheet in loop

I have written the below script which will loop through a range of data and copy from one sheet and paste to another, which works fine. I’m now looking to add the workbook name to column D on each row the data is pasted yet all attempts fail.
This is what I’m trying, please help.
For i = 6 To LastRow
If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
Range(Cells(i, 1), Cells(i, 4)).Select
Selection.Copy
erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
Worksheets("iForms").Cells(1, 5).Value = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
If Cells(i, 1) <> "" Then Cells(i, 22).Value = "Yes"
If Cells(i, 22) <> "" Then Cells(i, 23).Value = Time
If Cells(i, 23) <> "" Then Cells(i, 24).Value = Environ("UserName")
ActiveWorkbook.Save
End If
Next i

Resources