+ Reply to Thread
Results 1 to 9 of 9

Macro to create a new value

  1. #1
    Forum Contributor
    Join Date
    12-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    113

    Macro to create a new value

    I appreciate if you could help me with a macro to handle this: I have a column A with Unit number & column B with name. I need to assign a new value to column C:

    if Unit =1 & Label = James, then value in column C = 1;
    If Unit = 1 & Label = Gregory, then value in column C = 2;
    ........... (continued for all rows)
    Attached Images Attached Images
    Last edited by Ucpaul; 04-16-2013 at 10:46 PM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to create a new value

    Hi, Ucpaul,

    resetting the number to start with 1 on each change in Column A may look like this:
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Macro to create a new value

    I don't know why you need this a macro....

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    12-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: Macro to create a new value

    Quote Originally Posted by jindon View Post
    I don't know why you need this a macro....

    Please Login or Register  to view this content.
    Thank you, Hahobe & Jindon for the macro. I have a bout million of records and need macro for a faster job.

    Anyway, both of you missed my question, please see example below:

    if Unit =1 & Label = James, then value in column C = 1;
    If Unit = 1 & Label = Gregory, then value in column C = 2;
    ...........................

    It means that if Label is not James or Gregore, then value in column C will be empty.

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to create a new value

    Hi, Ucpaul,

    I can only speak for myself: why did you post an image that doesnīt show the wanted output which somehow has influenced the answers to your question?

    Please Login or Register  to view this content.
    Ciao,
    Holger

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Macro to create a new value

    Quote Originally Posted by HaHoBe View Post
    why did you post an image that doesnīt show the wanted output which somehow has influenced the answers to your question?
    Agreed
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    12-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: Macro to create a new value

    Quote Originally Posted by jindon View Post
    Agreed
    Please Login or Register  to view this content.
    Hello Guys,

    Sorry that I confused you with my previous attached. I revised it and the answer should be something like below (I "borrowed" your macro and tried rewiting it. I appreciate if you could make it be shorter and work). Thank you for help and kindness.

    revised.jpg
    Range("a2", Range("a" & Rows.Count).End(xlUp)).Columns(3).Formula = "=if(and(a2=1,b2={""James""}),1,"""")"
    Range("a2", Range("a" & Rows.Count).End(xlUp)).Columns(3).Formula = "=if(and(a2=1,b2={""Gregory""}),2,"""")"
    Range("a2", Range("a" & Rows.Count).End(xlUp)).Columns(3).Formula = "=if(and(a2=2,b2={""Donovan""}),1,"""")"
    Range("a2", Range("a" & Rows.Count).End(xlUp)).Columns(3).Formula = "=if(and(a2=2,b2={""John""}),4,"""")"

  8. #8
    Forum Contributor
    Join Date
    12-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: Macro to create a new value

    Need help with this: if I run the above, it just run the last command.

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to create a new value

    Hi, Ucpaul,

    the solution jindon delivered was certainly based on the last sentence in post #4 (the readers of this post do not know what the dots stand for):

    It means that if Label is not James or Gregore, then value in column C will be empty.
    You still publish pictures - itīs true: a picture is worth a thousand words. But is your Excel able to make values from these pictures? Or show more than a static view from a word document? A workbook to have a look at both the data as well as the wanted outcome would have been helpful in the opening post as a lot of efforts would have been directed into the right direction from the start. For my part: sorry but I didnīt get the information from your posts until after I had answered while I should have known them before to adapt into my "efforts".

    it just run the last command.
    Thatīs what you told VBA to do as you referenced the very same range four times. Maybe alter the formula in the first cell to suit your needs and then have that formula being written to the immediate window after entering

    Please Login or Register  to view this content.
    and hitting enter. Please mind that any " inside the formula would need to be doubled up.

    But I wonder why you would want VBA to insert this formula which could easily be dragged down by double-clicking on the lower right corner of the cell containing the correct formula.

    Slow and clumpsy but the code I provided could be amended without problems.

    Ciao,
    Holger

+ 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