CutCopyMode automatically reset - excel

I have a sheet, and a button on it (Excel forms button, not ActiveX).
Depending on current selection, the button is moved/resized or made visible/unvisible (handled by Worksheet_SelectionChange event).
All this was designed in Excel 2010 and worked fine until some end users reported errors while using the workbook in Excel 2007. The issue is, that when you copy/cut a range, and then change selection CutCopyMode is reset to False (floating frame around what was copied disappears). This does not happen in Excel 2010. I found out CutCopyMode is reset when I programatically modify button properties (top, height, left, visible, backcolor are modified each cancels CutCopyMode).
Do you have any ideas how to solve this?

Following the logic here, it seems that you will need to keep track of the range on the clipboard before modifying your button, and then recopying/recutting after modifying your button to restore the original range to the clipboard.

Related

Command Button doesn't work when first clicked

All the programming I have learnt is through manuals and websites such as this. So I wouldn't call myself a programmer but I do use a lot of quick and dirty macros to make my life easier. Occasionally, it all falls down and assistance is required!
I have a workbook that summarises the jobs I have worked on. In order to work quicker I have added a few command buttons too speed up simple tasks like filtering the blanks in a specific column. Normally this works OK. It is three weeks since I last used the workbook and today it is misbehaving. Click on a command button and the ribbon turns mostly grey. Click on the button again and the code runs and the ribbon returns to normal.
I have put a breakpoint in the code. On first click the breakpoint is not reached. On second click the breakpoint is reached!
Interestingly, if I click the command button the first time and then go into the vba editor and click on the break button the ribbon returns to normal again. So something is happening but it doesn't get as far as CommandButtonIssd_Click.
The command button code looks like this:
Private Sub CommandButtonIssd_Click()
FilterIt 9
End Sub
Most of the other command buttons just act on a different column number.
In the main module the code is:
Sub FilterIt(FieldNo As Integer)
'
' Filter Macro
'
FilterOff
ActiveSheet.Range("A4").AutoFilter
ActiveSheet.Range("A4").AutoFilter Field:=FieldNo, Criteria1:="="
End Sub
Sub FilterOff()
Dim a As Range, b As Range, iRow As Integer, iCol As Integer
If ActiveSheet.AutoFilterMode Then 'on, turn it off
Set a = Selection 'keep current location
iRow = a.Row
iCol = a.Column
Selection.AutoFilter
Range(Cells(iRow, iCol), Cells(iRow, iCol)).Select 'reset current location
End If
End Sub
All this does is filter column 9 to show only the blank cells.
I am thinking that it's a Windows10, Office365 issue as the code has been stable for a while and it was working OK last time I used it. In which case, it's just a case of waiting for MS to sort it's bugs out and issue an update (every Thursday, regular as clockwork!).
OR my code is suspect and needs tweaking to stop it messing with the system.
You thoughts would be appreciated.
I had this same problem today with an Excel 365 workbook: All CommandButtons would not fire on first click. They would fire on subsequent clicks, however.
today it is misbehaving. Click on a command button and the ribbon turns mostly grey. Click on the button again and the code runs and the ribbon returns to normal.
In my case, the bad behavior disappeared when I closed and reopened Excel and then reopened the workbook. After that, CommandButtons fired upon first click. I concur with #MikeK that it appears to be an Excel bug.
Finally!
Same problem, different workbook.
New laptop, Windows 11, Office 365 and I think I have a better definition of the problem as a little more information is now available.
If I open the spreadsheet on the laptop screen the macro and command buttons on the spreadsheet all work fine.
If set up the monitor screen as a duplicate of the laptop screen it all works OK.
If I set up the monitor screen as an extended display, then move the spreadsheet to the monitor screen and try again it fails as follows:
Open the macro sheet and it loads OK on the monitor screen but the form with the menu on it is displayed on the laptop screen!?
Exit the macro and it takes me to the macro spreadsheet and three command buttons as it should do.
Click on "Open Contact List" and the spreadsheet content is replaced by plain black (or the contents of the underlying window!). Click again without moving the mouse and the (now invisible) button works and loads the contact list.
close the contact list and the excel window continues to display incorrectly as above.
click inside the excel window and all is restored.
same applies to the other two buttons.
Shouldn't all this be handled by the windows system?
Previously I could deal with having to click a button twice to make it work. Now I have to click an invisible button!
If it is a Windows bug, how do I report it?

Userform keeps clearing values when reopened

How do I keep the values that where inputted into the excel Userform from clearing itself out once I click on the finish button? So whenever I call upon the Userform through a Commandbutton the previously filled field information is left the way it is and not to be cleared basically.
This Userform consist of MultiPage which is designed to take in the inputs from the user and place them in excel cells once the user has hit the finish button. Within the Userform contains Commandbuttons which toggles an hide/unhide feature which will also have to be saved somewhere
The Finish page (where the user will close/exit the page) looks like this:
Code for the finish button so far:
I'm not sure why everyone is saying there is no way to do this - you should be able to use:
Private Sub BtnFinish_Click()
Me.Hide
End Sub
And all previous values will be kept. Someone correct me if I'm wrong :).
I should add that once the EXCEL.EXE is closed, the values are lost.

unprotect sheet not working in excel 2013 (vba)

I have a workook in which userform will pop-up only if somesheets as present (say Sheeta). On clicking submit in userform will do and some calculation and store it in Sheetb. I need to lock some cells from editing in sheetb.
code used in submit click
Sheets("sheetb").unprotect password="test"
'code for calculation
sheets("sheetb").protect password="test" , AllowFormattingcells:=true 'This statement locks the unlocked cells
The above code works fine when pop-up the userform through button say (loadfile).
I don't know what mistake I made.
Please help me to get rid of this...
Note : I want to prevent cells Sheets("sheetb").Range("N10:Q20") only from editing. Is it possible to prevent cells from editing without protecting a sheet.
Thanks in advance!!!
Following my comment, if your goal is to protect only range "N10:Q20" on "sheetb" and leave the rest unprotected, then you can do that very easily: simply select the whole range on the worksheet (CTRL+A), press the right-mouse button and go to "Format the cells.."; select Protection tab and untick "Locked" box. Next, select your range "N10:Q20" and do the same procedure but this time tick the "Lock" box. Now, run your code and you will notice that only your selected range will be protected, all the other cells on "sheetb" will stay unlocked and editable.

VBA excel 2007. Create Button in cell

I'm creating a button with code
Set CreateButton = ActiveSheet.Buttons.Add _
(C.Left, C.Top, C.Width, C.Height)
where C is Cell object. Button appears not in cell but little upper.
CreateButton.Top is 107.25. And when I try to assign CreateButton.Top to itself, value is 114.75. What is going on?
It appears to be related to the margins. I changed it around and got the button to move its relative position by changing the top margin. Like you it only happened when adding the button while in page layout mode. In normal mode everything worked regardless of the margin.
You may be able to compensate with that information.

Userform in Excel 2013 disappears while switching an active workbook behind

I have faced one problem in Excel 2013. In my project, I have a userform that contains a listbox which lists names of all opened excel books.
When a user clicks on a particular name in the list, the appropriate book is activated. So the userform is always on top and while the user is clicking on the names in the list, he can see all opened workbooks getting activated one by one behind the active userform.
This is the way it used to work in Excel 2010.
Now, after upgrade from 2010 to 2013, if a user clicks on a name in the list, selected workbook gets activated but the userform disappears.
What exactly has been broken in Excel 2013 and what is the way to bypass this problem?
Yes, the default behavior has changed. In Pre Excel 2013, The different workbooks are shown within one main Excel window if opened in the same instance. But in Excel 2013, they are shown in their own window. And because of this a modeless userform will only be visible on top of the workbook that was active when the userform was loaded.
And I don't think this has a fix yet. It is by design and may not be fixed.
#Siddharth, based on what you said, I think I have a solution. When you open a form, it will only show up on top of the ActiveWorkbook. As long as all the code is in the same code thread (not sure if terminology is right) , you're stuck with the same workbook; But if you use the Application.Run or Application.Ontime commands it seems to start it's own code thread, which is independent enough to work. Just Activate the workbook/sheet first, and then Unload the form and remote call the macro that showed the form in the first place.
Unfortunately, you need to use Unload instead of Hide, because of the aforementioned issue; which means you need to save any/all options selected.
Private Sub lstFiles_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
ThisWorkbook.Application.Workbooks(lstFiles.Text).Activate
Unload Me
Application.Run "USER_Macro1"
'Application.OnTime Now(), "USER_Macro1"
End Sub
 
Private Sub USER_Macro1(Optional Control) ' As IRibbonControl)
ModelessForm.Show vbModeless
End Sub
I tested it in Excel 2013 and so far it even seems to work regardless of the Screenupdating = false issue seen elsewhere.

Resources