What am I missing?

wroze27

New Member
Joined
Apr 23, 2021
Messages
31
Office Version
  1. 2019
Platform
  1. Windows
Can someone please explain to me what I did wrong/ what is missing here. I need this code to do a couple things. First search for a worksheet using variable 'Page' which is a cell on sheet1. Each Sheet is named 1-80 and the cell it references may be any one of those numbers.
Next I need it to search in a certain range for another variable. That variable, Sun, can also be found on sheet 1.

I was able to write (Sheets(Page).Select) and it would work. And (ActiveSheet.Range("A1:B5").Find(What:=Sun).Offset(0, 1).Select) would also work.
Why do they not work when combined?

Thanks guys


VBA Code:
Dim Page As String
Page = Sheet1.Range("A4")
Dim Sun As String
Sun = Sheet1.Range("B1")

Sheets(Page).Range("A1:B5").Find(What:=Sun).Offset(0, 1).Select
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You are probably being tripped up by the Find method. Find has several options and it remembers the setting for them from the last time they were set either in previously run VBA code OR as set in the Find dialog box itself. It is best to provide values for those settings when you call Find so you know what Find is trying to find. For example, if the last time you used Find, you set it to look to match the entire cell's content within the searched range, it remembers that so the next time you use Find (either in VBA code or the dialog box), it will look to match what you are searching for in the entire cell's content within the searched range even if you wanted to find the searched for text in part of the cell's content.
 
Upvote 0
Along with Rick's comments, you can only select a cell on the active sheet. Try it like this
VBA Code:
Dim Page As String
Page = Sheet1.Range("A4")
Dim Sun As String
Sun = Sheet1.Range("B1")
Dim Fnd As Range
Set Fnd = Sheets(Page).Range("A1:B5").Find(What:=Sun).Offset(0, 1)
If Not Fnd Is Nothing Then Application.Goto Fnd
 
Upvote 0
Solution

Forum statistics

Threads
1,214,944
Messages
6,122,387
Members
449,080
Latest member
Armadillos

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