Extending the number of reference cells passed to .Range function in Excel VBA - vba

I am using worksheet.range("Relevantcells") to copy cells in a sheet. I have non-contiguous cells and hence it only lets me put in 60 specific cells.
How can I extend this to many more cells that I want to read from?

Instead of having one large non-contiguous range, you could create an array of smaller ranges in code and then copy each range iteratively.
You'll likely take a performance hit over copying the ranges en masse, but this approach should be more flexible.

Related

Copying named ranges that contain a certain value to another worksheet with VBA

I have a worksheet with 100 to 200 named ranges. I would like to search each of the named ranges en when they contain a certain value in any of the cells in the range, the range should be copied to another worksheet.
I am new to VBA. So any help is useful. Always nice to learn a thing or two :)

Trying to use excel formula or VBA

I have two different workbooks. Book 1 and Book 2 both have the same number of columns A through M.
I want to do match the records between two workbook, for example: I have a column A name Birthday, Column B City, Column C Passport Number......., in worksheet 1 & 2. I want to match worksheet 1 Cell A1 from the Range A:A worksheet 2, If the record in column A cell 13 not matching it shows Birthdate not match in N13 Workbook 2, If it does not match with worksheet 1 Cell 13 from the Range B:B worksheet 2 it shows city not MATCH in Column N 13 in workbook 2, and so on till column M.
I am using the formula below but it's not working properly, I don't know what I am missing and what formula should I add in. I have no idea about VBA. But I want to see is it easier to do by using excel formula or vba?
IF(COUNTIF(Target!$A$2:$A$5964,Source!A8)=0,"Birthday",IF(COUNTIF(Target!$B$2:$B$5964,Source!B8)=0,"City",IF(COUNTIF(Target!$C$2:$C$5964,Source!C8)=0,"Country",IF(COUNTIF(Target!$E$2:$E$5964,Source!D8)=0,"Passport Number Mismatch in Target",IF(COUNTIF(Target!$F$2:$F$5964,Source!E8)=0," Travel Date Mismatch in Target",IF(COUNTIF(Target!$G$2:$G$5964,Source!F8)=0,"First Name Mistmatch in Target",IF(COUNTIF(Target!$H$2:$H$5964,Source!G8)=0,"Full Name Mismatch in Target","Match in Target")))))))
Thanks in Advance.
VBA has access to these same worksheet formula functions (e.g. COUNTIF): there really aren't column or matrix functions that VBA has that formulas don't have.
However, VBA lets you write loops (e.g. while, for), it allows if-statements, procedure calls, and many lines of code so your calculations can have more steps and hence be more complex. VBA also lets you have temporary space in the form of arrays (and strings and objects, too) (so you don't necessarily need to use columns for temporary space as one might do with formulas). VBA also allows recursion, which makes some calculations easier (to some definition).
VBA provides an imperative programming model. VBA procedures can read and write any cell of the spreadsheet. Imperative programming, on the other hand, needs to be triggered somehow such as by using a button.
By contrast, the data-flow programming model with formulas will automatically recalculate whenever their input sources change, which is good. But there are some cases it doesn't handle naturally (e.g. recursion).
Another option is to combine VBA with formulas, by writing new formulas that are then implemented in VBA. If you are doing that, the VBA can only return information thru function return values; it cannot otherwise modify the spreadsheet.
So, if you can think of how to do this easier using loops (and arrays) or recursion and maybe with a button to trigger the computation (or by using custom formulas) then VBA might be interesting.

What range is calculated in Excel when using VBA .Calculate?

If I use:
Sheets("Sheet1").Calculate
Is every cell on Sheet1 calculated, or only cells in the UsedRange?
I'm writing a subroutine that recalculates a variable number of rows and I'm ultimately trying to determine if my calculation will be faster using:
Sheets("Sheet1").Range("VariableNumberOfRows").Calculate
Or
Sheets("Sheet1").UsedRange.Calculate
There are other rows outside the variable number of rows (and obviously inside the UsedRange), but these do not contain formulas therefore doesn't matter if they're included in the calculation.
If there's a technical article on what .Calculate specifically calculates (which cells, types of formulas) that would helpful as well.
Thanks!
Clearly the answer is no, it does not calculate cells outside the used range. There would be nothing there, so there is nothing to calculate. Excel tracks the cells used (it has to in order to report the .UsedRange).
Use .UsedRange if you want all cells on the sheet that contain formulas to be calculated, or specify a smaller range if you only want that range calculated.

How do I select specific cells in VBA?

Basically I need to select specific cells on an excel sheet in vba. For example the code should be able to select a5, a10, a15. I do not want the cells in between them, just the ones I listed. Is there a specific function that will do this? It seems like .Range could only take start cell and ending cell.
You'd use this: Range("A5,A10,A15").Select
To add additional cells, just use more commas.
Alternately, you can utilize the Union method to join multiple range objects into a single Range object.
Note that it is generally not a good idea to select cells in VBA as nearly everything can be done without selection. This is a frequent mistake made by people new to VBA due to generated macros (Record Macro) recreating what you do rather than the result you want.

Clear cells out of the used range

In one of my macros I loop trough used range, but the used range is way bigger than the actual used range, therefore the macro is impossible to run. I think that cells outside the actual used range may have other impacts on my code such as error "too many cell format" with only one cell.
How can I clean cells outside my used range so that they are not taken into account in my Used range ?
The Used Range not truly reflecting what is the actual used range is a common issue and just requires some basic cleaning up of the sheet.
Check out this on the Microsoft Support site: http://support.microsoft.com/kb/244435
You can delete everything outside of your actual used range.
You can either do this manually with selecting a row and ctrl+shift+down (and ctrl+shift+right with a column selected) or you can do it programatically by identifying where your data actually ends (either via a COUNTA or xlRight style of command) and then delete everything after those points.
The manual way is good for if it's just a one one off cleanup operation, and if you're doing it in the macro then you may as well just use the method for determining the range to keep to go straight to selecting it, and not bothering to delete outside of the range.