Daily tips for using Microsoft Excel.

Saturday, July 13, 2002

My tip of the day is from a bit of my own experience. As you are learning to write code, you've probably learned that you can radically speed up your macro by turning off the screen updating while the macro runs. You would add this line of code to your macro:
Sub Test()
Application.ScreenUpdating = False
...macro code here
Application.ScreenUpdating = True
End Sub

In Excel 2000 and prior, it was not required to turn ScreenUpdating back to true. When the macro finished, Excel would always revert back to turning the screen updating back to true. When I was writing macros for that platform, I would often leave off the ScreenUpdating = True because I knew Excel would handle it for me, and it solved the hassle of having to worry about it this macro was called by somewhere else that did not want the screen updating turned back on or not.

Now that those macros are being used in Excel 2002 - we have a problem. Excel's 2002 policy is that the macro has to turn screen updating back on. On my machine, it is only a problem if I halt a macro during debug, but on other machines, the screen updating stays off, leaving to some very confusing situations. I like things to be predicatable, and this certainly isn't!

So, the tip of the day, whether you are writing macros in Excel 97, Excel 2000, or Excel 2002, always turn the screen updating back to true with
Application.ScreenUpdating = True

Friday, July 12, 2002

Phillip asks: I made an spreadsheet for the the tax rates in Kansas. The rates for the whole state changed and I need to add .4 to every number in my spreadsheet. is there an easier way than manually?

Yes! There is a cool trick with Paste Special Add.

1) Find a blank cell in your spreadsheet.
2) Enter 0.4 in that cell
3) Select that cell and do Edit > Copy
4) Now, select every tax rate in your spreadsheet.
5) From the menu, do Edit > Paste Special ...
6) In the paste special dialog box, select Values in the top section, and "Add" in the Operation section. Click OK.

This will add the contents of the clipboard (0.4) to every number in the selected range.

Monday, July 08, 2002

Jac asks, I'm generating tables, same number of columns but each time different number of rows. I can determine without problem the last row and the last column to make the calculations I need in a macro. Now the purpose is to add in this macro the possibility to name the table (with the same name each time), but in the macro the cells range associated to the worksheet is R1C1:RxCy, I don't know how to write the range using the values lastrow and lastcolumn (which represent the last column and row of the table) with the RC system (I don't even know if it's possible).

Most VBA programmers would use the following method, which uses the Names.Add method.

Sub Method1()
LastRow = Cells(65536, 1).end(xlUp).row
LastCol = Cells(1, 255).End(xlToLeft).column
MyArea = "='" & ActiveSheet.Name & "'!R1C1:R" & LastRow & "C" & LastCol
ActiveWorkbook.Names.Add Name:="TheData", RefersToR1C1:=MyArea
End Sub

There is an easier method for assigning a range name. The following four statements are all equivalent. The last two are vastly easier:
ActiveWorkbook.Names.Add Name:="TheData", RefersToR1C1:="=Sheet1!R1C1:R10C5"
ActiveWorkbook.Names.Add Name:="TheData", RefersTo:="=Sheet1!A1:E10"
Range("A1:E10").Name = "TheData"
Cells(1, 1).Resize(10, 5).Name = "TheData"

Jac - I would adapt the last line for use:

Sub Method2()
LastRow = Cells(65536, 1).end(xlUp).row
LastCol = Cells(1, 255).End(xlToLeft).column
Cells(1, 1).Resize(LastRow, LastCol).Name = "TheData"
End Sub

I learned this shortcut for assigning range names from Bovey & Bullen's Excel 2002 VBA Programmer's Reference. The 2002 version of this book runs circles around the 2000 version. For my money, any Excel VBA programmer needs that book at his right hand. My copy is dog-eared and never out of reach. Buy it here to save $12 off the $39 cover price.

Sunday, July 07, 2002

Richard writes
If I may be so bold to ask, I should be grateful if you could enlighten me as to the answer to this perplexing conundrum on syntatical integrity
videlict:

when referencing a cell for external use in VisualBasic for example via "Range("F3").Select" suppose that I wish instead of the column 1 to substitute a variable from a loop to the following end:

For Scarab=3 To 15
Range("FScarab").Select
ActiveCell.Value=Scarab*Scarab-3
Next Scarab

for instance.

Clearly, one can not just use FScarab where before one would have had F3 but perhaps there is a method via which the desired result can be achieved. That is, is there a way to refer to the cell in column F and row Scarab for the variable Scarab and if so could you divulge this information.

There are several ways to do this. Probably the most intuitive is this method which involves concatenating the column letter "F" with the numeric row number inside of the Range() reference.
For Scarab=3 To 15
Range("F" & Scarab).Select
ActiveCell.Value=Scarab*Scarab-3
Next Scarab

The next method uses the CELLS(row, column) function instead of the Range reference. In this case, row and column need to be numeric. Your row number variable already is numeric, so you simply have to convert the "F" to a 6 in your head. You will also note that it is not necessary to select the cell, you can simply assign a value to it.
For Scarab=3 To 15
Cells(Scarab, 6).Value=Scarab*Scarab-3
Next Scarab

Finally, it is possible to simply loop through each cell in the range:
For each cell in Range("F3:F15")
Cell.Value = Cell.Row * Cell.Row - 3
Next Cell


Saturday, July 06, 2002

Bill asks Today, I've been working in Excel. Every time that I copy data from another application and paste it into Excel, the data is inexplicably being parsed into two columns. What is going on?

You've uncovered something that is either a really cool trick (once you understand it), or the world's biggest annoyance (if you don't know what is causing it). Here is how the auto-parse works and how it gets turned on.

Open a blank worksheet. Copy these lines to cell A1 on the worksheet:
Column 1 - Column 2
Bill - Jelen
Frank - Smith
Anne - Troy

To parse that column into two columns, you would highlight A1:A4 and from the menu select Data > Text to Columns.
In step 1 of the wizard, indicate the data is delimited.
In step 2, click the Other checkbox and indicate the the data is delimited by a dash.
Click Finish to parse the data.

Here is the trick: Until you do another Text to Columns routine, the "-" is remembered by the Text to Columns dialog for that worksheet. You can close the worksheet, open it a month later and the Text to Columns wizard is going to remember the dash was your preferred method of parsing data.

When there is something in the other field of the Text to Columns Wizard step 2, Excel will turn on auto-parse. You can then copy data from another application (notepad, for example). When you paste to the workbook, if Excel finds any dashes, it will auto parse the data into two columns. To try it out, copy these lines to A5
Duane - Aubin
Ivana - Taylor
Scott - Pierson

To turn off the feature, you need to do another Text to Columns and uncheck the Other box in step 2.

Friday, July 05, 2002

Kári asks, I have a formula where I have to put in a criteria. The criteria is, that the formula has to gather numbers that are bigger than 0,5 (>0,5), but not bigger than 2 (<2). But how do I do that? I have tried: ">0,5"&"<2" and a lot of other combinations, but nothing works.

Kári: For a SUMIF or COUNTIF formula with 2 conditions, you need to use an array formula.
This type of formula is discussed here: http://www.mrexcel.com/tip031.shtml.

Since I wrote that article a few years ago, a better version of the formula has come to light. The web page discusses using this formula for a CountIf with 2 conditions:
=SUM(IF($C$2:$C$4403>0.5,IF($B$2:$B$4403<2,1,0),0))

You can use boolean logic instead to write this formula for CountIf
=SUM(($C$2:$C$4403>0.5)*($C$2:$C$4403<2)*1)
or this formula for SumIf:
=SUM(($C$2:$C$4403>0.5)*($C$2:$C$4403<2)*($C$2:$C$4403))

Remember, you must hold down the Ctrl and Shift keys then hit enter to enter these CSE or Array formulas.