Excel VBA - how to handle many cell references without creating a mess - vba

Suppose you have an Excel workbook and you need to write a macro that takes inputs from many different cell references, and updates values at many different cell references, how do you keep the code neat and maintainable?
It would be helpful to know:
best practices
useful tricks
unavoidable difficulties
necessary trade offs
If there are links to existing guides or discussions that would be helpful too. I haven't been able to find any.
Edit: I found http://www.eusprig.org/best-practice.htm very useful - from the European Spreadsheet Risk Interest Group (EuSPRIG).

It slightly depends om what the macro is going to do, but I use 'calculation' sheets, where I gather together the data I need for the macro, and output the results there.
I tend to do this do in defined ranges.
The result data can then be referenced from elsewhere.
Tricks: One thing I do is to create a visual 'check off' for each piece of input data as it's used. This just enables me to make sure that all the data I thought I was going to use, I have used.

Following Naming criteria is better solution , irrespictve of language.
in_name_range, out_name_range
in_num_range, out_num_range
Check points and adding watch , would surely help.
If you are going for modularity, Take care about updated ranges
(As you can't return more than one value at a time better write ranges in some predefined order in some sheet. This works for me.)

Related

Programmatically change paragraph placement in Word range

I seem to be stuck with a rather simplistic problem when writing a macro for MS Word, I even feel quite embarassed for myself.
I have a range of paragraphs (content don't matter here) that need to be sorted by a given criteria (sorting is not the case here as well) - p1-p2-p3
After sorting let's say we'll have p2-p1-p3
I seem not to be able to code the very process of paragraphs exchanging places e.g.
The usual approach for variables does not really apply to Word's objects.
I'm not really looking for a code sample (but will look into ofc), but for a hint of what to approach to use. Thanks

How can I access data that changes locations and header names across workbooks?

I am trying to access data that changes locations and header names across different workbooks. This is part of a bigger project but this is the only part I cannot figure out. I have attached images to help clarify what I mean. Let's say that I'm trying to access the range that contains the Net Worth/Balance so I can do some calculations with it. How do I do this if the headers change across dozens of workbooks? On top of that the headers aren't always the first rows with any writing on it. Sometimes there is info in the upper rows that are needed for the workbook but not my purpose.
The programmer in me wants to use a dictionary and find all variations of the "Net Worth" title that exist but is that the most efficient way? Should I just use a super long if then statement?
Thanks in advance for your help!
****Also if anyone is wondering if I should even use a macro for this please remember this is part of a much larger project where there are many calculations and they are repetitive across workbooks. My only difficulty is finding where the data is in each workbook.

VBA Code Standardisation Practices

I've been applying VBA at work for a number of purposes. I have noted the more 'clicks' a user has to do for a form (with a number of macros), the higher the rate of error. I was wondering rather than having 3 separate functions requiring 3 separate clicks - should I bunch all these functions together into the one sub module for ease of use?
Clinical staff have indicated that this would work best with them, but in terms of code optimization and keeping different functions separate - wouldn't it make things very messy? As I'm still in the process of learning VBA, I thought I'd turn to the expert community which has helped me out a lot so far.
I'd appreciate any and all comments regarding your thoughts on this and how I can create best-practice VBA standards to adhere towards.
An example of this would be the static copy function I have created and the monthly Calculation function. For the monthly calculation to be completed it pulls data from a summary tab and compares this against the static data. However for the static data to first be captured - the user needs to click the static data macro. I've separated the static snap-shot function and the monthly-report function but was wondering if I could instead combine both of these into one function. Readability wise it's not problematic (as I have the comments explaining each section) - but standard wise, would it be unwise?
I apologize if this question is somewhat broad in nature.
After reading around a few forums I've picked up the following information:
Do not use global variables unless you have a specific reason to do so
Don't forget to initialize variables else you may run into sub-script errors
Keep your code simple (iPhone Approach - one click approach)
If modules are similar consider grouping the functions together into one
Macro recorder is your friend
Thanks for the tips everyone!

Looking to build a lineup builder for the site draftkings using excel

Why excel? Well excel is what is used to import the player salaries.
Now I need the spreadsheet to do the following.
Create teams within salary cap.
Include/exclude specific player function
Build multiple lineups from a selected list of players within the cap
Can I do all of this with excel? or do I need to know excel vba as well?
Also which parts of excel or if necessary excel vba must I need to know to code such a thing? Also if someone could give me a short summary of the steps needed to hypothetically make such a thing it would be great. Thanks.
I'm posting this as a reply because it is too long for a comment window.
Just because Excel has a grid doesn't mean that it is fit for data storage and data handling, on the contrary.
What you typically want to do is create a transparent structure that guarantees the integrity of your data and that allows dynamic portability when needed one day.
Excel is meant to be a spreadsheet, people forget this all the time or they just avoid the topic: although Excel has a grid, doesn't mean that it is a good fit for reliable data storage. It is not even the least complex way of storing data, depending on the amount of VBA that you need to manage all these data and the gates that you unnecessarily open towards potential bugs.
This is why an RDBMS is what will fit your needs, in this case Access would be a good option as it preserves your data integrity if you get the table structure right and it executes a lot of tasks for you that you should otherwise need to program yourself to protect the integrity of your data.
Although you can perform SQL on spreadsheets too, note that Excel does NOT cover related tables (what you typically seem to need for building your teams and salary limits), so what many Excel programmers will typically do is to create their own code to make this cross-table data storage thing work.
Don't do this if the alternative is available and much more reliable and future-proof.
At first sight, it seems that you won't even need any VBA; I'm not sure of all the things you want to do, but my first impression is that you can manage everything with SQL syntax and stored queries in MS Access. You can import Excel sheets into Access if you get their format right so that should not be a problem.
Once your data is stored there, rest assured that you have made your life a lot easier.

Import Excel Workbook contents into a data table (Visual Basic .NET)

I am trying to perform an operation that might be quite simple really, but since I don't have experience in doing this prior to this point, I may be missing something.
I have an Excel file that needs to be imported into a data table in Visual Basic (.NET 2010). Now, I am trying to import as a data-table, so that I can have the data flexible and ready to be used as and how I wanted to.
The Excel spreadsheet doesn't exactly have the most promising and worthwhile formatting, which makes this situation a tad-bit difficult. The column headers are not on the first row itself, but are on the 5th row and this might not always be the case. The column headers might even end up on the 10th row or even the first row - Any help on this would be great too.
The name of the worksheet can also be different, since I don't have any control over the end users that will be using the application. Any suggestions or code-help here please?
You will need to deal with the formatting issues to have true success. A consistent input is the only way to secure consistent output. Otherwise, you will need to build an interface that allows your users to map fields as they go.
Once you have that taken care of, I suggest using the OpenXML SDK to import your values ( http://msdn.microsoft.com/en-us/library/office/hh298534.aspx )