VBA less than operator for cells.value not working correctly [closed] - excel

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
I try to give out a value using usual less operator in excel VBA. So I would like to give out the value 18 every time the value in one cell is less than 8 and greater than 0. Else "Empty" shall be printed out. This shall only work for specific rows where some columns are not empty. Here my code snippet:
ActiveWorkbook.Sheets(1).Columns(22).NumberFormat = "0.00"
ActiveWorkbook.Sheets(1).Columns(23).NumberFormat = "0.00"
For x = 2 To LastRow
If Not IsEmpty(Cells(x, "E")) And Not IsEmpty(Cells(x, "F")) And Not IsEmpty(Cells(x, "G")) And Not IsEmpty(Cells(x, "H")) And Not IsEmpty(Cells(x, "I")) And Not IsEmpty(Cells(x, "J")) Then
If Cells(x, "V").Value <= "8" And Cells(x, "V").Value > "0" Then
Cells(x, "AA").Value = "18"
Else: Cells(x, "AA").Value = "Empty"
End If
End If
Next x
The code only works for cells with value 9, other than that it always gives out 18, even though the number is much higher than 8.
I check also the debugger and it says Value/Double as type and the correct value of the cells. But anyways the macro enters 18.

You are using string values ("8", "0", "18", "Empty") in your comparison and assignment statements instead of numeric values. This is causing unexpected results as string comparison is different from numeric comparison.
Try using numeric values instead of strings. Also, you can simplify your code using the "And" operator to combine the cell empty checks, and use the ElseIf statement to avoid unnecessary comparisons. Here's an updated code snippet:
ActiveWorkbook.Sheets(1).Columns(22).NumberFormat = "0.00"
ActiveWorkbook.Sheets(1).Columns(23).NumberFormat = "0.00"
For x = 2 To LastRow
If Not IsEmpty(Cells(x, "E")) And Not IsEmpty(Cells(x, "F")) And Not IsEmpty(Cells(x, "G")) And Not IsEmpty(Cells(x, "H")) And Not IsEmpty(Cells(x, "I")) And Not IsEmpty(Cells(x, "J")) Then
If Cells(x, "V").Value > 0 And Cells(x, "V").Value < 8 Then
Cells(x, "AA").Value = 18
ElseIf Cells(x, "V").Value >= 8 Then
Cells(x, "AA").Value = "Empty"
End If
End If
Next x

Related

figuring out all the components to a relatively simple looking macro

I'm trying to figure out what the following is actually doing:
Sub ZoneTarget()
'
' Macro1 Macro
'
'
Dim myTarget As Variant
Sheets("Zones").Select
For i = 3 To 30
myTarget = Cells(i, 20).Value
If myTarget = "" Then
Exit For
End If
' Application.CutCopyMode = False
' Cells(i, 18).GoalSeek Goal:=myTarget, ChangingCell:=Cells(i, 8)
If Cells(i, 21).Value < -0.05 / 100 Or Cells(i, 21).Value > 0.05 / 100 Then
Cells(i, 8).Value = Round(Cells(i, 8).Value * (1 + Cells(i, 21).Value), 6)
End If
Next i
End Sub
I've modified the worksheet, and now the macro is no longer working. Once I've figure out what is being done I can hopefully update the right part to make it work again.
From what I can tell the following is supposed to happen:
On the "Zones" tab
In rows 3 to 30
In column 20 (T)
But only until the row where column T is not blank
Where column 21 (U), in said rows is either less than -0.05 or greater than 0.05
Then value in column 8 (H) should be multiplied by one plus value in column 21 (U)
That value should be rounded
I'm not sure what I'm missing here, but when I run the macro it breaks.
Any help would be appreciated.

VBA - advanced macro changing color of cells based on values from data

I'm trying to write a code which changes the color of cells in the specified area based on previously defined values (dates). So if the defined date minus actual date is smaller than zero then cells interior color changes to red.
I'm pulling data from area: From row 2 To 160 And column 24 to 33. I'm checking the difference between these dates and the actual date and if its less than zero I want cells in the region: Row 2 To 160 and column 10 to 19 to change color to red.
I wrote a simple code to only test if it works. But the color is changed to red skipping the condition (some values are grater then zero and either way they are red).
Sub niowy()
Worksheets("External").Activate
For i = 2 To 160
For j = 24 To 33
For k = 10 To 19
If Cells(i, j).Value = "" Then
Cells(i, j).Select
Cells(i, k).Select
ElseIf Cells(i, j).Value - Date > 0 And Cells(i, j).Value - Date < 20 Then
Cells(i, k).Interior.Color = rgbOrange
ElseIf Cells(i, j).Value - Date < 0 Then
Cells(i, k).Interior.Color = rgbRed
End If
Next k
Next j
Next i
End Sub
If you have any idea to speed up a code a little bit or a different approach I would be grateful for any ideas. Take into consideration I'm just starting learning vba so the code might be pretty messy. I also tested "datediff" function but it failed.
Sub niowy()
Dim target As Range
Dim i As Long
Dim j As Long
With Worksheets("Main")
For i = 2 To 160
For j = 24 To 33
If Val(.Cells(i, j)) = 0 Then
Else
Set target = .Range("J" & i & ":t" & i)
Select Case (.Cells(i, j).Value - Date)
Case 1 To 20
target.Interior.Color = rgbOrange
Case Is < 0
target.Interior.Color = rgbRed
End Select
End If
Next j
Next i
End With
End Sub

Excel vba macro to move portion of column down one row when condition is met

I have a working brute-force macro which, when a condition is met (values in two other cells are not equal), moves the cell values of rows 9 to 243 of two particular worksheet columns downward one row at a time (starting at row 243 and working up toward row 9), and then inserts a zero value in row 9 of each of the two columns. I know there must be a much more elegant and efficient way to do this using ranges; unfortunately I have no "range" experience. I apologize in advance for my ignorance, and would be thankful for some help. I promise to do my best to learn from any examples presented. ~ Snorkel
Sub Downshift()
If Worksheets("Proc").Cells(53, 4).value <> Worksheets("XIV").Cells(9, 2).value Then
For x = 244 To 10 Step -1
For y = 115 To 116
Worksheets("XIV").Cells(x, y).value = Worksheets("XIV").Cells(x - 1, y).value
Worksheets("SH").Cells(x, y).value = Worksheets("SH").Cells(x - 1, y).value
Next y
Next x
Fox y = 115 to 116
Worksheets("XIV").Cells(9, y).value = 0
Worksheets("SH").Cells(9, y).value = 0
Next y
Worksheets("Proc").Cells(53, 4).value = Worksheets("XIV").Cells(9, 2).value
End If
End Sub
If I understand you correctly, in effect you are cutting and pasting? I may have not got all the details correct, but this should be along the right lines.
Sub Downshift()
If Worksheets("Proc").Cells(53, 4).Value <> Worksheets("XIV").Cells(9, 2).Value Then
With Worksheets("XIV")
.Range(.Cells(9, 115), .Cells(243, 116)).Cut .Range(.Cells(10, 115), .Cells(244, 116))
.Range(.Cells(9, 115), .Cells(9, 116)).Value = 0
End With
With Worksheets("SH")
.Range(.Cells(9, 115), .Cells(243, 116)).Cut .Range(.Cells(10, 115), .Cells(244, 116))
.Range(.Cells(9, 115), .Cells(9, 116)).Value = 0
End With
Worksheets("Proc").Cells(53, 4).Value = Worksheets("XIV").Cells(9, 2).Value
End If
End Sub

What to do with error 1004 in loop through column?

I'm trying to use this code for showing the overflow to another value in the column I in sheet 20170224-SUAGDB. But I'm getting an Error 1004 about the line If Works....Value Then. Can somebody help me with the error?
Dim i As Long
For i = 2 To Rows.Count
If Worksheets("20170224-SUAGDB").Cells(i, 9).Value <> Worksheets("20170224-SUAGDB").Cells((i + 1), 9).Value Then
Cells(i, 9).Font.Color = vbRed
End If
Next i
Since you haven't .select'ed any specific Range() in your Worksheet, your Rows.Count value is equal to 1048576.
You can check it's value using MsgBox Rows.Count.
So, when your loop goes from 1 to 1048576, in the last iteration when you try to compare .Cells(i, 9).Value with .Cells((i + 1), 9).Value, the second term "explodes" your total number of rows!
Your code looks for the row number 1048577 - which don't exists - so it throws a 1004 error.
One possible solutions is changing :
This: For i = 2 To Rows.Count
To this: For i = 2 To Rows.Count - 1
This solution will loop through all - 1 rows even if they don't contain any data.
Or maybe even:
This: For i = 2 To Rows.Count
To this: For i = 2 To Range("I:I").End(xlDown).Row - 1
This solution get the last non-blank row value of the selected column.
ps. If you don't add - 1 in For i = 2 To Range("I:I").End(xlDown).Row - 1, and your worksheet contains some data at the row number 1048576, your code will throw the same error 1004.
Some of your objects are not fully qualified. For instance, get the last row number in column "I" (9) in `Worksheets("20170224-SUAGDB").
Try the code below:
Dim i As Long
With Worksheets("20170224-SUAGDB")
For i = 2 To .Cells(.Rows.Count, 9).End(xlUp).Row '<-- get last row in column "I" (column 9)
If .Cells(i, 9).Value <> .Cells((i + 1), 9).Value Then
.Cells(i, 9).Font.Color = vbRed
End If
Next i
End With

concatenate cells along 2 columns

I'm trying to concatenate de values of 2 cells in each raw, along 2 columns.
If the concatenated value (Temp) is equal to another one (MaxT) some cells to the right are affected by a change of background colour. All values are strings of text.
MaxT = CStr(MaxT)
For i = 5 To 88
TempT = Sheets("B").Range(Cells(i, 2)).Value & Sheets("B").Range(Cells(i, 5)).Value
If TempT = MaxT Then
Range(Cells(i, 7), Cells(i, 36)).Interior.ColorIndex = 15
End If
Next i
I'm getting this error:
Run-Time error 1004
Application-defined or object-defined error
Where is the problem in the code?
Here is the solution:
MaxT = CStr(MaxT)
For i = 5 To 88
TempT = Sheets("B").Cells(i, 2).Value & Sheets("B").Cells(i, 5).Value
If TempT = MaxT Then
Range(Cells(i, 7), Cells(i, 36)).Interior.ColorIndex = 15
End If
Next i

Resources