Genius99

New Member
Joined
Jan 11, 2014
Messages
3
This is the macro what is the error.. When cell D40 is clicked all the macros are called and they are all the same (except ---- for different sheets)....
When i debug it points to this an error - ""If Target.Address = "$D$40" Then Call Sheet9.Remove_Zeros""....
After i delete it it shows this as an error - ""If Target.Address = "$D$40" Then Call Sheet2.Remove_Zeros""....
What should i do??
I am new to vba n macros ..
Pls HElP ME.....




Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
If Target.Address = "$D$40" Then Call Sheet2.Remove_Zeros
If Target.Address = "$D$40" Then Call Sheet3.Remove_Zeros
If Target.Address = "$D$40" Then Call Sheet6.Remove_Zeros
If Target.Address = "$D$40" Then Call Sheet7.Remove_Zeros
If Target.Address = "$D$40" Then Call Sheet8.Remove_Zeros
If Target.Address = "$D$40" Then Call Sheet9.Remove_Zeros
End Sub





The code in those macros are as follows...--------------
Code:
Sub Remove_Zeros()

Sheets("Details").Activate
Dim LRow As Long, n As Long
 
LRow = Range("B1000000").End(xlUp).Row
    For n = LRow To 1 Step -1
    If Cells(n, 2).Value = 0 Then Cells(n, 2).EntireRow.Delete
Next n


End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I would begin here:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$D$40" Then
        Application.EnableEvents = False
            Call Sheet2.Remove_Zeros
            Call Sheet3.Remove_Zeros
            Call Sheet6.Remove_Zeros
            Call Sheet7.Remove_Zeros
            Call Sheet8.Remove_Zeros
            Call Sheet9.Remove_Zeros
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
which worksheet contains the cell D40 that you refer to?

why do you need to activate sheet "Details" ?
 
Upvote 0
It is not good practice to have the same code repeated multiple times since if you need to change it you're guaranteed to miss some. It would be better for there to be one Remove_Zeros (in a standard code module) where you pass the sheet to act on. e.g.

Code:
Sub Remove_Zeros(shtName As String)
Dim LRow As Long, n As Long, sht As Worksheet
Set sht = Sheets(shtName)
With sht
    LRow = .Range("B" & .Rows.Count).End(xlUp).Row
    For n = LRow To 1 Step -1
        If .Cells(n, 2).Value = 0 Then .Rows(n).Delete
    Next n
End With
End Sub

then use

Code:
Call Remove_Zeros("Sheet2")
 
Upvote 0
Even my code worked well once.. but error popped after after...

now even ur's code isn't working properly.... it's pointing towards to the sheet name as error..
like ------ ""Sheet2.Remove_Zeros""
after i delete that --- the nxt one appears....
 
Upvote 0
@jsotola
D40 cell is in the details page...
and i had copied the code from net so idk y it activates sheet ""Details"".... i just din't delete it...


@Teeroy
i want to call for the sheets when the cell D40 of "details" page is clicked....
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

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