Excel-VBA 曜日検出

Bookを複製しての月次更新の場合、日時と曜日にズレが出ます。
この修正を行うVBAです


Attribute VB_Name = "Module13"
Option Explicit

Sub Youbi()
Attribute Youbi.VB_ProcData.VB_Invoke_Func = " \n14"


'
' Youbi Macro
'
'
Dim XlName As String

Dim Mou As String
Dim MouED As String

Dim BookNam As String
Dim MonST_n As Long
Dim MonEN_n As Long
Dim SST_n As Long
Dim SEN_n As Long

Dim Rtn As Long

Dim i, j, k As Long
Dim Sht_nam_c(31) As String
Dim Skd_day_c(31) As String
Dim IN_job_c As String
Dim BS_job_c As String
Dim Num_n As Long


Sht_nam_c(1) = "RM102"
Sht_nam_c(2) = "RM103"
Sht_nam_c(3) = "RM105"
Sht_nam_c(4) = "RM106"
Sht_nam_c(5) = "RM107"
Sht_nam_c(6) = "RM108"
Sht_nam_c(7) = "RM110"
Sht_nam_c(8) = "RM111"
Sht_nam_c(9) = "RM112"
'-----------------------
Sht_nam_c(10) = "RM201"
Sht_nam_c(11) = "RM202"
Sht_nam_c(12) = "RM203"
Sht_nam_c(13) = "RM205"
Sht_nam_c(14) = "RM206"
Sht_nam_c(15) = "RM207"
Sht_nam_c(16) = "RM208"
Sht_nam_c(17) = "RM210"
Sht_nam_c(18) = "RM211"
Sht_nam_c(19) = "RM212"
Sht_nam_c(20) = "RM213"
Sht_nam_c(21) = "RM215"
Sht_nam_c(22) = "RM216"
Sht_nam_c(23) = "RM217"
Sht_nam_c(24) = "RM218"
Sht_nam_c(25) = "RM220"

'---------------------------------------

Skd_day_c(1) = "FRSKD-01"
Skd_day_c(2) = "FRSKD-02"
Skd_day_c(3) = "FRSKD-03"
Skd_day_c(4) = "FRSKD-04"
Skd_day_c(5) = "FRSKD-05"
Skd_day_c(6) = "FRSKD-06"
Skd_day_c(7) = "FRSKD-07"
Skd_day_c(8) = "FRSKD-08"
Skd_day_c(9) = "FRSKD-09"

Skd_day_c(10) = "FRSKD-10"
Skd_day_c(11) = "FRSKD-11"
Skd_day_c(12) = "FRSKD-12"
Skd_day_c(13) = "FRSKD-13"
Skd_day_c(14) = "FRSKD-14"
Skd_day_c(15) = "FRSKD-15"
Skd_day_c(16) = "FRSKD-16"
Skd_day_c(17) = "FRSKD-17"
Skd_day_c(18) = "FRSKD-18"
Skd_day_c(19) = "FRSKD-19"

Skd_day_c(20) = "FRSKD-20"
Skd_day_c(21) = "FRSKD-21"
Skd_day_c(22) = "FRSKD-22"
Skd_day_c(23) = "FRSKD-23"
Skd_day_c(24) = "FRSKD-24"
Skd_day_c(25) = "FRSKD-25"
Skd_day_c(26) = "FRSKD-26"
Skd_day_c(27) = "FRSKD-27"
Skd_day_c(28) = "FRSKD-28"
Skd_day_c(29) = "FRSKD-29"

Skd_day_c(30) = "FRSKD-30"
Skd_day_c(31) = "FRSKD-31"


'----------------------------------------
Application.ScreenUpdating = False
'---------------------------------------



MsgBox ("Book名の日付に従って曜日を設定します")



 '  ファイル名を取得
 XlName = ThisWorkbook.Name
 
 Debug.Print ThisWorkbook.Name
 
 
BookNam = Mid(XlName, 8, 6) ' ファイル名から年・月を切り出す、ファイル名の桁数に注意

Sheets("RM102").Select


Mou = Left(BookNam, 4) & "/" & Right(BookNam, 2) & "/01"  ' 検索用に年月日を作成

Debug.Print Mou

MouED = WorksheetFunction.EoMonth(Mou, 0)                ' 月末日の決定


'Range("A32") = Mou
'Range("A33") = WorksheetFunction.EoMonth(Range("A5").Value, 0)

MonST_n = Weekday(Mou)  ' 1日の曜日を求める

'Debug.Print MonST_n


'MonEN_n = WorksheetFunction.EoMonth(Range("A5").Value, 0)
' MonED = WorksheetFunction.EoMonth(Range("A5").Value, 0)

Range("A2") = Left(Mou, 7)

Mou = Range("A2")

Range("B2") = WeekdayName(MonST_n, True)

 Range("B2").Select
 
    Selection.AutoFill Destination:=Range("B2:AF2"), Type:=xlFillDefault
    
    Range("B2:AF2").Select

  Sheets("RM102").Select
    Range("B2:AF2").Select
    Selection.Copy
    Sheets("RM103").Select
    Range("B2").Select
    ActiveSheet.Paste
    
For i = 2 To 25

   Sheets(Sht_nam_c(i)).Select
   
    
    Range("B2").Select
    ActiveSheet.Paste
    
    Range("A2") = Mou


Next i

For i = 1 To 31

   Sheets(Skd_day_c(i)).Select
   
   Range("A3") = BookNam


Next i



Sheets("RMMS").Select
Range("A1").Select

'
End Sub

わかお かずまさ

VegaSystems


📷📷📷📷📷📷
#LAN_PRO
#Bloguru
#kuma

ワオ!と言っているユーザー

×
  • ブログルメンバーの方は下記のページからログインをお願いいたします。
    ログイン
  • まだブログルのメンバーでない方は下記のページから登録をお願いいたします。
    新規ユーザー登録へ