Edit excel sheet manually while running a code - excel

I would like to know if i can fill in a row of values manually during the run of a code when a msgbox is displayed by the code asking me to fill the same. I tried filling them but after i press 'ok' on the message box, the code continues to run and also, i cant edit the file without pressing ok on the message box. So, could anyone tell me if there is a way I can do that? Thanks in advance!

You could try to use a userform instead of an messagebox. As far as i know there is no simple way to create a messagebox, where you can edit the sheet in background.
If i need to edit the sheet while having an active macro, i'm using a userform. (they are not that hard insert in vba, try it!)
You have to start the userform with the vbmodeless.
Should look like this: (i'm not 100% sure, because i've no vba ide herere to test, but i thinks it's something like this)
Sub ShowMyCreatedUserform()
myUserform.Show vbModeless
End Sub
If this userform is open, your code stops, but you can edit the sheets. After pressing a button (for example "ok"-button) you can continue the macro.

I found 'ontime' command on the net.. I think the code can be split into 2 parts and ontime function can be used..

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?

VBA - Run excel's built-in Screenshot tool (not ScreenClipping) using VBA

Does anyone here have any idea how to row Excel's built-in tool (see screenshot below) and automatically select specified window to capture using VBA Script?
What the code below does is essentially press the shortcut keys: Alt+N+SC+C, where Alt=%, N=Insert Ribbon, SC=Screenshot Button, C= Capture Screen Button, you may have to modify depending on language/office version/shortcut setup. I got it working by running the code from a form control button on the worksheet, running from VBA-editor did not work in this case.
Sub Screenshot()
SendKeys ("%NSCC"), True
End Sub
Note: You should only use the SendKeys Method if no other option is
available, because it can cause problems, if the wrong window is
active when the code runs.
_
Edit: As you have requested to pick one of the windows in the
screenshot gallery, i recommend you find the the SendKeys equivalent
of TAB and ENTER. Not the best solution i admit, but better than
nothing.
_
Edit2: See this brilliant answer:

Stop macro to allow the workbook to be edited

I have written a code to open a workbook and filter data by the number in a particular column. I need to filter "0" first and then edit the data and then filter between "+8" and "-8".
I added a message box to pause the macro but I am not able to edit the opened workbook while the message box is displayed. I tried with "Application.waiting" option.
I need to pause the macro automatically and start it manually.
Sub Filter_data()
Workbooks.open"D:\Reposrts\AAA.csv"
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:="0"
Activesheet.columns("A:Z").Autofit
MsgBox"Task ok" 'Here I need to pause the macro and allow for edit opened wb and then manually start macro for below line'
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:=">8", Opersator:=xlAnd, Criterial:="<-8"
End Sub
Split your existing Macro into two parts
The first part contains everything up to have the MsgBox"Task ok" line
The second part contains everything after that point
Create a Modeless (or "Non-Modal") User Form with your label and an "OK" button to call the second part of your split macro
"Modeless" means you can edit the workbook while the User Form is open. MsgBox is "Modal", which is why you can't edit anything. More details available from Microsoft
Replace the MsgBox"Task ok" line in the first part of your split macro with a line that opens your User Form.
Once the first half-macro has finished, and the User Form is waiting for you to click "OK", you will be able to edit workbooks. Once you click "OK", the second part will start.
It is probably best to first consider if the "edits" you need the user to make are possible via VBA, a User Form, or a DialogBox (which include the likes of the "Select Range" DialogBox, or the "Colour Picker" DialogBox)
I would recommend you not to try to edit the Workbook while a macro is running- I don't even think that is possible at all. Furthermore, try not to refer to ranges by activating/selecting ranges.
Activesheet.Range("I1:I100").Autofilter 'Instead of this use the code below
With Workbooks("AAA.csv")
.Sheets("NAME").Range("I1:I100).Autofilter
'More code
End With
Like Chillin mentioned, you could assign hotkeys to your filter macros- split them in two. Other than that, you can use buttons to activate the macros.
To assign a keyboard shortcut to a macro:
Press ALT+F8 to open the macro dialog box. Select the macro, and click on Options. In the window that opens you can assign a keyboard shortcut to the macro you selected.

Keep the previous userform open after Unload.Me using Excel VBA

I use the Unload.Me function to close a userform.
I have two userforms, when I close one, the second userform will close automatically as well.
I want try to keep the previous userform open.
A quick workaround, try to specify the exact form you are unloading.
i.e. use Unload UserForm2
If that doesn't help, check if the forms are modal forms. If not, set them to modal.
I tried adding a subsequent instruction in the same sub procedure that called/showed Userform2 (thinking that it might not close Userform1 until it finished the rest of the code after Userform2.show). Instructions like assigning a random value to a random variable did not work, but making a message box pop up did work. For example in the sub procedure calling userform2 you might try:
Userform2.show
Msgbox("Userform2 has already closed. Close this message box to see Userform1.")
This seems despotic to me too, but it worked.
This's caused by a code that executes when the form that you don't want to close becomes the active form.
It's 2022 now, but anyway, for those interested.
You do not need a messagebox, you can just set focus to an item in userform1.
That way you avoid having to click OK to close the messagebox

Why won't code in the Worksheet_Change() event run?

Update:
If I defined range as input_range from A2:P29. Then If Enter in row 30, then it works. However, the Input_range is not automatically extended and I would like to shift the whole row down not only last row in the range. How could I achieve these two things?
Previous question
Starting with the help of Siddharth Rout, I am able to make this work on new Excel file. However, no matter what I tried on this file
Sample Excel File - it just does not work. I guess there is some kind of structure to prevent from automating adding row. If so, how can I am able to make the code work?
When you're detecting the Worksheet_Change() event, you need to have your code in the worksheet object, not in a module.
Here's how to do it:
Alt-F11 to open the VBA editor
Copy the Worksheet_Change() sub from Module1
Double-click on Sheet6(itemmaster)
Paste the code in there
Make sure you see Worksheet and Change in the two drop-downs at the top of the editor.
Note: you should always have Option Explicit turned on to require variable declaration. Read this answer to see how to do it: https://stackoverflow.com/a/10653204/138938

Resources