How to display a specific sheet from the VBA editor window - show

I want to activate my sheet "feuil2" with the following code that I run from the VBA editor.
Sub display()
Workbooks("intro.xlsm").Sheets("Feuil2").Activate
End Sub
I press F5 to run this macro, but the VB editor remains in the foreground and I don't see the sheet "feuil2"
I also tried with "select", and with "visible=yes" but it does not work better.
I also tried from the immediate window: doesn't work!
Do you have an idea?
Thanks in advance

Related

Excel thinks Combo Box is clicked when sheet is calculated in VBA

I have a number of private subs in Sheet code that refer to ComboBoxXX_Click. If I run another macro in the same Sheet that has SheetX.Calculate anywhere in the macro (referring to the sheet that contains the ComboBox) in the VB Editor using the step-into function, when it comes to execute the .calculate line it jumps to the ComboBoxXX_Click Macro. This makes me think that Excel is thinking that the ComboBox is clicked when the sheet is recalculated.
Try a different option instead of ComboBoxXX_Click
I suggest looking at the list of events that can be triggered on MSDN for that control.
try MouseDown
If you could add more information and blocks of code to help us help you. Also have you stepped through your code to find out at what point it is calling the ComboBoxXX_Click?
enter image description here
Here is an example of what I mean. Once the step into hits the Sheet4.Calculate on the first macro it skips to the second one (ComboBox25_Click) and if I step into the second one, once it reaches the Sheet4.Calculate line it also skips to a random ComboBoxXX_Click Macro.

Hyperlink doesn't activates macro in VBA

I'm trying to get a hyperlink to activate a macro. I can't use a fixed target address, because I've several links; The goal is to create a clickable history of taken step. So I need to create a back button.
I already tried this:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.Range.Address = "$A$4" Then
MsgBox "This isn't what I had in mind"
Else
MsgBox "MACRO!"
Exit Sub
End If
End Sub
Anybody an idea?
If I click the hyperlink it only show the code for that cell.
The code is in the Worksheet module.
The general idea:
I'm making a workbook that calculates based on several steps (40 in total). For example: if sum is larger than 9 go to step 4, if not, go to step 21.
But because we're all human and mistakes can be made, i want excel to make a list of all the steps that have been taken and their answer. I want to make the steps in this list clickable, so when they click on the step, the can go back to that step and continue from there. So a lot of hyperlinks :)
Why not use Worksheet_SelectionChange with the same setup?
This seems to work for me, I've copy and pasted your code into the "Sheet1" module of a new Excel workbook, and put a hyperlink on the 'Sheet1' tab to click on. The MsgBox pops up as expected.
I believe the macro will only work if the code is in the Sheet module for the Worksheet that the users is clicking the link on.
If you need a Hyperlink Back button, you can always add one to the Quick Access Toolbar:
This Macro doesn't work if you use =HYPERLINK(), you have to convert it via 'rightclick', Hyperlink.

Sheet.Activate changes sheet but continues to edit data on previous sheet

I have a dialog box with a couple of buttons that launch macros to activate and change to different sheets.
The problem I am having is after I click the button, the macro activates the new sheet and I see it. But when I go to delete data, add data or try to delete a row "Nothing happens" the data on the screen is still there. If I go back to the previous sheet, the cells and rows that I had intended to delete were deleted in that sheet. It is very wierd and never seen anything like that. It appears that my macro code is note doing enough to actually change to the new sheet. I do not have this problem if I click a different sheet tab to change to it. Or if I click the dialog button to go to the new sheet and quickly do a ctrl-pgDown and Ctrl-PgUp to change from another tab and back that seems to fix the problem.
This is the code in my macro I am using to try to change to the desired tab.
Private Sub Report1Button_Click()
On Error GoTo Handler
Sheets("Report1").Activate
If StayOpenBox.Value = False Then
Unload MainMenu
End If
Exit Sub
Handler:
MsgBox "Sheet 'Report1' not found or renamed"
End Sub
Thanks for any help or suggestions
UPDATE:
Here is code that I use to call the dialog box. I have a shape on the other sheet that is assigned to this macro to open dialog box
Sub ShowMainMenu()
With UserForm1
.Show
End With
End Sub
Also there is no further code to make edits to the new sheet. My Button click simply switches to the other sheet and when I attempt to make edits manually, they are actually done on the previous sheet which is not the one I am currently looking at. So anything I do, Bold text, delete text, delete row, etc, is not done on the current sheet I am looking at, but when I return to the previous sheet the changes where made there. Im on Excel 2013, I have reproduced this problem in 2 separate files, but I will try on a different computer and older version of excel. Screenshot of my situation is below.
UPDATE 2:
I ran this xlsm file on a 2nd computer with Xls 2007 and was not having the problem. So I ran the macro on a 3rd computer that also has Excel 2013 and it is experiencing the same problem. So it is not computer specific and seems to be a problem in XLS 2013 but not in XLS 2007. I will try to find a computer with Excel 2010 to test as well, but something about this code is causing a problem in 2013 but not in older versions of excel.
When you run VBA code, it will default to using the ActiveSheet if you don't define the Sheet. When you have objects/methods that you want performed on a specific sheet, you should always specify! You can do that one of two ways:
Sheets("Report1").[Object].[Method]
'or
Sheets("Report1").[Method]
or you can pass the Sheet name to a variable and use that for shorter code
Dim Report1 As Worksheet
Set Report1 = Sheets("Report1")
Report1.[Object].[Method]
'or
Report1.[Method]
Try changing the sequence of lines in your code. I had a similar situation and it turned out that I inserted the "delete sheet" code in between the commands that were copying data from sheet1 to sheet2. When I put the deletion lines (commands) after I finished copying sheets everything started to work correctly. Many commands activate one sheet while performing and this immediately disactivates another sheet. So if you used "ActiveSheet" sommand somewhere it may be incorrectly understood - the command may be executed not on the sheet you meant.
Just use the full address for the range you are trying to manipulate, for example instead of:
Sheets("mySheet").Activate
Range("A1:B10").Cut
Sheets("myOtherSheet").Activate
Range("A1:B10").Paste
use:
Sheets("mySheet").Range("A1:A10").Cut Destination:=Sheets("myOtherSheet").Range("A1:B10")
I just had the same problem, also with Excel 2013. So even if the thread is over 9 month inactive, I want to share my solution in case somebody gets here through a Google search.
The solution was really simple. Call the userform with:
UserForm1.show vbModeless

VBA to open Excel hyperlink does not work when hyperlink generated with a formula

There seems to be a bug with Excel hyperlinks which are generated with a formula. I'm using Excel 2010. I have a spreadsheet with cells containing URLs, and my goal is to do the following two things:
Turn these cells into hyperlinks.
Create a keyboard shortcut to open these hyperlinks so I don't have to use the mouse.
To do #1, initially I just used the function =HYPERLINK(). So, my URLs are in column A, and I used this formula to make hyperlinks in column B.
To do #2, I created the following macro which should open the hyperlink with the keyboard shortcut Ctrl+H:
Sub Open_Hyperlink()
'
' Open_Hyperlink Macro
'
' Keyboard Shortcut: Ctrl+h
'
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
The problem is that this macro only seems to work on hyperlinks which are not created using a formula. For example, if I just type into a cell http://www.google.com, Excel will automatically make this a hyperlink and the keyboard shortcut macro works, where it doesn't with formula generated hyperlinks.
I've also noticed that when I right click on formula generated hyperlinks, there is no option in the drop-down menu to open the hyperlink, yet there is that option when right clicking on hyperlinks not generated by a formula.
I've found the following workaround. Rather than generate hyperlinks using a formula, I used a macro which I found here.
Sub HyperAdd()
'Converts each text hyperlink selected into a working hyperlink
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell
End Sub
I'm able to use the keyboard shortcut to open the hyperlinks generated with this macro. I'm wondering if anyone has or had a similar problem, and why the formula generated hyperlinks are not working for me. I would prefer to use formulas to make hyperlinks in the future, since it is simpler, so if anyone knows of a way to avoid using a macro to make hyperlinks, I'd really appreciate it.
I'm wondering if anyone has had a similar problem, and why the formula
generated hyperlinks are not working for me.
Alas, this seems to be painful truth: Excel does not add to Hyperlinks collection formula-generated links - below is the screen from the debugger which is pointed to =HYPERLINK("http://www.google.com/";"Google"):
I'm not sure whether this is a deliberate implementation or a bug, but yes, formula-generated links may NOT be opened using Hyperlinks().Follow method.
However, if you're going to use keyboard shortcut for links opening, just use the following code - it will automatically convert to clickable link selected cell text and open it:
Sub Open_Hyperlink()
Selection.Hyperlinks.Add Anchor:=Selection, Address:=Selection.Formula
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
Just assign any shortcut and you're ready to go) Here is the sample: https://www.dropbox.com/s/d4cie7lun22quma/FollowLinks.xlsm
Hope that's somehow helpful. Good luck!

How to shell to an exe and pass cell values from Excel worksheet

Is this possible in Excel?
I have a workbook with multiple worksheets. I wrote some vba code in a code module to shell to an exe and pass it cell values as arguments.
What I want to be able to do is select a cell or row in any of my worksheets and then call my Shell Sub while passing the values from a couple cells to the Sub. A hot-key combination would be best.
The part I am having trouble with is calling a sub in a code module from a hot hey.
Can you help with this? Any sample code?
Much appreciated!
Found my answer! It was in a post from Leith Ross found here...
http://www.excelforum.com/excel-programming/590157-keyboard-shortcut-to-run-sub-module-in-vb.html
If your macro is a Sub then its easy to assign a shortcut key to your macro. While in Excel, type ALT+F8 to bring up the Macros List. Select the name of your macro by clicking it. At the bottom right corner of the dialog, click "Options..". You can then assign a shortcut key and add a description of the macro for other to see when it is selected in the Macro List.
That's exactly what I needed.