+ Reply to Thread
Results 1 to 18 of 18

Adding lines and conditional formatting with macros

  1. #1
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58

    Adding lines and conditional formatting with macros

    Hi guys,

    I am new here. I have only recently started playing around with macros, and am slowly getting into them. I do however still struggle a bit to ‘read’ them. Having played with formulas for years I can generally translate a string of formula text into English, like ‘if this cell value is greater than that cell value, then do this, if it is not then if it is equal to that cell value, do that, if not return 0’. Babbling like a child basically.

    With visual basic I have managed to record some handy macros and then tweak them a little manually but I am still struggling to follow it going through it step by step reading it like a formula. So I hope you won’t mind me asking some very stupid questions. I mean well; I’m just a bit slow.

    At present I am trying to do two different things on two different sheets, and I was wondering if perhaps one of you could nudge me in the right direction.


    1:
    I am trying to insert a blank row above every row that has a certain word in column B.

    So basically ‘find value “Example” in column B, and when you find it, insert an entirely blank row directly above it’.


    2:
    Is a bit more challenging. I want to change the colour of a cell if the value in the cell corresponds with the value of another cell in the same column.

    For example, I have a long list of surnames in column A. When I add ‘McNeil’ at the bottom, I would like to be able to run a macro that checks if the name McNeil appears anywhere else in column A, and if it does, that it changes the colour of the cell. Preferably both of the cells that say McNeil, but one would do very nicely indeed.

    If that is possible, I wonder if it is possible to do the same with the first name in column B, but only if there was a match for the surname in column A on the same row. So, if McNeil does not appear in column A, don’t bother, but if it does, does the corresponding first name appear in column B?

    If both of that is possible, the next step would obviously be if McNeil appears in column A (say twice, once in A123 and once in A678), do cells B123 and B678 match as well?


    Would anybody be able to give me a shove in the right direction on either of these ?

    I was also wondering if there is somewhere a list of macro functions, explaining how to construct them. Within the search function of Excel there are these help chapters for formulas, explaining the format and then what every part does. Is there a similar thing for macros?

    I would be most grateful for any help you can offer, and am looking forward to getting stuck into macros a bit more.

    Cheers.

    Damien
    Last edited by DamienC; 07-04-2008 at 02:48 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Welcome to the Forum, please take the time to read the Forum Rules then use the Edit button to amend your Thread title.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58
    My apologies

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Quote Originally Posted by DamienC
    My apologies
    Please edit your title.

    Also, this post has been reported to me for cross posting - http://www.mrexcel.com/forum/showthread.php?t=328358

  5. #5
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58
    I have changed my thread title. Not sure how I can be more precise or concise.

    Would have been happy to provide you with a reference to the cross thread, but I posted here first, so there was nothing to refer to yet. It would also seem I am not able to remove the other one.

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    This will add your new Rows

    Please Login or Register  to view this content.
    Why can't you use basic conditional formatting for the other requirement/

  7. #7
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58
    Amazing!

    Will go and stare at that one for a while to try and deciphre how that works.


    As for the conditional formatting, I can't work out in basic formatting how to make the condition relate to another cell. In other words, I can format cell A1conditionally only if the condition applies to cell A1.

    *Thought dawning* Can I write a lookup formula into the conditional format window?


    Thanks very much for your help. It is most appreciated.

  8. #8
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58
    That first solution really helped. Not just to do what I wanted it to do, but also for me to understand a bit better how macros work. Very cool indeed.

    I am still very much stuck on the second problem though. Have been trying with conditional formatting, but don't seem to be getting anywhere with this.

    The help is much appreciated.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You can use Data validation to prevent duplicates. Select say A1 to A100 and then from the Data menu choose Data Validation.

    From the Allow box in Settings choose Custom

    In the box below type the formula as show below;

    =COUNTIF($A$1:$A$99,A1)=1

    Then add an apprptiate message in the Error Alert tab, eg

    "You are entering a name that is already listed"

    You will not now be able to enter an existing name, prevention is better than ure!

  10. #10
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58
    That sounds promising.

    Except duplicates are allowed. Multiples even. But if there is a match we then need to add the values in another column. So I thought if I can mark them a certain colour we could then manually check the coloured ones, rather than manually checking all of them.

    I have been thinking of ways to do this using a regular formula, but our list is presently 30,000 long and growing by several hundred every day. Creating a formula on every row grinds the thing to a halt.

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I think this des what you want. I have used a sheet event so it will be automatic
    Please Login or Register  to view this content.
    Right click on the sheet tab
    Choose View code
    Set the left hand drop down to WorkSheet
    Find Worksheet_Change in the right hand dropdoen
    Copy & paste the codinto the window

  12. #12
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58
    Sorry, that took me a while to get in. I managed to change the macro slightly to look for the name in Column A (surname) and then column B (first name). Quite happy I could figure out how to do that. I must say I have more trouble 'reading' this one than with the other one. Am I correct in assuming the lines in green, preceded by a ' are for my benefit and are not actually part of the macro? If so, that is amazing!

    It does now highlight the corresponding names in the list above when you click on the name in column A, which is excellent. It does also highlight columns A and B of the cell that you have clicked. Is it possible to tweak the macro to only highlight the selected cells if there is a match in the list above and to leave them white if there isn't?

    Also, is it possible to change the colour of the cells permanently rather than highlighting them when you click on them?

    I'm going back to trying to work out this macro language. Thanks so much for helping me get used to them.

  13. #13
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58
    Am I thinking in the right direction here?

    To avoid the cell I select going red, I have to either:

    tell it to only look for the value more than once (I suspect at the moment is changes colour because it finds the cell itself within the range)

    or

    tell it to only look up for a match (which would work because when I enter new data it will be at the bottom of the sheet).

    Does that sound about right, or am I trying to think of something that will never work?

    My brain is getting quite a good workout today.

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I put the comments in to help you.

    If you leave the cells highlighted then subsequent matches won't stand out.

    I thought that you allowed duplicate surnames, but not if the first name matched. So the code that I wrote checks for the first name & if it is found it then checks the surname. If both match the cells are highlighted. If there is no match the clls remain white.

  15. #15
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58
    I like the comments. It makes life a whole lot easier.

    It's not that duplicated cannot happen. It is just that we have to check them when they do. Which I imagine is the same for the purpose of the macro, except that I can't use data validation.

    The highlighted cells will not have to stand out amongst old highlighted cells, as the old ones will have been sorted into the list alphabetically. I only need them to stand out when they are first imported into the spreadsheet. So if we import 400 names, and 13 match, those 13 will stand out amongst the 387 blank ones. Once we have checked them they will be sorted alphabetically, hopefully never to be seen again, and they can be whatever colour is most comfortable for them.

    At present though, when I select a new name, that always goes red. As will any match in the column. What I would like it to do is go red only if there is a match in the column.

    Does that make sense?

  16. #16
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Remove this code to keep the coloured cells

    Please Login or Register  to view this content.
    here's an example workbook using the original code.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    07-03-2008
    Location
    Scotland
    Posts
    58
    That was pretty straightforward.

    Then I think my last problem remaining is to not colour the actual cell I am clicking on. I presume:
    Please Login or Register  to view this content.
    is the area it looks for a match? Is it possible to change that to that range, but excluding the cell itself?
    Last edited by royUK; 07-05-2008 at 05:29 AM.

  18. #18
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Amend these lines

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1