2016-12-28 6 views
-1

Excel 2016年1月2日、1月16日、1月30日、2月13日などの名前の26枚のワークブック。各シートセルB1には、同じ情報が1月2日、1月16日、1月30日、2月13日にそれぞれ存在する。各シートにはさらに日付情報があります。ワークシートJan 2、B3はその週の最初の日付(1月2日)を持ち、1月3日、1月4日1月5日に終わります。私は今日の情報が入っているワークシートをワークブックに公開したいと思っています。私は、例えば月19日にそれを開いたときに、それが開かれたワークシートは月8Excel 2016現在の日付に基づいて特定のシートを開きます。

Dim ws As Worksheet 
Dim mnth As String, dte As String, mday As String 
mday = Now() - Weekday(Now(), 3) 
mnth = Month(mday) 
dte = Day(mday) tabstr = mnth & " " & dte 
For Each ws In Worksheets 
If ws.Name = tabstr Then ws.Select Exit For 
End If 
Next ws 
+0

だろうS.Oへようこそ!何か試しましたか?その場合は、コードを入力してください。 [ツアー](http://stackoverflow.com/tour)と[質問方法](http://stackoverflow.com/help/how-to-ask)をご覧ください。フレンドリーリマインダー:StackOverflowは "あなたのためのコード"サービスプロバイダーではありません。 [VBA入門](https://blog.udemy.com/excel-macros-tutorial/)または[中級チュートリアル](http://analysistabs.com/excel-vba/codes-examples-macros-how最も便利な基礎 - 上級/)と私の[個人的なお気に入り](http://analystcave.com/excel-vba-tutorial/) – Sgdva

+0

Dim Ws As Worksheet Dim mnth As String、dte As String、mday文字列として MDAY = NOW() - 平日(NOW()、3) mnth =月(MDAY) DTE =日(MDAY)ワークシート内の各WSのために tabstr = mnth& "" &DTE ws.Name = tabstrの場合次に ws.Select Exit For End If 次へ –

+0

私は上記のコードを試しましたが、#1 "2"のように扱っていると思いますし、 "jan 2"と見なす必要があります –

答えて

0
Sub CalendarUpdate() 
    ' Created by Brad Tostenson 12/28/2016 
    ' Credit "Johnny B" 
    ' calendarUpdate Macro 

    ' The following code performs 2 basic operations; 
    ' OPERATION 1: Opens the Calendar Of Time Off workbook associated with this workbook, updates the information in it and closes it. 
' OPERATION 2: Sets the opening page to the current period of time for the Calendar Of Time Off workbook. 

' OPERATION 1 
' _________________________________________________________________________________ 
' Turns off alerts so that the user does not receive any dialog boxes 
    Application.DisplayAlerts = False 
' Saves the TimeOff workbook 
    ActiveWorkbook.Save 
' Opens the Calendar workbook so that it can be updated with the new information 
    Workbooks.Open Filename:="S:\Calendar Of Time Off.xltx" 

' OPERATION 2 
' _________________________________________________________________________________ 
' variable setup 
Dim ws As Worksheet 
Dim mnth As String, dte As String, mday As Date 
' OPERATION 2.1 (Previous week's Monday) 
' Finds todays date, from that it finds the previous week's Monday e.g. (if today is Thursday December 29, mday would = December 19) 
    mday = Now() - Weekday(Now(), 3) - 7 

' OPERATION 2.11 (a portion of this operation is duplicated in operation 2.2) 
' Based on the outcome of "mday" it gets the month information and then makes the three letter version = "mnth" e.g. (if mday = December 19, mnth = Dec) 
    mnth = MonthName(Month(mday), True) 
' Based on the outcome of "mday" it gets the day information and then makes that = "dte" e.g. (if mday = December 19, dte = 19) 
    dte = Day(mday) 
' Uses the information in "mnth" & "dte" to create a value that will match the syntax used in the naming of the tabs e.g. (Dec 19) 
    tabstr = mnth & " " & dte 
' The following code uses the "tabstr" (Dec 19) information and will look for a match in the tab names and then opens the workbook to that tab 
    For Each ws In Worksheets 
     If ws.Name = tabstr Then 
      ws.Select 
      Exit For 
     End If 

    Next 

' OPERATION 2.2 
' Finds todays date, from that it finds the this week's Monday e.g.(if today is Thursday December 29, mday would = December 26) 
    mday = Now() - Weekday(Now(), 3) 
    mnth = MonthName(Month(mday), True) 
    dte = Day(mday) 
    tabstr = mnth & " " & dte 
    For Each ws In Worksheets 
     If ws.Name = tabstr Then 
      ws.Select 
      Exit For 
     End If 
    Next 
' _________________________________________________________________________________ 
' OPERATION 1 (CONTINUED) 
' Saves the Calendar of Time Off Sheet as a template to avoid file open errors 
    ActiveWorkbook.SaveAs Filename:="S:\Calendar Of Time Off", FileFormat:=54 
    ActiveWorkbook.Close 
End Sub 
関連する問題