|
Moving around the Worksheet in VBA for ExcelUsing Named Fields, Variables and other Advanced FeaturesIn this fifth part on moving around the worksheet are examples that you might never need, But when you need them they are hard to come around. Special Addresses If you name some fields (nfMyField) you must still use the quotation marks within the parentheses: But if you use the content of a variable as address don't use the quotation marks: You can also use an address that you have stored in another cell of another sheet. For example let's say that in cell A1 of the sheet "Data" you have entered "C12". The following code: Use the same approach for Rows and Columns If a block of cells is selected (like G5 to M25), this block of cells is the Selection and has adresses of its own within. For example: Special Formulas Sometimes part of the formula must vary depending on which cell is selected. You need the line number of a formula to be variable. For example in cell G8 the formula must be "=F8", in cell G9 the formulas must be ""=F9". Try this code: If when the selected cell is G8 you want the formula to look on row up "=F7" you will use: The entire address can also vary. When in cell G8 the formula should be "=SUM(G1:G7)". You will write: You can also make an address vary depending on the month: Advanced Examples on Ranges, Columns and Rows You are adding lines of data to an excel database. You want each new record to be pasted in the first empty line. You first go to the title cell of the first column, you count the number of lines using a variable, you offset and you paste: You want to do the same thing from the cell that is selected: If you want to develop an formula with an "If" to check if cell A1 is empty you would not write: You need a small VBA procedure to go down a column and delete the entire row if the cell is empty. You need a small macro to go down a column and delete the entire row if the value of the cell is 0. See both and 20 more of the same nature in "vba-short-macros-1.xls" one of the 25 spreadsheets that comes with the downloadable tutorial. Discover more tips |