+ Reply to Thread
Results 1 to 20 of 20

Macro for specific multiplication of columns

  1. #1
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Macro for specific multiplication of columns

    Hi all,

    I'm looking for a macro that will help me accomplish the following:

    In column B, I have numerical values that range from -10 to 10.
    In column C, I have one of two values: 1, or 2.

    What I'd like is:
    If the value in column C is 1, then multiply the value in Column B by -1.
    If the value in column C is 2, then multiply the value in Column B by 1 (do nothing).

    And these values should populate column C, and clear column B.

    So what this would look like on a sample:

    Before:
    B1: 6 C1: 1
    B2: -4 C2: 2
    B3: 1 C3: 2
    B4: 5 C4: 1

    After:
    B1: empty C1: -6
    B2: empty C2: -4
    B3: empty C3: 1
    B4: empty C4: -5


    Thanks for any help!

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Macro for specific multiplication of columns

    Let's start with this.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro for specific multiplication of columns

    @hakerman2,

    I'm getting a compile error: variable not defined when I try to run it.

    I should also mention, for future reference, that not all cells in columns A and B contain numbers, some contain a string of text like "abc".

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Macro for specific multiplication of columns

    I'm getting a compile error: variable not defined
    Since I hardly ever use Option Explicit this might happen.
    How about this one ?

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro for specific multiplication of columns

    Same error

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Macro for specific multiplication of columns

    This does the job without any loops

    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  7. #7
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro for specific multiplication of columns

    @mehmetcik

    Thanks, although if I run this on columns A and B, this populates C, im looking to replace the cells in B.
    What modifications should be made to change that?

  8. #8
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro for specific multiplication of columns

    @mehmetcik

    I am also getting the same compile error (variable not defined) when using option explicit

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Macro for specific multiplication of columns

    Change Blad1 to Sheet1. Assuming this is your sheet codename.

  10. #10
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Macro for specific multiplication of columns

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro for specific multiplication of columns

    @bakerman2, oh ok I see. I'm now running into the same issue where the macro outputs the values into column C, and deletes column B. I want the values to be in column B, and the values in column A to be deleted.

    @mehmetcik, hmm the macro isn't doing anything now

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

    Re: Macro for specific multiplication of columns

    Please Login or Register  to view this content.

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Macro for specific multiplication of columns

    Please Login or Register  to view this content.
    Last edited by bakerman2; 11-25-2017 at 03:10 AM.

  14. #14
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Macro for specific multiplication of columns

    Thanks Jindon.

    I learnt something here.

    Please Login or Register  to view this content.

  15. #15
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Macro for specific multiplication of columns

    @ jindon & mehmetcik

    But you didn't take this into account.
    not all cells in columns A and B contain numbers, some contain a string of text like "abc".

  16. #16
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Macro for specific multiplication of columns

    We are looking at columns B and C

    In column B, numerical values that range from -10 to 10

    In column C, one of two values: 1, or 2

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

    Re: Macro for specific multiplication of columns

    mehmetcik,
    OP said,
    Quote Originally Posted by tisahardknocklife View Post
    I should also mention, for future reference, that not all cells in columns A and B contain numbers, some contain a string of text like "abc".
    However, OP didn't mention what to do when such cell(s) found, so my code is just populating Error in the cell.
    Only my guess...
    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro for specific multiplication of columns

    @bakerman2, thank you, that's exactly what I was looking for!

    quick question--how can I modify it to multiply different columns?
    e.g. instead of A and B, if I wanted to multiply columns D and E?

  19. #19
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Macro for specific multiplication of columns

    The lines marked in red are the ones to use if you want to multiply columns D & E.
    Please Login or Register  to view this content.
    PS thanks for rep+.

  20. #20
    Registered User
    Join Date
    02-24-2017
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    65

    Re: Macro for specific multiplication of columns

    That worked, thanks.

    Don't mention it, you deserve it!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. multiplication of 2 columns
    By IGIKOMANGOMA in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-22-2016, 12:28 AM
  2. [SOLVED] macro to copy specific columns and rows for a specific month and week
    By Shellybelly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2015, 11:23 AM
  3. [SOLVED] macro needed to extract specific columns out of multiple columns with their row data
    By genetist in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-07-2014, 12:49 AM
  4. Replies: 8
    Last Post: 04-04-2013, 08:02 PM
  5. [SOLVED] Macro code to Copy specific rows into specific columns
    By macrofan2012 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-11-2012, 11:24 AM
  6. Macro to copy specific data to specific columns from a pivot table
    By Raju Radhakrishnan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-09-2012, 07:24 PM
  7. Replies: 11
    Last Post: 02-18-2009, 10:49 AM

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