+ Reply to Thread
Results 1 to 9 of 9

Run two macros in one sheet when a cell value = 1

  1. #1
    Registered User
    Join Date
    09-17-2019
    Location
    Ireland
    MS-Off Ver
    2016
    Posts
    5

    Run two macros in one sheet when a cell value = 1

    Hi,

    I am new to the visual basic. I need to copy data from two Columns to another two columns. I think the two macros below should work for me (got it from google search). But how can only activate these macros when the value of cell A1=1. Please help me to write the code.

    Thank you

    General Macro1

    Sub Macro1()
    Dim i As Integer
    i = 3
    Do While Cells(i, 7).Value <> ""
    Cells(i, 11).Value = Cells(i, 7).Value
    i = i + 1
    Loop
    End Sub


    General Macro2


    Sub Macro2()
    Dim k As Integer
    k = 3
    Do While Cells(k, 8).Value <> ""
    Cells(k, 12).Value = Cells(k, 8).Value
    k = k + 1
    Loop
    End Sub

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,810

    Re: Run two macros in one sheet when a cell value = 1

    You can check if A1=1 and then run your code:
    Please Login or Register  to view this content.
    However, you could do what you want using one macro. Can you post a copy of your file?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    09-17-2019
    Location
    Ireland
    MS-Off Ver
    2016
    Posts
    5

    Re: Run two macros in one sheet when a cell value = 1

    Thank you for your response.
    Please explain, how can I modify your Macro to run two operations?
    Please find the attached file.

    Regards
    Blessen
    Attached Files Attached Files

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,810

    Re: Run two macros in one sheet when a cell value = 1

    Try:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-17-2019
    Location
    Ireland
    MS-Off Ver
    2016
    Posts
    5

    Re: Run two macros in one sheet when a cell value = 1

    Thank you very much for the code and your valuable time.

    I wish it runs continuously as long as A1 =1 and copy any new Cell values from G and H columns to K and L columns. Then stops when A1=0
    It now runs only when I press F5 (Run) on the VBA Project window. Any thoughts to make it happen.

    Regards,
    Blessen

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,810

    Re: Run two macros in one sheet when a cell value = 1

    Is the value in A1 entered manually or is it the result of a formula? Can you explain in detail what you mean by "runs continuously as long as A1 =1 "?

  7. #7
    Registered User
    Join Date
    09-17-2019
    Location
    Ireland
    MS-Off Ver
    2016
    Posts
    5

    Re: Run two macros in one sheet when a cell value = 1

    Hi,

    It is a project work. This is my wish list
    Excel add-in called MXsheet from Mitsubishi, records data to the cells from a machine PLC. A1 is assigned to a bit device, On=1 and off=0
    If A1 is ON, then readings from two temperature sensors are taken to the G and H columns and those readings need to be shifted to another sheet or columns in the same sheet.
    When A1=0, then save the data and create a line trend graph from the two readings. If the Macro can print the graph, I will be over the moon.
    The MX sheet deletes the G and H readings when it starts again. Those columns are assigned to the MXsheet.
    The number of rows are depending on the duration of the machine cycle, it varies from 10 min to 20 min. The sampling rate can be adjusted, ideally every 10 seconds.
    I hope you could understand the logic to help me. Please...

    Thank you

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,810

    Re: Run two macros in one sheet when a cell value = 1

    I'm not sure I understand the process. In VBA there are several "events" that can trigger a macro to run automatically. One of those events is a Worksheet_Change event which means that when a cell in a sheet is changed, the macro runs automatically. However, I believe the key in your case, is how that change in A1 is made. Usually the change has to be made manually, that is, the O or 1 in A1 has to be entered manually. It sounds like the add-in is making the change in A1 to either O or 1. If this is correct, then I don't know if that will trigger the macro to run. You would have to try it to find out. Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet containing the data and click 'View Code'. Paste the macro below into the empty code window that opens up. Close the code window to return to your sheet. If it works, when A1 changes to "1", you will see the data copied to columns K and L.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-17-2019
    Location
    Ireland
    MS-Off Ver
    2016
    Posts
    5

    Re: Run two macros in one sheet when a cell value = 1

    hi,
    Thank you for the code.
    I had the run time error 9 on excel, just got it fixed.
    Your code is working when I manually entered 1 into cell A1. To make the transfer live, I am going to pulse A1 on every second, so the updated rows on G & H will be transferred to K & L. Did not test with the add-in yet, probably will get a chance to check it on Monday.
    Thank you for your time.
    Sorry for the delay in replying you.

+ 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. Help With Macros – Action (Select Sheet) Based On Cell Value
    By npsnps in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2016, 12:01 PM
  2. [SOLVED] Return Selected Cell to Cell A1 on Every Sheet/Tab - VBA Macros
    By Jason Carlos in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-01-2014, 08:50 PM
  3. [SOLVED] Need help in creating Macros to move a cell value into new sheet
    By karthisiva in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2013, 10:51 PM
  4. MACRO save active sheet wth name from selected cell and without macros and buttons
    By forfiett in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-26-2013, 04:14 PM
  5. [SOLVED] Macros to find matching text on other sheet and to select an offset cell
    By lottidotti in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2012, 05:59 AM
  6. Replies: 6
    Last Post: 08-10-2012, 08:42 PM
  7. Macros to change a cell value by clicking and to take you to another sheet
    By jhelliar in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-09-2011, 10:05 AM

Tags for this Thread

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