I have a code which processes an excel sheet row by row.
Under certain conditions, certain manual manipulations are to be done on the data in the rows. For this, the program fires a UserForm. The UserForm contains certain fields which are populated from the underlying excel rows. These fields are then manipulated by the user and certain buttons on the userform are clicked. Once the manual processing is done, the program then needs to proceed with the rest of the rows in the Excel sheet. When the UserForm is triggered, before taking action on the USERFORM, user needs to to see
a) What is pushed up in the UserForm
b) Contents of the Underlying Excel Sheet
The program needs to wait till the user takes action
However, there seems to be no easy way to let the user see the underlying rows as the Userform (on top) prevents access to the Excel Sheet below. To allow access to the Excel Worksheets, I tried using
UserForm.Show vbModeless
UserForm.Show False
Both of these are totally bypassing the Userform and the program runs for the remaining rows without waiting for the user input.
I researched here and found this.
deactivate Excel VBA userform
In my Case, the MainProgram calls the UserForm and has to wait for the user input in the USERFORM before processing further rows. If the conditions triggering the userform are not met, the program proceeds to the next rows.
Help highly appreciated. Thanks in advance.
EDIT:
As a workaround I have written a line to select the cell where the manual intervention is triggered. This brings that cell right under the manual intervention UserForm. The UserForm can now be moved around to look at what is below.
How about adding a de-activate Button to your form using this script:
Private Sub CommandButton1_Click()
UserForm1.Hide
MsgBox "hit OK to continue", vbOKOnly, "Script Paused"
UserForm1.Show
End Sub
Related
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.
I'm a new VBA user. I have a workbook with multiple worksheets. Each one has it own userform for data entry. The userform shows when I click on the sheet. After I'm done entering data, the worksheet is populated and the userform closes (unload). All this works well. However, after the initial data entry is complete, the goal is to use the data on the worksheets for other applications and the userform is no longer needed. What is the code or the terminology to say the userform should not reappear again when the worksheet is clicked on? Currently, I red X out of the userform. If I click the command button to close, it repopulates and I lose all my data.
Thanks!
As A.S.H commented; you could store the information in a number of ways. An easy example is declaring a variable outside of the Macro:
Public FormOpened as boolean
And then set FormOpened as true once the form has been shown. Then you could add a check to the start of the mouse-click macro:
If FormOpened = True then Exit sub
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.
My macro is going to compare a sheet with another sheet. This second sheet needs the user to paste data in there. (Note: The data being copied is not in Excel).
One way is to run the macro, and end it by prompting the user to paste the data in, then run "Macro2". However, I'd like to keep it all in one macro, so have found a way to wait for user input before continuing. This seems to work for me, so my main question is:
How stable is doing it this way?
...macro stuff above here
MsgBox ("[Please copy the data into the new sheet, after clicking 'OK']")
Do While WorksheetFunction.CountA(newWS.Cells(1, 7)) < 1
DoEvents
Loop
...then after the user pastes info, continue on, using the data that's been pasted.
The idea is that DoEvents just runs and runs while my sheet is blank. Right after the user pastes the data into the newWS, the macro continues on (since it will see data in column 7)...
Is this an okay method, or is it a bad idea to use like that? I've never really used DoEvents, so don't know if it's doing something in the background that could cause issues.
Edit: The data is in Lotus Notes, which I can export to Excel. However, that takes a few more steps (and I'd rather not create some new temporary excel files), so copy/pasting is my preferred method. This question is half practical, and half theoretical. Sorry for any confusion!
Probably not the best idea. Instead, allow them to select the data and perform the copy, all through VBA:
MsgBox ("[Please select data to copy into the new sheet, then press 'OK']")
newWs.Cells(1,1).PasteSpecial '### Modify to your specific location to paste the data
'Here you can add logic to validate that they have pasted enough data,
' and use control statement to prompt them to paste more data, etc.,
' if necessary, or exit sub early
'For example:
If WorksheetFunction.CountA(newWS.Cells(1, 7)) < 1 Then
MsgBox "Try again!"
Exit Sub
End If
Alternatively, you can use a DataObject:
Dim dataObj As New MSForms.DataObject
dataObj.GetFromClipboard
newWs.Cells(1,7).Value = dataObj.GetText
You could restructure the code so that it lives inside of a userform with ShowModal set to false (in the properties). Code prior to when you want the user to gather data can be put in the useform's initialize event. Then the userfrom shows (with a simple label caption and an okay button). Since it is modeless the user can copy data from an external program and paste it in. Then the rest of the code runs after the user hits okay. The form itself can be hidden during this phase. As proof of concept I created the following form:
with the following code:
Private Sub CommandButton1_Click()
Me.Hide
MsgBox Range("A1").Value
Unload Me
End Sub
Private Sub UserForm_Initialize()
'macro code can go here
'it runs before the form shows
'e.g.
MsgBox "Initializing"
End Sub
I launch the form on a blank sheet. First a message box appears before the code (confirming that code can run while the form is being initialized but before it is visible) then the form shows:
I go to an open instance of Notepad which contain a sentence and, while the form is still open -- paste it into A1:
Finally I press okay and the userform then hides itself but continues to run code (which now has access to the copied data):
Remember to unload the form at the end.
I am trying to monitor a sheet which has several text boxes on it.
The idea behind it is the user enters text into these boxes and then you can click the submit button to send it all to a sql database.
Now I want to have it so if the user has made changes, and they go to leave the sheet a macro is triggered to tell them that they haven't saved it.
I've already got this triggering on the deactivate worksheet event, but I was wondering if I can monitor all of the textboxes on the sheet (oleobjects) under one change event
I am already assuming this isn't possible with just one but was hoping.
Thanks in advance
Tom
One way to do this would be to write a separate subroutine that is called within the Change event of all the Textboxes. Keep in mind though that this will be raised every time the Textboxes change at all- every keystroke.
In each TextBox_Change Event:
Private Sub TextBox1_Change()
TextChanged TextBox1
End Sub
New subroutine:
Sub TextChanged(fromTextBox As TextBox)
'validation code goes here
End Sub