VBA does not active - excel

I suggest that my workbook contains a VBA code below. But It does not run when I opened my workbook too.
Sub Appl_StatusBar()
Application.StatusBar = "SupportABC"
End Sub

If you put your code in the Workbook Open Event it will do what you need. To do this click the top dropdown where it says "(General)" and hit "Workbook". In the right dropdown select "Open" and save your code there". See below
Private Sub Workbook_Open()
Application.StatusBar = "SupportABC"
End Sub

Related

How to access a VBA Userform Button control code programmatically

I want whenever I click on my ActiveX command button on Sheet1 it open VBA Editor and jump directly to the code I wrote for a button on my userform. I know the way to jump directly to code for a module but I couldn't find a property for userform button control (something like ThisWorkbook.VBProject.VBComponents("UserForm1").Controls("MyButton"))
Following is my code for jumping right into my Madule1 code:
Private Sub CommandButton1_Click()
Dim Wb As Workbook: Set Wb = ThisWorkbook
'---Open Module1
Application.VBE.MainWindow.Visible = True
Wb.VBProject.VBComponents("UserForm1").Activate
End Sub
I thaught these pictures would help to better understand my goal:
1-I have an ActiveX command button on sheet1
2-I have userform with a commandbutton on it
3-This commandbuttun has it's own code which I want to jump to it's code
If you want to jump to that specific sub directly, try this:
Private Sub CommandButton1_Click()
Application.VBE.ActiveVBProject.VBComponents("UserForm1").CodeModule.CodePane.Show
With Application.VBE.ActiveCodePane.CodeModule
lStartLine = .ProcStartLine("CommandButton1_Click", 0) '"CommandButton1_Click" being the name of the sub you want.
.CodePane.SetSelection lStartLine, 1, lStartLine, 1
End With
End Sub
First line should show you the code panel for the userform, and the second part selects the row down by the sub you are looking for by name.

VBA disabling drag and drop prevents users to copy and paste to another workbook

I've tried to disable drag and drop on a specific Excel workbook.
I wrote the following code on VBA editor on 'My Workbook' section.
Private Sub Workbook_Activate()
Application.CellDragAndDrop = False
End Sub
Private Sub Workbook_Deactivate()
Application.CellDragAndDrop = True
End Sub
Problem is: after that people cannot copy-paste any cell from this workbook to another.
How's so?
Thank you for any suggestion

Non modal UserForm and Workbook navigation

Very specific problem not related directly to code but more to Excel behavior.
When launching a non modal UserForm (vbmodeless) from a minimized WorkBook (not visible on screen), while another WorkBook is on screen, Excel will "link" this UserForm and its focus to the visible one. This means that minimizing or maximizing the second WorkBook will hide and show the UserForm, but doing so with the first WorkBook won't, even if it has been launched by the first WorkBook.
Worse, closing the second WorkBook will close the UserForm.
An example of the issue :
Sub TestSo()
Application.WindowState = xlMinimized
UFTest.Show vbModeless
End Sub
Just create an empty UserForm named UFTest, open 2 Workbooks and display them split mode on screen and then call this Sub. You'll see that the UserForm is now linked to the wrong WorkBook.
Is there any way to prevent this from happening?
Does it do the trick?
Sub TestSo()
UFTest.Show vbModeless
ThisWorkbook.Windows(1).Visible = False
End Sub
Then in Userform restore the visibility
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub

Userform showing when not wanted, not called for

im having some weird things happen with some code in excel
Private Sub CommandButton2_Click()
Dim thiswb As Workbook
Set thiswb = ActiveWorkbook
Call EnterDataToSS(thiswb)
Me.Hide
BeamNoFind.Show vbModal
End Sub
the called code basically drops some values into a spreadsheet.
any left over values are populated to LISTBOX1 on BeamNoFind Userform.
Then on the BeamNoFind userform there is a button and the LISTBOX1. when you select and item from listbox1, and click the button, a third userform opens (VBMODELESS) to allow placement of the value.
below is the code of the button to show the third userform.
Private Sub CommandButton2_Click()
Dim Selected_Length As String
Dim Selected_Name As String
Dim Selected_Length_index As Integer
Selected_Length_index = BeamNoFind.ListBox1.ListIndex
With BeamNoFind.ListBox1
If .ListIndex > -1 Then
Selected_Name = .Column(0, .ListIndex)
Selected_Length = .Column(1, .ListIndex)
CellInputForm.beam_length_label.Caption = Selected_Name & " [ " & Selected_Length & " ] "
BeamNoFind.Hide
'ChngDataSrc.Hide
'Unload ChngDataSrc
CellInputForm.Show vbModeless
Else
MsgBox "No selection", vbExclamation, "Oops!"
End If
End With
End Sub
the weird thing is, when i click the button to show my modeless userform, to place the data in a cell, the initial macro is being triggered that drops you into the first userform.
Where i have the commented code 'ChngDataSrc.Hide' and 'Unload ChngDataSrc' are my attempts to stop the userform from displaying when i dont want it to.
When i unload the form, i then get an error instead of the form displaying, the error is with the initial macro:
Sub get_scheduling_data(control As IRibbonControl)
ChngDataSrc.Show
End Sub
It has something to do with vbModeless because if i replace "vbModeless" from "CellInputForm.Show vbModeless" line with "vbModal", it shows correctly, without the unwanted form (ChngDataSrc). but then the function of the form (select cell, press ok button, value placed in selected cell) is gone.
I found a solution, but its not a real solution,
i placed
ChngDataSrc.Hide in the Activate sub of the CellInputForm userform.
So when CellInputForm.show vbModeless is run, and the ChngDataSrc userform pops up unwantedly, it is then hidden again.
Id rather find out why it is being showed in the first place, but this fix seems to work for now.

VBA to stop printing worksheet in excel 2010

I am preparing journal voucher in excel 2010. I want to minimize the error. Hence i need a VBA code which helps to stop printing voucher if to cells are not equal. Please help.
What you want to do is create an event. In the VBA editor, double click ThisWorkbook tab in the Project Explorer and enter this code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Sheets("Sheet1").Range("A1") <> _
Sheets("Sheet2").Range("A1") Then
MsgBox "Cannot print. Values do not match"
Cancel = True
End If
End Sub
You can edit the cells (A1 on both sheet1 and sheet2 in my example) to be whatever you need. Make sure you save the workbook and open it with macros enabled for it to be active.

Resources