Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

undo command grayed out

0

Hello,

I have attached a workbook with three sheets. 

In summary, the workbook automates a purchase order where you select an item from the RKI Item code in Column D which is a list from a data validation.  The other items in the form are formulas keying off of RKI Item Code.

The workbook contains some macros to hide and unhide rows and a macro to make a PDF from the print range and ask user where to save the PDF.

It also contains a Private Sub which automatically expands column F when the cursor is located in that column.  

My question is:  While on the "PO" form sheet, the Excel  undo function is grayed out.  It is not grayed out while working on the other sheets.  Have you ever experienced this before?  What could be causing this?

Answer
Discuss

Discussion

Hi Susan,

This is a new one for me. I can only guess it has something to do with the structure or formatting. I searched online and found this: UnDo - ReDo It seems the problem possibly lies in a WorkSheet_Change event for this sheet. The code window for sheet3 (PO Form) is open and sized bigger than my laptop screen and the title bar is behind the menu bar so I cannot resize it - so I can't check all of the code in it. You also have a class module (which is empty) for sheet1 but there is no sheet1. When I look at the properties of "ThisWorkbook" it shows there is a password but when I go back to Excel and the "Review" tab it does not show the work book as protected??
I also noticed that, on sheet "PO Form", almost all cells do not show their cell ID in the name box - not until you hover the cursor over the down arrow. Very strange. Also, your structure of sheet "2024Prices" is a bit puzzling to me. The first 3 rows are a scrollable duplicate of rows 1-37 below it. You must have some need for the split screen. Wish I coild help beyond observing these oddities.

Cheers   :-)
WillieD24 (rep: 557) Mar 11, '24 at 7:58 pm
@WillieD24  Thank you for such a quick reply.  It sure does help to have a second set of eyes on one's work!  I wanted to say that I was hoping to get to this today, but things happen, so I'll look at this on Wednesday, so please be patient with me, can't do it tomorrow.  Thank you again.
SusanUser (rep: 16) Mar 11, '24 at 9:56 pm
@Willie @Susan - I added an Answer (to add to Willie's)
John_Ru (rep: 6142) Mar 14, '24 at 11:50 am
@Susan- I've now replaced my Answr with one that shrinks and expands the column but Undo is available nearly all the time. Hope you like it!
John_Ru (rep: 6142) Mar 16, '24 at 8:00 am
Add to Discussion

Answers

0
Selected Answer

@Susan

I don't like to walk away feeling beaten, so I did some more digging. First I added another worksheet (PO Form 2) and formatted rows and columns like the original PO Form. I then, one-by-one, added the macros to that sheet. Everything was fine until adding the Worksheet_SelectionChange event. That's when things went off the rails. 1) The reason the "UnDO - ReDo" or Ctrl+Z are not available is because changes made by a macro (Worksheet_SelectionChange event) have no UnDo ability. 2) The cell name issue also cropped up when this event was added. I have been unable to find a way do the column re-size and have the cell name show without having to hover the cursor over the down arrow of the name box. On my "PO Form 2" sheet I added a button with a macro to resize col "F" as/when needed. (file attached so you can see the result)

I also recommend getting rid of the class module since it is empty and sheet1 does not exist. I would also put all of the macros in a regular module and just leave the Worksheet_SelectionChange event in the worksheet code window.

If this answers your question please mark my answer as Selected.

Cheers   :-)

Discuss

Discussion

@Willie - nice one. Can't say I've noticed those aspects before but you've provided a solution :--) 

I took a quick look late last night and saw you'd written code to toggle column F between two fixed widths. Think I have used the property:
.EntireColumn.Autofit = True
instead of setting a fixed upper width but that depends on what Susan wants.
John_Ru (rep: 6142) Mar 13, '24 at 8:29 am
@Willie,
Thank you for the new code in Sub "Resize_2".  How does VBA know that every time the cursor is in column 6 that that sub is to be executed?  It was my understanding that in order for VBA to automatically run, you have to create a worksheet event (such as in the Worksheet_SelectionChange)  as I did in the workbook I attached to my original question.  Further, why does my original routine make the Undo not work?   It may not be important in that you provided a solution, however, for learning purposes I'd like to know!  I think the Worksheet  Event is a nifty tool but if it creates havoc I might hesitate to use it in other instances.
SusanUser (rep: 16) Mar 13, '24 at 9:06 pm
@Susan, The “Resize_2” macro has nothing to do with cursor position. It runs (is triggered) by clicking the “Resize Col F” button on the “PO Form 2” sheet. The code checks the current width of column F and if it is 70 then make it 36, if it is not 70 then make it 70. As I explained in my answer, it is the Worksheet_SelectionChange event (macro) that results in “UnDo” not being available on the “PO Form” sheet. This event (macro) is triggered every time a different cell is selected. As explained in my answer, any change made by a macro has no “UnDo” ability. I believe through all of this (despite taking the long road to get there) I have answered your original question: “My question is:  While on the "PO" form sheet, the Excel undo function is grayed out.” If you agree, please mark my answer as Selected.
Cheers   :-)
WillieD24 (rep: 557) Mar 14, '24 at 12:10 pm
Add to Discussion
0

Susan

Further to Willie's good work, the Undo/ReDo buttons are ghosted since once your code modifies the Excel environment (e.g. changing column F width), the Undo buffer is flushed.

Replacement Answer 16 March 2024:

After I'd posted a revised Answer (now deleted) based on automatic expansion but shrinking the column via a button (a bit like Wille's approach), I realised that a better compromise is possible...

In the attached file, column F expands and shrinks automatically but Undo is available mainly- it is only lost when you first click inside or first click outside the specified range. When you're inside the range, you can Undo actions- and the same if you're just working outside that range. The revised code is:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Dim ExpWidth As Long, ExpRng As Range, ShrnkWidth As Long
    ' state desired expanded and shrunken widths...
    ExpWidth = 70
    ShrnkWidth = 35
    ' ... of column if cells are selected in this single column range
    Set ExpRng = Range("F18:F22")
    
    ' do nothing for multiple selections...
    If Target.Count <> 1 Then Exit Sub
    
    ' only adjust column if one specified cells is FIRST selected
    If Not Intersect(Target, ExpRng) Is Nothing And ExpRng.ColumnWidth = ShrnkWidth Then
        'expand specified range
        Columns(ExpRng.Column).ColumnWidth = ExpWidth
        ' reselect (so NameBox repopulates)
        Target.Select
    End If
    
    ' only restore column if a cell outside specified range is FIRST selected
    If Intersect(Target, ExpRng) Is Nothing And ExpRng.ColumnWidth = ExpWidth Then
        ' shrink specifed range
        Columns(ExpRng.Column).ColumnWidth = ShrnkWidth
        ' reselect (so NameBox repopulates)
        Target.Select
    End If

End Sub

where you may need to change the values in bold (in other cases).

Once the column is expanded, the macro does nothing (so the Undo buffer isn't flushed) and likewise if it's already shrunken.

Incidentally, Willie pointed out correctly that the cell address (in the formula bar's NameBox) disappears very quickly when the original Worksheet_SelectionChange code is operational (and changes the environment) but I found that can be fixed easily via the lines above like:

        ' reselect (so NameBox repopulates)
        Target.Select

Hope this helps.

Discuss

Discussion

Can you tell me why the undo/redo buttons become ghosted via the Worksheet_Change event?  Will that happen with any Worksheet_Change event?  

And how does Willie's solution work without an Worksheet_Change event?
SusanUser (rep: 16) Mar 14, '24 at 5:39 pm
Susan. (Answering from my mobile so can't check this but...) Running VBA code generally  clears the Undo buffer (so the Undo button can't be used to reverse what the code did). With the Worksheet_Selection code changing things, it means the Undo button is ghosted everytime you swap cells so it becomes largely useless, especially if you have the default setting which moves to the next cell down after a value is entered say. 

Willie added a button titled Resize Col "F"  (and code) to expand column F "manually" (NOT automatically on change of selection, via the event). If you want to retain Undo/Redo, I suggest that you delete or comment out the Worksheet_SelectionChange event macro and use that button instead.  
John_Ru (rep: 6142) Mar 14, '24 at 6:34 pm
Thank you for your prompt responses and please know that my intention is to learn what is happening.  

Willie has provided a solution that does indeed work without ghosting the Undo/Redo button.  However, I did want Column 6 to automatically adjust upon selection.

I would still like to understand why that my Worksheet_Selection code makes the Undo/Redo button completely ghosted, even when the conditions to  activate the event (the Target.Cell is selected) are not met.  Maybe you could go through the code line by line.  I've looked at the only other example I have of a Worksheet event (the Turkey Manager) and it does not behave in this way.  The Turkey Manager workbook does ghost the undo button after running.  However, after the turkeys do their thing, and excel activity takes place, the undo is no longer ghosted and one can undo whatever was done! Not so with my code.  Why?  TIA
SusanUser (rep: 16) Mar 15, '24 at 11:13 am
Susan, please see Replacement Answer 16 March 2024 (and new file). I think this may work okay for you.

I don't have time to explain but your Turkey Mangaer file uses different events (Workbook_Open and Workbook_SheetActivate) so isn't comparable really.
John_Ru (rep: 6142) Mar 15, '24 at 2:39 pm
@Susan,
1) Please see my discussion point of Mar. 14 @ 12:10 PM.
2) The workbook is RunTurkeyRun – not Turkey Manager; Turkey Manager is the macro at the workbook level. There are NO Worksheet event macros. That is why the UnDo/ReDo buttons are greyed out only when the Workbook event macro is running. The Worksheet_SelectionChangeI event will cause the UnDo/ReDo to be greyed out because UnDo/ReDo cannot act on changes made by a macro. Changes made by a macro can only be undone by another macro or manually.
I believe I have answered your question, and John has added discussion supporting my explanation.
Again, I ask you, if you agree please mark my answer as Selected.
WillieD24 (rep: 557) Mar 18, '24 at 11:09 am
@Willie - agreed on points 1 and 2 but you say "John has added discussion supporting my explanation"- that WAS true but yesterday I provided a full solution, restoring both cell numbers and the Undo facility (except when you move between "zones" in the shee)t.

@Susan - you can decide which Answer you prefer and select accordinly. I'd like to think that my solution might be easier for your users; being automatic yet giving you back Undo.
John_Ru (rep: 6142) Mar 18, '24 at 11:26 am
I have decided to select WillieD24 answer of 11March2024  10:09 pm

It remains a mystery to me why the undo/redo button doesn't work with my Private_sub . When I get some more time I will play around with worsheet events.  Please know that I appreciate all the input and time you've spent on this!  It has not been a waste of time because I have learned and I'll come back when I have other questions!
Thank you.
SusanUser (rep: 16) Mar 18, '24 at 11:54 am
@Susan,
Thank you for selecting my answer.

As for the mystery, any change(s) made by any macro cannot be undone by the UnDo button. The change(s) can only be undone by running another macro or by doing so manually. The Worksheet_SelectionChange event is a macro. Therefore, after the event (macro) runs there is no option/ability to undo the change(s) by the UnDo button.

Post any future questions you may have and we will offer any help we can.

Cheers   :-)
WillieD24 (rep: 557) Mar 18, '24 at 1:21 pm
That's fine Susan, Willie did well.

I think the loss of Undo isn't too tricky to understand. Your original Worksheet_SelectionChange event was triggered everytime you moved between cells. Each time it changed Excel (set the width of column F) and in doing so flushed the Undo buffer which in turn ghosted the Undo button. Normally when you enter a value, Excel moves down a cell (so the macro fires and ghosts the Undo button as explained above). In effect, you never get Undo. Make sense?

My replacement selection gave you back the Undo button (except when you moved in or out of F18:F22). 
John_Ru (rep: 6142) Mar 18, '24 at 4:11 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login