VBA: Sheet select, when any value in range is found

pat767

New Member
Joined
Aug 27, 2011
Messages
7
Hello all,

I'd need some help with this:

Given that I have 3 tabs (sheets), let's say: Alpha, Bravo and Charlie. I also have a 'data' sheet.

Let's assume that I have a =NOW() formula on Data!A1
I also have a list of 15 specific dates in 'Data', let's say from B1:B15

When running the sub, I want the code to "sniff" Data!A1 to detect today's date and if today's date corresponds to any of the given dates in B1:B15, the select a certain sheet (ex. Alpha) ... else, select sheet "Bravo".

Thanks for any help you can give!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try

Code:
Sub atest()
Dim Found As Range
Set Found = Sheets("Data").Range("B1:B15").Find(what:=Date, LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
    Sheets("Bravo").Select
Else
    Sheets("Alpha").Select
End If
End Sub
 
Upvote 0
Hello again,

I couldn't seem to intergrate this properly, especially after a few mods of my own.

Basically, I am using a macro in another workbook, which calls up the functions as described below.

The problem I am having is that it ends up selecting the "Semaine" sheet, even if the PC's date is changed to one of the dates in B2:B15.

Here is the code...

Code:
Sub Ouverture14()

' All throughout, the N1 value is today's date and O1 value is the day of the week, from 1 to 7

    Workbooks.Open Filename:= _
       "\\corpo.stm.info\services\Centre de controle\Exploitation\JOURNAUX_CCM\MODELES\Ouverture CCM 1-4.xlt" _
       , Editable:=True

Dim Found As Range
Set Found = Sheets("Listes").Range("B2:B15").Find(what:=Sheets("Semaine").Range("N1").Value, LookIn:=xlValues, lookat:=xlWhole)
If Not Found Is Nothing Then
Sheets("Dimanche et fêtes").Select
Application.Run "'Ouverture CCM 1-4.xlt'!dateouvertureLAVAL"
Else
Select Case Range("O1").Value
'(Selects the right sheet according to the day of the week.)
Case 7
Sheets("Samedi").Select
Application.Run "'Ouverture CCM 1-4.xlt'!dateouvertureLAVAL"

Case 1
Sheets("Dimanche et fêtes").Select
Application.Run "'Ouverture CCM 1-4.xlt'!dateouvertureLAVAL"

Case 2, 3, 4, 5, 6
Sheets("Semaine").Select
Application.Run "'Ouverture CCM 1-4.xlt'!dateouvertureLAVAL"
End Select

End If

strPath = "\\corpo.stm.info\services\Centre de controle\Exploitation\JOURNAUX_CCM\"
ActiveWorkbook.SaveAs Filename:=strPath & "Ouverture CCM 1-4 " & Worksheets("Semaine").Range("N1").Value, FileFormat:=xlWorkbook, AccessMode:=xlShared
ActiveWorkbook.Close True
End Sub

I apologize for the poor formatting; I uploaded this post using my iPhone.
 
Upvote 0
The problem is that NOW() returns Date and Time whereas you are looking only for the Date. If you use Date as I did then you should not have that problem.
 
Upvote 0
The problem is that NOW() returns Date and Time whereas you are looking only for the Date. If you use Date as I did then you should not have that problem.

Tried it with "=Date" with no success. It still selects the "Semaine" sheet.
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,704
Members
452,938
Latest member
babeneker

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