SOLVED

Automation of Date

%3CLINGO-SUB%20id%3D%22lingo-sub-2279191%22%20slang%3D%22en-US%22%3EAutomation%20of%20Date%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2279191%22%20slang%3D%22en-US%22%3E%3CP%3EThere%20is%20date%20appearing%20as%2002%2F28%2F2021%20in%20cell%20B6%20in%20the%20attached%20excel%20sheet.%20I%20want%20that%20when%20the%20%22Previous%20Month%22%20button%20is%20selected%2C%20the%20date%20in%20cell%20B6%20should%20change%20to%2001%2F31%2F2021.%20Similarly%20when%20button%20%22Next%20Month%22%20is%20selected%2C%20the%20date%20in%20cell%20B6%20should%20change%20to%2003%2F31%2F2021.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20don't%20how%20this%20can%20be%20achieved%20but%20it%20is%20of%20utmost%20important%20to%20achieve%20this.%20Any%20similar%20idea%2Fs%20is%2Fare%20also%20welcomed.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2279191%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20for%20web%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20on%20Mac%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20on%20mobile%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20Scripts%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EUser%20Adoption%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2281223%22%20slang%3D%22en-US%22%3ERe%3A%20Automation%20of%20Date%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2281223%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F577386%22%20target%3D%22_blank%22%3E%40Dharmendra_Bharwad%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20have%20very%20little%20experience%20with%20Google%20Sheets%2C%20sorry.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2281218%22%20slang%3D%22en-US%22%3ERe%3A%20Automation%20of%20Date%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2281218%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20was%20wondering%20if%20you%20can%20help%20me%20to%20have%20the%20same%20result%20in%20Google%20Sheet%20then%20it%20would%20be%20double%20thumbs%20up%20to%20you.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2279278%22%20slang%3D%22en-US%22%3ERe%3A%20Automation%20of%20Date%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2279278%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F577386%22%20target%3D%22_blank%22%3E%40Dharmendra_Bharwad%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20did%20the%20following%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EI%20right-clicked%20the%20Previous%20Month%20button%20and%20selected%20Assign%20Macro...%20from%20the%20context%20menu.%3C%2FLI%3E%0A%3CLI%3EI%20entered%20PreviousMonth%20in%20the%20Macro%20name%20box%20(a%20macro%20name%20cannot%20contain%20spaces)%2C%20then%20clicked%20New.%3C%2FLI%3E%0A%3CLI%3EThis%20took%20me%20to%20the%20Visual%20Basic%20Editor%2C%20with%20the%20first%20and%20last%20lines%20already%20created.%20It%20looked%20like%20this%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20PrevMonth()%0A%0AEnd%20Sub%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CUL%3E%0A%3CLI%3EI%20typed%20the%20code%20into%20the%20line%20in%20between.%20It%20takes%20the%20value%20of%20cell%20B6%2C%20and%20used%20the%20EOMONTH%20function%20to%20store%20the%20last%20day%20of%20the%20previous%20month%20in%20B6%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20PrevMonth()%0A%20%20%20%20Range(%22B6%22).Value%20%3D%20Application.EoMonth(Range(%22B6%22).Value%2C%20-1)%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CUL%3E%0A%3CLI%3EI%20switched%20back%20to%20Excel%2C%20then%20did%20basically%20the%20same%20for%20the%20Next%20Month%20button%2C%20but%20with%20a%20different%20line%20of%20code%2C%20of%20course.%3C%2FLI%3E%0A%3CLI%3EFinally%2C%20I%20pressed%20F12%20from%20the%20workbook%20to%20display%20the%20Save%20As%20dialog.%3C%2FLI%3E%0A%3CLI%3EI%20selected%20Excel%20Macro-enabled%20Workbook%20(*.xlsm)%20from%20the%20Save%20as%20type%20dropdown%2C%20then%20clicked%20Save.%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2279266%22%20slang%3D%22en-US%22%3ERe%3A%20Automation%20of%20Date%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2279266%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3BThanks%20a%20lot%20for%20the%20solution.%20yes%2C%20it%20works%20exactly%20as%20I%20wanted.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20it%20would%20be%20great%20if%20you%20can%20specify%20each%20step%20by%20step%20as%20I%20need%20to%20have%20multiple%20similar%20macros%20to%20be%20enabled%20in%20other%20workbooks%20too.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2279218%22%20slang%3D%22en-US%22%3ERe%3A%20Automation%20of%20Date%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2279218%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F577386%22%20target%3D%22_blank%22%3E%40Dharmendra_Bharwad%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20can%20be%20done%20with%20a%20couple%20of%20macros.%20See%20the%20attached%20version%20-%20now%20a%20.xlsm%20workbook.%3C%2FP%3E%0A%3CP%3EMake%20sure%20that%20you%20allow%20macros%20when%20you%20you%20open%20the%20workbook.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EP.S.%20The%20value%20in%20cell%20B6%20was%20a%20text%20value%2C%20I%20converted%20it%20to%20a%20'real'%20date.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

There is date appearing as 02/28/2021 in cell B6 in the attached excel sheet. I want that when the "Previous Month" button is selected, the date in cell B6 should change to 01/31/2021. Similarly when button "Next Month" is selected, the date in cell B6 should change to 03/31/2021.

 

I don't how this can be achieved but it is of utmost important to achieve this. Any similar idea/s is/are also welcomed.

 

Thanks.

5 Replies

@Dharmendra_Bharwad 

This can be done with a couple of macros. See the attached version - now a .xlsm workbook.

Make sure that you allow macros when you you open the workbook.

 

P.S. The value in cell B6 was a text value, I converted it to a 'real' date.

@Hans Vogelaar Thanks a lot for the solution. yes, it works exactly as I wanted.

 

But it would be great if you can specify each step by step as I need to have multiple similar macros to be enabled in other workbooks too.

best response confirmed by Dharmendra_Bharwad (Contributor)
Solution

@Dharmendra_Bharwad 

I did the following:

  • I right-clicked the Previous Month button and selected Assign Macro... from the context menu.
  • I entered PreviousMonth in the Macro name box (a macro name cannot contain spaces), then clicked New.
  • This took me to the Visual Basic Editor, with the first and last lines already created. It looked like this:
Sub PrevMonth()

End Sub
  • I typed the code into the line in between. It takes the value of cell B6, and used the EOMONTH function to store the last day of the previous month in B6:
Sub PrevMonth()
    Range("B6").Value = Application.EoMonth(Range("B6").Value, -1)
End Sub
  • I switched back to Excel, then did basically the same for the Next Month button, but with a different line of code, of course.
  • Finally, I pressed F12 from the workbook to display the Save As dialog.
  • I selected Excel Macro-enabled Workbook (*.xlsm) from the Save as type dropdown, then clicked Save.

@Hans Vogelaar 

I was wondering if you can help me to have the same result in Google Sheet then it would be double thumbs up to you.

@Dharmendra_Bharwad 

I have very little experience with Google Sheets, sorry.