open custom ribbon in other workbook VBA - vba

I am writing some vba in excel to create another excel report.
This report needs to have adjusted the header /footer, which my company have through their own custom ribbon in Office.
If I am in my main workbook, I am able to activate the ribbon by following code:
Application.SendKeys ("%HY2%")
But after I created my excel report it seems like the custom ribbon is not given time to activate or something.
I then call the report like this
code:
Workbooks.Open Filename:="C:\Users\ccc\sss.xlsx"
and then try
code:
Application.SendKeys ("%HY2%")
Then it is possible to call the Home tab, but not the custom one.
I have tried to delay the code using wait function, sleep function and Do While (check if ready)
But during all these, it seems like excel tabs is on "hold", it is not activating the custom ribbon. Therefore my code opens the Hometab, but it cannot find "Y2" Ribbon.
I have tried activating the main workbook to do some random code, then going back again and activating the report again, but no...
Can anybody help please?

Related

VBA code to find value based on seek value

I want to write a piece of VBA CODE that will goal seek the variable value into the static value based on the input value
!
ie. something along these lines but i manually did these,
!
OK, here is what you have to do. First, you set the layout of your worksheet looks like this:
Here I use Sheet1. Please make changes accordingly if necessary.
In order to use Solver add-in, you must first make sure that the add-in is installed. For Office 2013 and later:
Click the File tab, and then click Options below the Excel tab.
In the Excel Options dialog box, click Add-Ins.
In the Manage drop-down box, select Excel Add-ins, and then click Go.
In the Add-Ins dialog box, select Solver Add-in, and then click OK.
After that, you must set a reference to the add-in in the workbook containing the code Visual Basic Editor (VBE) that calls the add-in's procedures. Click References on the Tools menu (see pictures below), and then select Solver under Available References.
Now, go to worksheet 1 code module in VBE. Just click Sheet1 twice in VBE and paste the following code into it:
Sub VBASolver()
SolverReset
SolverOk SetCell:="$F$2", MaxMinVal:=3, ValueOf:=0, ByChange:="$B$2:$B$4"
SolverSolve True
End Sub
You can run the loaded VBA program by clicking Run icon below Debug menu or pressing F5 key on the keyboard.

Hiding or not displaying excel VBA UDF module/code

I wrote a simple UDF in Excel VBA. I saved it as an Add-in and imported it in so that I can use the function like any other excel function. But whenever I open an Excel workbook and subsequently VBA window to create a macro, the module I wrote is displayed in the Project Explorer window and its code in the coding window.
Is there any way I can hide or not display every time I open VBA?
Please let me know if you need more information. Thanks in advance!

Excel quick access toolbar - run macro from active instead of previous workbook

I have a custom button (not an add-in, just a "shortcut" button) in Excel's quick access toolbar to run a macro in the active workbook. The workbook is modified and saved daily with a new filename. When the link is first created in the active workbook (call it Version 1) the macro runs fine. However the following day (filename now Version 2) clicking on the macro button opens workbook Version 1 and runs the macro saved in Version 1.
Is there a simple way to break the link and run the macro in the most recent active workbook?
The code is basic - it just opens up a userform and is saved in "ThisWorkbook"
Sub OPEN_DATA_USERFORM()
ufDATA.Show
End Sub
I will try to guide you through this, I hope it helps.
What we need to do is we need to call OPEN_DATA_USERFORM() from our "PERSONAL.XLSB" file. In VB editor screen, in the project explorer you will find a file called "PERSONAL.XLSB", you need to add an "Module" to that and add following code:
Sub KickOff()
Call Application.Run(Excel.ActiveWorkbook.Name & "!OPEN_DATA_USERFORM")
End Sub
By this we will be able to call the userform.show function from our PERSONAL.XLSB which is always running in the background when you open the excel.
PS: I am assuming your OPEN_DATA_USERFORM() is coded in your daily workbook.
UPDATE: PERSONAL.XLSB
Your file should be visible in your project explorer like below:
If it is not there record a dummy macro and select your personal file like below then it should appear in your project explorer in VBA screen:

Decide which ribbon tab to be selected

The program, that I'm working on, has a separate (additional) Add-in tab in the Excel ribbon. And there are our custom controls, etc. Some of the controls open a new workbook (new Excel window).
In Excel 2010, when the new workbook is opened, our custom tab is selected by default. But in 2013, the first (Home) tab is selected.
In the project, there is no code that controls which tab to be selected by default in a newly open workbook. That is why I am wondering it works in 2010, but not in 2013?
I researched about that, but I could find mostly articles about "how to create new tab", etc.
Any ideas and suggestions are welcome.
This should be the fix you're looking for:
yourRibbonInstance.ActivateTab("tabID")
keep in mind that the id of the tab might not be the same as its displayed name.
Not sure if this might be overkill but you could use a macro that runs on the workbook open event to activate the sheet you want to see first..
Sub workbook_open()
Sheets("Sheet2").Activate
End Sub

Excel VBA - Call macro using add in

I have added a toolbar menu for my macro, so I can just press the button and it runs my macro from any excel document. Every time I run the macro though, it opens the source file containing the macro. Is there a way that it won't open the source file and just run the macro? (even opening and closing wouldn't too much of an issue, but I'd prefer not opening it at all)
I haven't been able to find any information about this, so all help to get me started is appreciated.
You can't pull code out of the air to run it; Excel's going to have to open the file the code's stored in to run the code.
If I understand it correctly, you can create an Excel add-in from your code and have the add-in load automatically when Excel starts. Your code will always be available in that case.
This seems like a good place to start:
http://msdn.microsoft.com/en-us/library/aa140990(v=office.10).aspx
USE YOUR PERSONAL MACRO WORKBOOK
Open the VBEditor and find the module containing your macro.
Copy that code.
Now in the VBProject Panel on the left, locate your PERSONAL.XLS or PERSONAL.XLSB project. This is the project where you store macros you want available at all times. Add a module to that project and put your code into it.
Now update your "button" so that it points to the macro in that workbook and you're good to go.
On the off chance your PERSONAL.XLS project does not exist, use the macro recorder to record a "junk" macro and be sure to set it to "Store Macro In: Personal Macro Workbook"... that will create it for you the first time.