I have recorded macros in the attached file to highlight cells that contain matching initials by using conditional formatting on the first cell and copying it to the remaining cells. The problem is that if rows are inserted above that change the first cell reference then the macro no longer works properly. Is there a way to use a relative cell reference within the macro? The macro buttons are in cells A4 to A13. I have no VB experience and created the macros by recording keystrokes and using tips found in this forum. If there is a more elegant way to accomplish this please feel free to advise.
Thanks
Last edited by hillbk; 05-11-2009 at 02:09 PM.
If I understand your problem, instead if defining RangeStart as =$B$19, define it instead as =INDEX($B:$B, ROW($B$18) + 1). That means it always starts one row beneath the header.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Thanks for the help on the Range name but I don't think it fixes the underlying issue. I'll try to be more specific. For example, when I run the "PLD" macro I want it to examine the contents of each cell and highlight only those cells that contain the string "PLD". The macro I have currently written peforms conditional formatting on each cell based on whether the PLD string is found using the SEARCH command (=SEARCH("PLD",B21,1)>0). Thus the formatting for cell B21 refers to B21 and the formatting for cell C21 refers to C21 in the search command. If I use RangeStart as my cell reference in the macro, the formatting in every cell refers to B20. Is there a way to use the INDEX command so that when I copy it to another cell the reference becomes RangeStart +1, and then RangeStart + 2, etc.?
Thanks
You want cells that contain the "PLD" highlighted?
Select A1 and do Insert > Name > Define Ductwork!Me refers to =A1 (no dollar signs). Now Me always refers to the cell in which the reference appears.
Then change the code like this:
Sub PLD() With Range("RangeStart").Offset(1, 0) With .FormatConditions .Delete .Add Type:=xlExpression, Formula1:="=SEARCH(""PLD"",Me,1) > 0" .Item(1).Interior.ColorIndex = 36 .Add Type:=xlExpression, Formula1:="=SEARCH(""NA"", Me,1) > 0" .Item(2).Interior.ColorIndex = 15 End With .Copy Range("RangeStart:RangeEnd").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False End With Range("RangeStart").Select Application.CutCopyMode = False End Sub
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
This works perfectly! Thanks. Can you explain a little more how the "Me" command works with respect to the conditional formatting? Is "Me" actually a command or could it be any name? I will mark this as Solved.
Me is a named range, just like RangeStart, except instead of being defined in absolute position, it's defined relative to the cell in which it appears. It's convenient enough that I have it in my standard template.
Here's another example of a relative named range. Select cell A2, and do Insert > Name > Define, relUp refers to =A1
Then in some arbitrary cell (say C10), you can enter =SUM(C5:relUP). If you insert a row above the formula, the new row will be captured in the total.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
That's what I was trying to figure out! In the standard Excel online help topics, they state that, by default, named ranges are absolute references. I knew there had to be a way around that somehow.
Thanks again.
Regarding your earlier instructions to name cell A1 as Ductwork!Me, is it possible to refer to the current worksheet rather than the Ductwork worksheet? I have multiple sheets that have a similar setup and I would like the formatting macros to work on all of them rather than have specific macros for each sheet. In other words, is there a way to define cell A1 on each sheet as "Me"?
Thanks
Absolutely. For now, select each worksheet and do insert>name>define sheetName!Me as referring to the cell you have selected with NO $ signs.
For the future, create a blank workbook with a single sheet, the styles and font size you like, and any other customizations (e.g., color palette) you wish. Then select A2 and define
Sheet1!Me Refers to: =A2
Sheet1!relUp Refers to =A1
conBig Refers to: =9.99999999999999E+307
conZzz Refers to: žžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžž žžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžž žžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžžž
The last two names are named constants with workbook (not worksheet) scope. That space among the z's above is a forum artifact.
Then save in your XLSTART directory as Book.xlt, and again in the same directory as Sheet.xlt.
Book.xlt is the default template when you add a workbook (Ctrl+n), and Sheet.xlt when you insert a worksheet.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
When I attempt to create a named cell called "Me" on each worksheet it merely replaces the previous version of Me effectively only allowing one named cell called Me at a time (which isn't surprising I guess). My goal is to have the "PLD" macro that you helped me with work on multiple sheets. Is this possible?
When you do Insert > Name > Define, and preface the range name with the sheet name, the name has sheet scope. Without, it has workbook scope. You need to wrap your brain around this. This is unrelated to relative and absolute references, so for an example, we'll use absolute references.
Create a workbook scratch.xls with four sheets.
Select Sheet1 and define Sheet1!Joe refers to =$A$1 (SHEET scope)
Select Sheet2 and define Sheet2!Joe refers to =$B$2 (SHEET Scope)
Select Sheet3 and define Joe refers to =$C$3 (WORKBOOK scope)
Then ponder Joe used in various ways on various sheets:
Formula is On Reference Is Refers To ------------- -------------- --------- Sheet1 Joe Sheet1!A1 Sheet2 Joe Sheet2!B2 Sheet3 Joe Sheet3!C3 Sheet4 Joe Sheet3!C3 Any sheet Sheet2!Joe Sheet2!B2 Any sheet Sheet3!Joe Sheet3!C3 Any sheet scratch.xls!Joe Sheet3!C3
Last edited by shg; 05-12-2009 at 10:26 AM.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
I think I understand the concept but I believe I may have misstated my goal. In your example if I reference "Joe" on Sheet4 it refers to C3 on Sheet 3. What I'm trying to do is when I reference Joe on Sheet1 I want it to refer to C3 on Sheet1 and when I reference Joe on Sheet2 I want it to refer to C3 on Sheet2. Regarding my original file, I have mulitple sheets that contain the "PLD" string. If I'm in Sheet1 I want the PLD macro to highlight all the cells containing PLD on Sheet1 and if I'm in Sheet2 I want to highlight the cells containing PLD in Sheet2, etc.
Thanks for your patience.
My example was to illustrate the difference between sheet and workbook scope for a name.
On Sheet1, select A1 and do insert > name > define, Sheet1!Me refers to =A1
On Sheet2, select A1 and do insert > name > define, Sheet2!Me refers to =A1
...
On SheetN, select A1 and do insert > name > define, SheetN!Me refers to =A1
Now Me ALWAYS refers to the cell where the reference is made.
After you get it correctly in your Book and Sheet templates, you'll never have to do it again.
If you're having trouble getting this set up in this particular workbook, try this instead:
Delete name Me from ALL sheets. Then select ANY sheet, select A1, and do Insert > Name > Define, Me refers to =!A1
That will create the name for all sheets.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
I now understand the concept but still have a problem getting it to work. I changed the RangeStart and RangeEnd names to a workbook reference without any problem but when I change Me to =!A1 it hangs up. If I change it back to Ductwork!A1 it works fine. I've uploaded the revised file if you want to look at it.
Thanks again
I see what you mean; it doesn't like Me defined that way. That's a curious aberration of Conditional Formatting.
OK, back to the prior method. On each sheet, define SheetName!Me
Last edited by shg; 05-12-2009 at 06:29 PM.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks