Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-20-2021, 10:04 AM
ChrisOK ChrisOK is offline Convert NowMonth MonthNow to double digits mm Windows 10 Convert NowMonth MonthNow to double digits mm Office 2019
Advanced Beginner
Convert NowMonth MonthNow to double digits mm
 
Join Date: Sep 2016
Posts: 54
ChrisOK is on a distinguished road
Question Convert NowMonth MonthNow to double digits mm

How do I convert / format the month name of a SHEET / TAB to be double digits rather than single?
It currently produces: "2021-3" but need "2021-03"



Code:
Sub InsertNewWorksheetAndName()

    nowMonth = Month(Now) - 1
    nowYear = Year(Now)

    ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
    ActiveWorkbook.Sheets(Worksheets.Count).Name = nowYear & "-" & nowMonth

End Sub
Reply With Quote
  #2  
Old 04-20-2021, 03:37 PM
macropod's Avatar
macropod macropod is offline Convert NowMonth MonthNow to double digits mm Windows 10 Convert NowMonth MonthNow to double digits mm Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Code:
Sub InsertNewWorksheetAndName()
Application.ScreenUpdating = False
Dim nowMonth As Long, nowYear As Long
nowMonth = Month(Now) - 1: nowYear = Year(Now)
With ActiveWorkbook
  .Sheets.Add After:=Worksheets(.Worksheets.Count)
  .Sheets(.Worksheets.Count).Name = nowYear & "-" & Format(nowMonth, "00")
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-20-2021, 05:00 PM
ChrisOK ChrisOK is offline Convert NowMonth MonthNow to double digits mm Windows 10 Convert NowMonth MonthNow to double digits mm Office 2019
Advanced Beginner
Convert NowMonth MonthNow to double digits mm
 
Join Date: Sep 2016
Posts: 54
ChrisOK is on a distinguished road
Thumbs up

@macropod Thank you greatly! That works!
(of course it does) still in awe
Reply With Quote
  #4  
Old 04-20-2021, 05:21 PM
macropod's Avatar
macropod macropod is offline Convert NowMonth MonthNow to double digits mm Windows 10 Convert NowMonth MonthNow to double digits mm Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Even simpler:
Code:
Sub InsertNewWorksheetAndName()
Application.ScreenUpdating = False
With ActiveWorkbook
  .Sheets.Add After:=Worksheets(.Worksheets.Count)
  .Sheets(.Worksheets.Count).Name = Format(DateAdd("M", -1, Now()), "YYYY-MM")
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-26-2021, 03:47 AM
p45cal's Avatar
p45cal p45cal is online now Convert NowMonth MonthNow to double digits mm Windows 10 Convert NowMonth MonthNow to double digits mm Office 2019
Expert
 
Join Date: Apr 2014
Posts: 863
p45cal has a brilliant futurep45cal has a brilliant futurep45cal has a brilliant futurep45cal has a brilliant futurep45cal has a brilliant futurep45cal has a brilliant futurep45cal has a brilliant futurep45cal has a brilliant futurep45cal has a brilliant futurep45cal has a brilliant futurep45cal has a brilliant future
Default

…and shorter:
Code:
Sub InsertNewWorksheetAndName()
Application.ScreenUpdating = False
With ActiveWorkbook
  .Sheets.Add(After:=.Worksheets(.Worksheets.Count)).Name = Format(DateAdd("M", -1, Now()), "YYYY-MM")
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote
Reply

Tags
monthnow nowmonth mm

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert NowMonth MonthNow to double digits mm Find last three digits after . ( including zero) LearnerExcel Excel 4 02-08-2018 10:49 AM
Having number's digits together mohsen.amiri Word 0 06-23-2017 01:20 AM
Convert NowMonth MonthNow to double digits mm Need help calculating frequency of digits laucn Excel 2 06-08-2015 07:50 AM
Separate the digits into 3 combinations Jasa P Word VBA 1 08-19-2012 11:04 PM
Convert NowMonth MonthNow to double digits mm Problems merging in last 4 digits of an account higher than 16 digits Glynda Mail Merge 1 04-08-2011 12:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft