If cell value = somthing specific then vba help

ExcelNoob222

Board Regular
Joined
Jun 17, 2020
Messages
77
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the below code that works:

VBA Code:
Sub test()

    If Sheet2.Range("A1") = 1 Then
    Range("B1") = "1"
    ElseIf Sheet2.Range("A1") = 2 Then
    Range("B1") = "2"
    End If
End Sub

However as soon as I change the sheet name:

VBA Code:
Sub test()
    If Helper.Range("A1") = 1 Then
    Range("B1") = "1"
    ElseIf Sheet2.Range("A1") = 2 Then
    Range("B1") = "2"
    End If
End Sub

I get "Run-time error '424': Object required" error. Any ideas?

EDIT: Just as I posted I realized I forgot Sheets("Helper") whooops!!!!!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Glad you figured out out.

One thing to keep in mind. When setting values to "1" or "2", when you use the double-quotes like that, you are setting it to the TEXT value of 1 or 2, not the numeric values 1 or 2.
If you want to set it to numeric values, lose the double-quotes there.

Note that in your original code, if you wanted to set the values to numbers, you could rewrite it a little simpler (only needed one IF) like this:
VBA Code:
Sub test()
    If (Sheet2.Range("A1") = 1) or (Sheet2.Range("A1") = 2) Then
        Range("B1") = Sheet2.Range("A1")
    End If
End Sub
 
Upvote 0
Glad you figured out out.

One thing to keep in mind. When setting values to "1" or "2", when you use the double-quotes like that, you are setting it to the TEXT value of 1 or 2, not the numeric values 1 or 2.
If you want to set it to numeric values, lose the double-quotes there.

Note that in your original code, if you wanted to set the values to numbers, you could rewrite it a little simpler (only needed one IF) like this:
VBA Code:
Sub test()
    If (Sheet2.Range("A1") = 1) or (Sheet2.Range("A1") = 2) Then
        Range("B1") = Sheet2.Range("A1")
    End If
End Sub

Thanks for the tips!
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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