Menu to navigate through sheets doesn't work proper - vba

I've created a menu to navigate through sheets. But when I use one of the buttons to go to another sheet and type something in this another sheet the things that I type appear in the first one. Although when I use the tabs to jump to one sheet to another it works fine. Seems to be that the macro is considering a relative reference instead a absolute one.
Here's what my macro does (or at least should):
Private Sub cadastrar_clientes_Click()
Application.ScreenUpdating = False
Menu.Hide
Sheets("Clientes").Select
Range("A1048576").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Select
End Sub
Notes:
I've tried to use ".activate" instead of ".select".
I've tried to use only "Sheets("Clientes").Select", even so the bug occurs.
To execute "Excel /unregserver" on the Windows CMD solved my problem once, but I couldn't do this again. It doesn't seems to work anymore.
I did a menu like this one once, but I didn't had these problems.
When the workbook opens the following code is executed:
Private Sub Workbook_Open()
Application.DisplayFullScreen = True
Application.DisplayFormulaBar = False
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayWorkbookTabs = False
End Sub
That's it. I hope that somebody could help me.

Replace all code on VBA module Menu with this (tested in the file you provided)
Option Explicit
Private Sub fechar_menu_Click()
Menu.Hide
End Sub
Private Sub novo_pedido_Click()
goToTab ActiveWorkbook.Worksheets("Novo pedido"), "B5"
End Sub
Private Sub ver_pedidos_Click()
goToTab ActiveWorkbook.Worksheets("Consultar pedido"), "F1"
End Sub
Private Sub cadastrar_clientes_Click()
goToTab ActiveWorkbook.Worksheets("Clientes")
End Sub
Private Sub cadastrar_produtos_Click()
goToTab ActiveWorkbook.Worksheets("Produtos")
End Sub
Private Sub cadastrar_transportadoras_Click()
goToTab ActiveWorkbook.Worksheets("Transportadoras")
End Sub
Private Sub painel_financeiro_Click()
goToTab ActiveWorkbook.Worksheets("Painel Financeiro"), "B3"
End Sub
Private Sub goToTab(ByRef ws As Worksheet, Optional cel As String = vbNullString)
Menu.Hide
Application.ScreenUpdating = False
With ws
.Activate
If Len(cel) = 0 Then
.Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).Select
Else
.Range(cel).Select
End If
End With
Application.ScreenUpdating = True
End Sub

Well, I guess I know a lot more about the problem now. I've tested the workbook over various computers and came to the conclusion that the problem is within the Office 2013. I haven't seem this issue when running the 2007 or 2010 version. So far I haven't come to a solution. I would like to thanks Paul Bika for helping.

Related

starting a userform without excel in the background

I have this vba code where it has a userform.
Now I would like to start the userform; where the people can't see the Worksheets.
I have added this in my code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Visible = True
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True End Sub
Then in the "ThisWorkbook; I have added this:
Private Sub Workbook_Open()
Application.Visible = False ' only for final version
UserForm1.Show
'enter code here
End Sub
The thing is when I upload this .xlsm to a website, and when you open it; it does not start the userform right away. It somehow goes to an error. If you have an excel open.
Not sure how to protect the file, without having the endusers altering the file
Any ideas?
Maybe this helps:
In "ThisWorkbook" under "Open" add:
Private Sub Workbook_Open()
Application.Visible = False
UserForm2.Show vbModeless
End Sub
This will hide Excel and execute UserForm2.
BTW: To end your program add this code (e.g. to one of your Userform Buttons):
Private Sub CommandButton1_Click()
Application.Quit
End Sub
It will close Excel.

Disabling "cut" from excel with vba

I've read many threads on this topic, but the code I have found there doesn't seem to work. I am trying to disable the "cut" function from an excel spreadsheet and I would like the icon to grey out.
I have been using this code:
Sub WorkSheet_Activate()
Application.CommandBars.FindControl(ID:=21).Enabled = False
End Sub
However, I can still use the "cut" function without any problem....
Also I know there are some functions that allow you to disable cut/copy/paste, but I still want copy and paste to be allowed in this spreadsheet.
Thanks in advance for your help!
Kristen
Check the link for the reference - Disable Cut
Try something like this:
Option Explicit
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
Select Case Application.CutCopyMode
Case Is = False
'do nothing
Case Is = xlCopy
'do nothing
Case Is = xlCut
MsgBox "Please DO NOT Cut and Paste. Use Copy and Paste; then delete the source."
Application.CutCopyMode = False 'clear clipboard and cancel cut
End Select
End Sub
Hope it helps.

Combobox drop down showing up in other sheets

I have a combo box drop down with search suggestions, made from code here:
http://trumpexcel.com/2013/10/excel-drop-down-list-with-search-suggestions/
It works very well, but when I'm on another sheet and pressing "Enter", the search field randomly pops up in the sheet
It's not even the full box, just the blue field
Any insights on disabling it? The only success I've had is turning calculation to manual, but the workbook needs automatic calculation
Thanks!
I ran into a similar issue with my own VBA version of a smart search bar. How I fixed it was by doing the following:
Private Sub ComboBox1_Change()
If ComboBox1.Value = "" Then Exit Sub '<------ Problem solved.
ComboBox1.ListFillRange = "DropDownList"
Me.ComboBox1.DropDown
End Sub
OR
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim SheetWithComboBox As Worksheet: Set SheetWithComboBox = ThisWorkbook.Sheets(1)
If ThisWorkbook.ActiveSheet.Name <> SheetWithComboBox.Name Then
ComboBox1.Visible = False
Else: ComboBox1.Visible = True
End If
End Sub
#Tyeler
Thanks for your help, your thinking helped me think of a way
Private Sub ComboBox1_change()
Dim sht1 As Worksheet
Set sht1 = Worksheets("xxx")
If ThisWorkbook.ActiveSheet.Name = sht1.Name Then
ComboBox1.ListFillRange = "DropDownList"
Me.ComboBox1.DropDown
Call macro1
Else: Exit Sub
End If
End Sub
I found a solution that, at least for me, works for all sheets.
Private Sub Combobox_Get_Focus()
ComboBox1.ListFillRange = "DropDownList"
Me.ComboBox1.DropDown
End Sub

Can't Edit Cell Values After Clicking Excel UserForm

When I click a button on my UserForm it goes to the relevant sheet (via .activate and then End Sub, but I have also tried .select) but I cannot edit the cell. However, when I click into the sheet normally via the bottom pane I can edit it again.
I have not found an excel log or process manager so I cannot see what macros could be running or data that is loading* and could be affecting this - does anyone have any idea of the possible reasons I can't edit cell values after using the UserForm?
Here's my code for the button in question (I added the Unload Me part in the hope it would stop any additional UserForm subs leftover:
Private Sub CommandButton1_Click()
Sheets("2H Campaigns View").Select
Unload Me
End Sub
*Our sheets use quite a bit of external data but if this were the case I assume clicking the bottom pane to edit wouldn't work either...
I have also tried shutting out of the UserForm (and nay possible macros its running) immediately after click with Unload Me but to no avail.
EDIT: I put a print to cell function (on a separate debug sheet) in at the end of each sub to check if there are any others running after the button is clicked, but it reads that the button (the above sub) is the last sub to run. Thus its safe to assume that the problem is unrelated to the below subs; isolated to either the button sub or something which the running of the button sub does to the settings for the workbook....
Other Subs used in this sheet (all under UserForm - cbSector_Change and UserForm_initalize draw sheet names in for two menus that categorize the pages; one is a sub menu of the other):
Private Sub cbSector_Change()
If cbSector.Value = "DIST" Then
With cbCampaign
.RowSource = Worksheets("Master Data").Range("G13").Value
.ListRows = Worksheets("Master Data").Range("H14").Value
.Value = Worksheets("Master Data").Range("b16").Value
End With
ElseIf cbSector.Value = "INDU" Then
With cbCampaign
.RowSource = Worksheets("Master Data").Range("gl7").Value
.ListRows = Worksheets("Master Data").Range("h17").Value
.Value = Worksheets("Master Data").Range("b16").Value
End With
ElseIf cbSector.Value = "CS" Then
With cbCampaign
.RowSource = Worksheets("Master Data").Range("gl8").Value
.ListRows = Worksheets("Master Data").Range("h18").Value
.Value = Worksheets("Master Data").Range("b16").Value
End With
End If
End Sub
Private Sub EButton_Click()
ThisWorkbook.Saved = True
ThisWorkbook.Close
End Sub
Private Sub SEButton_Click()
ThisWorkbook.Save
ThisWorkbook.Saved = True
ThisWorkbook.Close
End Sub
Private Sub UserForm_Initialize()
With cbSector
.RowSource = Worksheets("Master Data").Range("b13").Value
.ListRows = Worksheets("Master Data").Range("b14").Value
.Value = Worksheets("Master Data").Range("b12").Value
End With
End Sub
Private Sub cbSelect_Click()
If cbSector.Value = "(none)" Then
errormsg = "Please Select Sector"
ElseIf cbCampaign.Value = "(none)" Then
errormsg = "Please Select Campaign"
Else: errormsg = "nothing"
End If
If errormsg = "nothing" Then
Sheets(cbSector.Value & "_" & cbCampaign.Value).Select
Unload Me
Else: MsgBox (errormsg)
End If
End Sub
To launch the UserForm this code is attached to a button on all but one of the sheets in the workbook:
Public SheetSelected As Worksheet
Public errormsg As String
Sub CallUserForm()
nav.Show
End Sub
This is a partial answer to the last question you asked in the comments ("would there be any way to hack a manual sheet tab click in vba"?) I don't know how to do that directly, but here is an ugly hack which simulates using the Ctrl+PgUp and Ctrl+PgDn keyboard shortcuts to tab from one worksheet to another:
Sub PageToSheet(SheetName As String)
Dim here As Long, there As Long, i As Long
here = ActiveSheet.Index
there = Sheets(SheetName).Index
If here = there Then
Exit Sub
ElseIf here < there Then
For i = 1 To there - here
Application.SendKeys "^{PGDN}"
Next i
Else
For i = 1 To here - there
Application.SendKeys "^{PGUP}"
Next i
End If
End Sub
This won't work when the VBA editor is the active window. But if you launch the following test sub while the main Excel window is active it seems to work:
Sub test()
Dim s As String
s = InputBox("Enter name of sheet to go to")
PageToSheet s
End Sub
It would be better to try to track down the source of the bug that you are seeing and even experiment with transferring all data and code to a new workbook to make sure that you don't have an inexplicable corruption in the file itself (which is sometimes what is behind truly weird behavior). Still -- if you want to simulate manual page tabs you can via SendKeys.

How can I run a macro as a workbook opens for the first time only?

I've got a workbook which runs a macro to show the userform Open1 as it opens, using the (very basic) code:
Private Sub Workbook_Open()
Open1.Show
End Sub
This does its job fine - each time I open the workbook, the userform pops up and runs perfectly.
But, I want the userform to appear the first time the workbook is opened only. Is there a way to allow this to happen?
You could use a dummy module which gets deleted the first time you open the spreadsheet...
Something like:
If ModuleExists("DummyModule") Then
Open1.Show
DoCmd.DeleteObject acModule, "DummyModule"
End If
Function ModuleExists(strModuleName As String) As Boolean
Dim mdl As Object
For Each mdl In CurrentProject.AllModules
If mdl.Name = strModuleName Then
ModuleExists = True
Exit For
End If
Next
End Function
Update: as stated, DoCmd isn't used in excel vba. That will teach me to write code without testing it!
The following updated code will work, but in order to access the VB environment, excel needs to be trusted.
There is a setting in the Trust Center>Macro Settings that you can tick for this code to work under Developer Macro Settings
As such, this may not be the way to go as it opens up the possibility of security issues...
Sub RemoveModule()
If ModuleExists("DummyModule") Then
Open1.Show
Dim vbCom As Object: Set vbCom = Application.VBE.ActiveVBProject.VBComponents
vbCom.Remove VBComponent:=vbCom.Item("DummyModule")
End If
End Sub
Function ModuleExists(strModuleName As String) As Boolean
Dim mdl As Object
For Each mdl In Application.VBE.ActiveVBProject.VBComponents
If mdl.Name = strModuleName Then
ModuleExists = True
Exit For
End If
Next
End Function
Try this:
If Sheets("Hide").Cells(1,1) = "1" Then
Open1.Show
Sheets("Hide").Cells(1,1) = "0"
End if
You must create the sheet Hide, and give the cell A1 the value 1, in that case the form will be shown.
After you create the sheet, hide it with this
Sheets("Hide").Visible = xlVeryHidden
And show it with this
Sheets("Hide").Visible = True
Here's an alternative bit of code that will persist between saves and allow you to reset it. No need to create a hidden sheet.
Put this in a module (invoke the DisplayFormIfFirstTime from your Workbook_Open event handler....)
Option Explicit
Private Const cMoniker As String = "FormHasBeenDisplayed"
Private Sub DisplayFormIfFirstTime()
If HasBeenOpened = False Then DisplayForm
End Sub
Public Sub DisplayForm()
MsgBox "Ok, its not a form but a dialog box...", vbInformation
End Sub
Public Function HasBeenOpened() As Boolean
Dim oName As Name
On Error Resume Next
Set oName = Application.Names(cMoniker)
On Error GoTo 0
If Not oName Is Nothing Then
HasBeenOpened = True
Else
Call Application.Names.Add(cMoniker, True, False)
End If
End Function
'Call this to remove the flag...
Public Sub ResetOpenOnce()
On Error Resume Next
Application.Names(cMoniker).Delete
End Sub
Based on the idea supplied by PaulG, I have coded an upgrade that will check for the name and if not found run a function, add the name and save the workbook for a more seemless approach to this problem...
Placed in ThisWorkbook
Private Sub Workbook_Open()
Run "RunOnce"
End Sub
Placed in a module
Sub RunOnce()
Dim Flag As Boolean: Flag = False
For Each Item In Application.Names
If Item.Name = "FunctionHasRun" Then Flag = True
Next
If Flag = False Then
Call Application.Names.Add("FunctionHasRun", True, False)
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
Call RunOnceFunction
End If
End Sub
Private Function RunOnceFunction()
Open1.Show
End Function
Sub ResetRunOnce()
For Each Item In Application.Names
If Item.Name = "FunctionHasRun" Then
Application.Names.Item("FunctionHasRun").Delete
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
End If
Next
End Sub