vba to transfer code from module vba project to excel sheet vba project - vba

I've developed an excel addin that imports sheets into the existing one. i would like to be able to double click anywhere and be able to jump back to the first sheet.
Private Sub Worksheet_BeforeDoubleClick(ByVal target As Range, cancel As Boolean)
Sheets(1).Select
cancel = True
End Sub
Another program writes a summary excel file. I've developed a add-in macro that when opened and run, Imports the full solution sheets and do some data manipulation. is there a way to transfer this code into the excel sheet vba project? thanks in advance

use workbook events
so type in ThisWorkbook code pane the following:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Sheets(1).Select
Cancel = True
End Sub

Press Alt+F11 to go to VBA development tab and then in the tab on the left there should be displayed your Excel worksheets.
Simply double click on the worksheets you want to apply that feature and paste the code :)
See this image for more details about where to click:
https://www.extendoffice.com/images/stories/doc-excel/doc-drop-down-list-prevent-paste/doc-drop-down-list-prevent-paste-1.png

Related

initialize a workbook to open to the same sheet every time when opened

I have a workbook in excel that has an accumulating number of sheets. I understand the excel document will open to the last sheet you were on. But is their a way to make the workbook open to the same page every time? (it's ok if I need to use VBA) thank you in advance for any help.
Excel's Workbook_Open() event fires too soon in the loading process for some methods or properties of the workbook itself to be reliably used. Just activate the sheet that you want to return to in the BeforeClose event handler.
'In ThisWorkbook
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Worksheets("SheetWhatever").Activate
End Sub
Include a Workbook_Open event within the code module for ThisWorkBook
Private Sub Workbook_Open()
'Instead of "Sheet1", use whichever sheet you want to activate
Worksheets("Sheet1").Activate
End Sub

Excel VBA DoubleClick

I'm working on a work project where I have an excel sheet with values that turn red when they are out of spec. What I'd like to do is be able to double click on a cell and have the sheet in my workbook pop up that has trending data on it. I have already created the sheet with the graph on it. Long story short, I'd like to be able to double click on a specific cell and have it bring up the corresponding sheet.
I have tried this code, and it will not work. Is anyone able to maybe write code from scratch or alter the code so I could use it? The cell I'm trying to click on is N9, and the sheet I want it to open is called "Alpha Final Rinse"
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
Sheets("Alpha Final Rinse").Select
End Sub
I'm doing this in Excel 2013. Thank you!
If you only want N9 to be able to switch focus to another worksheet, isolate Target with the Intersect method.
In the data worksheet's code sheet:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
If Not Intersect(Target, Range("N9")) Is Nothing Then
cancel = True
Worksheets("Alpha Final Rinse").Activate
End If
End Sub
Note that cancel = True is necessary to stop the user entering in-cell edit mode (assuming that has been enabled in Options).
Your code will work if:
it is installed in the worksheet code area of your data sheet (the sheet whose cells you are double-clicking)
macros are enabled
the file type is .xlsm rather than .xlsx

Excel Hyperlinks and VBA

I have a Data Validation list
which refer to my various worksheets
I would like to be able to select the worksheet I wish to view / jump to and use a VBA button to take me to that worksheet.
Is this possible please?
PS: I would like a single button to go to the sheet selected from the dropdown
In the VBA Project window double click the Worksheet that has your validation list. This will open the that Worksheet's code module. Paste this code and change Range("E1") to the correct cell address.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Taget, Range("E1")) Is Nothing Then
On Error Resume Next
Sheets(Range("E1").Text).Select
On Error GoTo 0
End If
End Sub

Start macro by double click

I'm trying to start a macro through double clicking a cell.
I copied this code directly from the internet, but it is not working when I double click the cell C40.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Application.Intersect(Target, Range("C40")) Is Nothing Then
Cancel = True
SelectPayFreq.Show
End If
End Sub
Make sure the code is in the worksheet code area:
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
make sure Macros are enabled and Events are enabled
The code for worksheet events must be placed in the module for a proper worksheet not in a regular module.

How to run automatically Excel macros?

I have created a cricket sheet in excel. I have also created a macro which is used to display a message when overs bowled are equal to 20 (A T-20 Match). My problem is that when the over get to 20 no message is shown unless I press the shortcut key. Is there anything which I can do so that whenever overs reach to 20 a message automatically displays.
My code is:
Sub Innings()
If Range("H43") = 20# Then
MsgBox "Innings Completed"
End If
End Sub
Include the following macros in the worksheet code area:
Private Sub Worksheet_Calculate()
Call Innings
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H43")) Is Nothing Then
Call Innings
End If
End Sub
Because they are worksheet code macros, they are very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try them on a trial worksheet.
If you save the workbook, the macros will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macros:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
Gary's Student's code is working well. You just need to write Worksheet_Change and Worksheet_Calculate to the List1 not to the Module1. I add a picture.