Object required using "If Target.Address" outside Worksheet_change

guimenez

New Member
Joined
Jun 17, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hi.
I need to compare a cell with a value in my function but i'm having Object required error.
If i put the only the "IF" code inside the worksheet_change everything works fine.
But i need a function.
Can anyone help me?
Thank you

Here is the example


VBA Code:
Sub PrintSheet()
If Target.Address = "$F$2" Then
    If (Target.Value = "Work") Or (Target.Value = "Home") Then
        With Worksheets("sheet1").PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
        End With
    Else
    End If
End If
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
T
But i need a function
That's not a function.

Target is a range object supplied by the Change event. In your code it has no meaning. Why do you need Target at all if you only want to check F2?
 
Upvote 0
T

That's not a function.

Target is a range object supplied by the Change event. In your code it has no meaning. Why do you need Target at all if you only want to check F2?
Thanks for repying.
I'm not good at programming but i'm trying to setup my workbook.

I've remove the target.address and try the code bellow but i'm having now "Application-defined" or object-defined error"
Do you know what can i do more?
Thank you


I'm making a sub for printing that will compare a cell with a value, if the value is Work or Home it will remove the Footer from the print

VBA Code:
Sub PrintSheet()
If ("$F$2" = "Work") Or ("$F$2" = "Home") Then
        With Worksheets("sheet1").PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
        End With
    Else
    End If
End If
 
Upvote 0
Change this line:
VBA Code:
If ("$F$2" = "Work") Or ("$F$2" = "Home") Then
to this:
VBA Code:
If (Range("F2") = "Work") Or (Range("F2") = "Home") Then
and get rid of this:
VBA Code:
    Else
    End If
as you only have one IF, not two.

So your final code should look like:
VBA Code:
Sub PrintSheet()

If (Range("F2") = "Work") Or (Range("F2") = "Home") Then
        With Worksheets("sheet1").PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
        End With
End If

End Sub
 
Upvote 0
Thank you once again.
Using your code, i still getting "Application-defined" or object-defined error".

My workbook has 2 sheets: Data and PrintView

The button that i use to click to print is inside PrintView sheet.
The F2 cell that i want to compare is inside Data sheet.
And the Sub PrintSheet() is inside Data sheet

Maybe this can help you more.

Thank you.

Change this line:
VBA Code:
If ("$F$2" = "Work") Or ("$F$2" = "Home") Then
to this:
VBA Code:
If (Range("F2") = "Work") Or (Range("F2") = "Home") Then
and get rid of this:
VBA Code:
    Else
    End If
as you only have one IF, not two.

So your final code should look like:
VBA Code:
Sub PrintSheet()

If (Range("F2") = "Work") Or (Range("F2") = "Home") Then
        With Worksheets("sheet1").PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
        End With
End If

End Sub
 
Upvote 0
The button that i use to click to print is inside PrintView sheet.
The F2 cell that i want to compare is inside Data sheet.
And the Sub PrintSheet() is inside Data sheet
OK, a few issues there.

Since you are dealing with multiple sheets, you should use sheet references in your range references, i.e.
Rich (BB code):
Sub PrintSheet()

If (Sheets("Data").Range("F2") = "Work") Or (Sheets("Data").Range("F2") = "Home") Then
        With Worksheets("sheet1").PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
        End With
End If

End Sub

And you should NOT put this code inside a sheet module, especially since multiple sheets are involved.
You should put it in a General (or Standard) Module in your workbook.
See this here for how to insert a General Module: Insert and run VBA macros in Excel - step-by-step guide

By the way, taking a step back here, what EXACTLY are you trying to accomplish with this code?
Are you simply trying to remove all headers and footers from "sheet1"?
It looks like that is all your code is doing.
 
Upvote 0
Solution
OK, a few issues there.

Since you are dealing with multiple sheets, you should use sheet references in your range references, i.e.
Rich (BB code):
Sub PrintSheet()

If (Sheets("Data").Range("F2") = "Work") Or (Sheets("Data").Range("F2") = "Home") Then
        With Worksheets("sheet1").PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
        End With
End If

End Sub

And you should NOT put this code inside a sheet module, especially since multiple sheets are involved.
You should put it in a General (or Standard) Module in your workbook.
See this here for how to insert a General Module: Insert and run VBA macros in Excel - step-by-step guide

By the way, taking a step back here, what EXACTLY are you trying to accomplish with this code?
Are you simply trying to remove all headers and footers from "sheet1"?
It looks like that is all your code is doing.

Man you are a Genius :)
I've put the Sub in to Module1 and change the code like you said and its working flawless.
Now i will add the rest of the code to print.
Thank you so much my friend :)
 
Upvote 0
You are welcome.
Glad we were able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,784
Members
449,049
Latest member
greyangel23

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