Macro does not recognize value of If statement in row A

Liberteric

New Member
Joined
Sep 29, 2014
Messages
28
I have a macro named AddLine that inserts a row. The macro below is supposed to check each row in column A, if A=0, it should run AddLine, if not, it should go down a row then test it again. This is my macro. It treats all values in column A as 0.
Column A has and If to determine whether to put a 0 or 1 in the cell. Could it be a data issue?



Sub MyInsertMacro()
Dim myLastRow As Long
' Find last row with data in column B
myLastRow = Cells(Rows.Count, "B").End(xlUp).Row
' Loop through all rows in column A, starting at row 1
For myRow = 1 To myLastRow
' Check to see if value in column A = 0, If true, run macro AddLine
If Cells(myRow, "A") = 0 Then
AddLine
End If
On Error GoTo 0
Next myRow
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I have a macro named AddLine that inserts a row. The macro below is supposed to check each row in column A, if A=0, it should run AddLine, if not, it should go down a row then test it again. This is my macro. It treats all values in column A as 0.
Column A has and If to determine whether to put a 0 or 1 in the cell. Could it be a data issue?



Sub MyInsertMacro()
Dim myLastRow As Long
' Find last row with data in column B
myLastRow = Cells(Rows.Count, "B").End(xlUp).Row
' Loop through all rows in column A, starting at row 1
For myRow = 1 To myLastRow
' Check to see if value in column A = 0, If true, run macro AddLine
If Cells(myRow, "A") = 0 Then
AddLine
End If
On Error GoTo 0
Next myRow
End Sub

Not tested, but try to replace
If Cells(myRow, "A") = 0 Then
with
If Cells(myRow, 1) = 0 Then
 
Upvote 0
For some reason, my macros can not differentiate between a 1 or a 0 in column A. The If in A is correct. It just compares columns B and E, if = 1 else 0. Then numbers are correct but the macro acts like they're all 0's. I've changed the number to text, number, general. What format or type of data do macros look for? Any ideas would be welcome. Thank you so much for your response.
 
Upvote 0
Not tested, but try to replace
If Cells(myRow, "A") = 0 Then
with
If Cells(myRow, 1) = 0 Then

I tried your suggestion but it still didn't recognize that column A was equal to 1. I'm at a loss. Thank you for any other advice you care to give me.
 
Upvote 0
I tried your suggestion but it still didn't recognize that column A was equal to 1. I'm at a loss. Thank you for any other advice you care to give me.

I'm not a VBA guru but I think you'll have to add:
If Cells(myRow, 1).Value = 0 Then
 
Upvote 0
I made the first change you suggested and it still saw column A as a 0

Sub MyInsertMacro()
Dim myLastRow As Long
' Find last row with data in column B
myLastRow = Cells(Rows.Count, "B").End(xlUp).Row
' Loop through all rows in column A, starting at row 1
For myRow = 1 To myLastRow
' Check to see if value in column A = 0, If true, run macro AddLine
If Cells(myRow, "1") = 0 Then
AddLine
End If
On Error GoTo 0
Next myRow
End Sub

I tried to add the line you suggested but it still tried to run the AddLine macro on row 1, which column A equals 0
 
Upvote 0
Not home to test but does changing
Code:
For myRow = 1 to myLastRow
to
Code:
For myRow = myLastRow to 1 Step -1
help?
 
Upvote 0
By the way
Code:
If Cells(myRow, "1") = 0 Then
should be
Code:
If Cells(myRow, 1) = 0 Then
and
Code:
If Cells(myRow, "A") = 0 Then
is fine as there is no problem using the column letter in the Cells method.
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,977
Members
449,200
Latest member
Jamil ahmed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top