VBA code to find value based on seek value - vba

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.

Related

open custom ribbon in other workbook 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?

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

Standalone code for Excel

Can VBA code be written to perform actions on any Excel file?
When I create a project in Visual Studio, it asks for an Excel file to be linked to it. All the code that I write is in ThisWorkbook.vb and hence acts only on the Excel file linked to the project.
Ideally, I want a script that:
When the user double-clicks, he/she should be allowed to select an Excel file of choice for the actions to be performed on that file.
If the above is not possible, I'd at least like to invoke the VBA script from within an Excel file.
Basically, the VBA code should be independent of any Excel file.
I looked at VBScript, but the syntax for it seems to differ slightly.
You've mentioned Visual Studio, VBA, and VBScript. The solution I'm outlining works directly with VBA rather than Visual Studio or VBScript. (However, you might adapt Visual Studio (C# or VB) along the lines of what I'm outlining below.) Hope it's helpful, so here goes:
Here's what I do, and, it ultimately results in an .xlam Excel AddIn as #chris above has commented.
Overview
I start with ordinary .xslx workbook, to which I add a VBA project, making it an .xlsm. Then create some VBA Subs (macros). Then create some Excel QAT (Quick Access Toolbar) buttons for the workbook, which are bound to (i.e. they invoke) the VBA subs/macros. Then convert the workbook (with VBA in it) to an .xlam, which is an Excel AddIn. When you're all done, the buttons are accessible from any workbook (and the VBA code has access to any user workbooks as well as those originally in your .xlsm). Further, the workbook associated with the .xlam is invisible. So it just looks like you've added some buttons to the QAT that appear on all users .xlsx windows. The .xlam is pretty easy for users to install (though I provide a buttons to uninstall/reinstall/check version). You can upgrade an .xlam independently of users' workbooks; users' workbooks can thus be data only (.xlsx, no VBA).
Details
Write some Excel Subs you want to use later
You need to be aware that the buttons can only invoke macros (VBA Subs) without parameters, so the macros will have to check things like ActiveSheet and ActiveWorkbook and Selection to figure on what sheet the button was pressed, hence what user data to really operate on. (If you need to refer to your workbook with the VBA code in it, use “ThisWorkbook”). You should be aware that there can be naming conflicts, so try to name the parameterless subs with rather long names, such as MySomewhatUniqueProjectName_button1, etc…
Add Buttons to your .xlsm
Using Excel 2010 (I think this works with 2007 or later), I put workbook-specific buttons on the QAT part of the ribbon, which connect to macros (VBA subs) in the VBA code.
To do this, you from the Quick Access Toolbar customization drop down (the tiny down arrow at the very top row of the Excel window, the last icon from left to right) choose "More Commands…". When the “Customize Quick Access Toolbar” dialog box comes up, from the (2nd) "Customize Quick Access Toolbar:" heading (top to the right), choose "For XYZ.xlsm" from the dropdown instead of the "For all documents (default)". And from under "Choose Commands From:", use "Macros" (instead of “Popular Commands”) from the dropdown. Once you have those two things selected, you can move VBA subs from the left box to the right box using “Add >>”. Each so moved will become buttons visible in your QAT. As you’re doing this you can also edit the icon and text for the buttons, add a separator as needed (I always end with a separator in case other .xlam’s use the QAT). (Now is a good time to save this .xlsm.)
Convert the .xlsm into a .xlam
Then I convert the .xlsm to an Excel add-in, by merely saving it as an .xlam file. This will end up (1) hiding the workbook associated with the code you have (though it is still accessible to itself.). Further, now, (2) the (invisible, as now it's an .xlam) workbook will load whenever Excel is loaded. (To keep this fast for when users use Excel but don’t run my VBA code, I don't run any code when the .xlam is loaded, I only run code when a button is pushed.)
You can manage the AddIn using Excel’s AddIn manager. To update the AddIn, you have to use some trickery. While you can copy over it when Excel is not running, on the other hand, you cannot directly uninstall the AddIn, you can only disable it from Excel. But once disabled, you can delete the .xlam, and relaunch Excel, go to the AddIn manager to try to work with the (now gone) AddIn and you’ll get Excel saying it can’t find it, so do you want to delete it. Say yes, and it will be uninstalled.
FYI, Notes
I keep the .xlsm to edit later, but you can actually debug and edit the .xlam and later convert it back to an .xlsm with a minor bit of trickery: find its "ThisWorkbook" entry in VBA, and then the "IsAddIn" property, set to false, its workbook will suddenly appear and you can save as .xlsm, or edit its workbook and set the property back to true to resave as .xlam directly.)
Answer 1
You can do that in VB.Net too. Just make a regular project. (comment by #SLaks)
This worked for me very well and was exactly what I was looking for.
Answer 2
The very descriptive answer posted by #ErikEidt
I haven't tried this, but it seems like a good and alternative way of getting macros to work.

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.

Disable Ctrl+V paste option from Excel template in VSTO

I am working on an Excel template + VSTO application.
I have assigned various custom validations and formats for various cells.
But whenever I copy and paste something into the cell, these validations do not work (fails completely). Is there any way so I can disable (Ctrl + V) paste options from the Excel template?
I think it can be implemented in VBA macros.
But I'm not sure about it.
Have you tried using Application.OnKey? The following code should intercept Ctrl+V and effectively disable it. You should call it when your template opens.
Application.OnKey("^v", "");
If you would like to restore Ctrl+V, call the following:
Application.OnKey("^v", Type.Missing);
In VBA you can put the name of a sub in the second parameter, which will be run when that key is pressed. I'm not sure how that works in VSTO.
Of course, the user could still use the cell menu or the edit menu to paste. Also, be aware that this will affect all workbooks running in the Excel instance.