Macro to call macro if cell = 0. Please help. Wife's job depends on it.

Liberteric

New Member
Joined
Sep 29, 2014
Messages
28
I have a macro button named "AddLine" that shifts cells down from E thru (End Right) as long as I start in column E.

I want to test each line and if A = 0, run the macro "AddLine", if not then move down a line and repeat. I tried the following but it only inserts from the first row (which doesn't equal zero).

Row A is a formula. If(Br=Er,1,0). Is my test to see if a=0 returning a fal se because it's an if rather than the actual number 0? Below is my macro.

Sub MyInsertMacro()
' Starts in E1
Range("E1").Select
Application.ScreenUpdating = False

' 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 Step 1
' Check to see if value in column A = 0, If true, run macro AddLine
If Cells(myRow, "A") = 0 Then
Call AddLine

End If
Next myRow

Application.ScreenUpdating = True
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Start at the bottom and work up:
Rich (BB code):
For myRow =  myLastRow to 1 Step -1
'       Check to see if value in column A = 0, If true, run macro AddLine
        If Cells(myRow, "A") = 0 Then
        Call AddLine
            
        End If
    Next myRow
 
Upvote 0
It still does the same thing. Column a contains =IF(B#=E#,1,0). The macro runs the other macro on the first line, which returns a 1 in A as if it's = to 0.
 
Upvote 0
Still runs on row 1 even though A does not equal 0 then stops.

Run-Time error "1004":
Application-define or object-defined error

Debug shows following
Sub AddLine()
'
' Addline Macro
'
'
Range(Selection, Selection.End(xlToRight)).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(-1, -4).Select
Selection.copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.copy
ActiveCell.Offset(, 3).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Font.Bold = False
With Selection
.HorizontalAlignment = xlCenter
End With
ActiveCell.Offset(0, 2).Select
Selection.Font.Bold = False
With Selection
.HorizontalAlignment = xlRight
End With
Selection.copy
ActiveCell.Offset(-1, 0).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(1, 0).Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveSheet.Paste

Application.CutCopyMode = False
End Sub
Sub MyInsertMacro()

Dim myLastRow As Long
Dim myRow As Long
Range("E1").Select
Application.ScreenUpdating = False

' 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 = myLastRow To 1 Step -1

' Check to see if value in column A = 0
If Cells(myRow, "A") = 0 Then
Call AddLine
End If
Next myRow

Application.ScreenUpdating = True

End Sub
 
Upvote 0
It looks like to me "Myrow" needs to be advanced each time the loop proceeds like this:


Code:
Myrow= Myrow+1
 
Upvote 0
I thought the problem was that column A was a formula so I added "If Cells(myRow, "A").Value = 0 Then".
It still sees A as equal to 0 even though it says 1 in A.

Sub MyInsertMacro()

Dim myLastRow As Long
Dim myRow As Long
Range("E1").Select
Application.ScreenUpdating = False

' Find last row with data in column A
myLastRow = Cells(Rows.Count, "A").End(xlUp).Row
' Loop through all rows in column A, starting at row 1
For myRow = myLastRow To 1 Step -1

' Check to see if value in column A = 0
If Cells(myRow, "A").Value = 0 Then
AddLine
End If
Next myRow

Application.ScreenUpdating = True

End Sub

It also ends the macro because of the error in AddLine..
Run-Time error "1004":
Application-define or object-defined error

Debug shows following
Sub AddLine()
'
' Addline Macro
'
'
Range(Selection, Selection.End(xlToRight)).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(-1, -4).Select

 
Upvote 0
Try this and see what the output is in the immediate window:

Code:
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
    Debug.Print Cells(myRow, "A")
    On Error Resume Next
    If Cells(myRow, "A") = 0 Then
         debug.print "found"
    End If
    On Error GoTo 0
Next myRow
End Sub

What if you try value2?

Also, debug.print .value2
 
Last edited:
Upvote 0
Try this and see what the output is in the immediate window:

Code:
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
    Debug.Print Cells(myRow, "A")
    On Error Resume Next
    If Cells(myRow, "A") = 0 Then
         debug.print "found"
    End If
    On Error GoTo 0
Next myRow
End Sub

What if you try value2?

Also, debug.print .value2

see below
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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