+ Reply to Thread
Results 1 to 12 of 12

Code to write to a cell on another sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-16-2010
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    161

    Code to write to a cell on another sheet

    Hi
    New to VBA coding, and hoping for some direction please!

    I have a Workbook with 4 sheets, which is basically a score card, 2 sheets for 2 teams competing. (Sheets named I1 to I4)

    Basically, each team has 2 innings, and the match could end on sheet 3 or 4 depending if one team fails to beat the others score from 2 innings
    Examples:
    Scenario 1:
    Sheet I1 - Team A scores 500
    Sheet I2 - Team B scores 250. There is a threshold that if they fail to get within 200 they must compete again, so:
    Sheet I3 - Team B scores 200. So, they still failed to match 500 adding the 2 scores together.
    Sheet I4 in this scenario is now redundant as Team A won.
    What I want to do in this case, is have some code, that checks if cell N15 on sheet I3 states the letter 'Y' (based on formulas on the sheet that have worked out the game is over), and if 'Y' is present, it writes 'Y' (also to denote game over) to cell N15 on sheet I4 (If that cell states 'Y' then other things are written to the sheet)

    In Scenario 2:
    Sheet I1 - Team A scores 250
    Sheet I2 - Team B scores 300
    Sheet I3 - Team A scores 350
    Sheet I4 - Team B scores 250 BUT the game ended as a draw (complicated rules - the game is Cricket!!) as Team B were still 'In' at the end of the time period so I need to manually type 'Y' to Sheet I4 Cell N15 to denote match over and trigger formulae referencing this cell N15

    So, a long winded explanation to basically say, how can I write Y to cell N15 on Sheet I4 based on manually writing 'Y' to Cell N15 on Sheet I3 !!

    Hope that makes sense to someone!!

    Thanks
    Neil

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Code to write to a cell on another sheet

    Hello Neil,

    Maybe in N15 on I4

    Formula: copy to clipboard
    IF(I3!N15="Y","Y","")


    But if not would you upload the workbook manually adding the results you expect for different scenarios and explain how you arrive at the results if it's not obvious.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    06-16-2010
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    161

    Re: Code to write to a cell on another sheet

    Richard

    I can't have a formula in N15 on Sheet I4 as I also need to be able to manually type 'Y' in certain scenarios

    In Cricket, test match (which this is for), 2 teams have 2 innings. Team A bats (sheet I1), then Team B bats (Sheet I2), but if they don't get within 200 of the Team A's score, they 'follow on' and bat again, so Team B will be on Sheet I3 in this scenario.
    If they were all out in their 2nd Innings and still failed to match Team A's score with their combined scores from 2 innings, the match is won by Team A and I would write Y to Cell N15 on I3, which then I want to trigger this event to write Y to N15 on sheet I4 to trigger other formulae to write results etc.

    If Team B had surpassed the score of Team A with their combined scores, then Team A would bat again (Sheet I4), and N15 on I3 is ignored (I don't write to the cell), and I would then want to write to Cell N15 on sheet I4 once the game is over!!

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Code to write to a cell on another sheet

    Hello Neil,

    This explanation reminds me of the well known humorous explanation of the rules of cricket to a foreigner

    Can you upload the workbook with some typical results and indicate which are the results cells and some notes as to how you've arrived at them with reference to the contributory cells.

    Since you can't use a cell for both a formula and manually overwriting it with a constant you will need to use a Sheet Change event macro.

  5. #5
    Forum Contributor
    Join Date
    06-16-2010
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    161

    Re: Code to write to a cell on another sheet

    Quote Originally Posted by Richard Buttrey View Post
    Hello Neil,

    This explanation reminds me of the well known humorous explanation of the rules of cricket to a foreigner

    Can you upload the workbook with some typical results and indicate which are the results cells and some notes as to how you've arrived at them with reference to the contributory cells.

    Since you can't use a cell for both a formula and manually overwriting it with a constant you will need to use a Sheet Change event macro.
    Richard
    Here is a sample of the workbook, using the scenario where Team A bats first, then Team B, who fail to meet the follow on target, so bat again, and also fail to match Team A with the combined scores, ending the match.
    Thus the sheets are as follows:
    I1 = Team A
    I2 = Team B
    I3 = Team B
    I4 = Team A (unused due to the above conditions)

    A 'normal' match would be
    I1 = Team A
    I2 = Team B
    I3 = Team A
    I4 = Team B

    So, when you look, it's basically a score card for 4 innings of a Cricket test match.
    My main objective when I posted, was in this scenario, on sheet I3, if the match has finished as described above, 'Y' is entered to Cell N15 on sheet I3 (coloured red), and I want to then write that 'Y' to Cell N15 on sheet I4 (coloured yellow).
    Eventually I will use this to write results.

    Having seen the 'What happens if it rains' comment, I might also want to add a 'Y' on either Sheet I1 or I2 if the match is abandoned because of weather.
    So! Can the 'code' be used to check for a 'Y' in N15 on either Sheet I1, 2 or 3 then write it to N15 on I4

    Test Match.xlsx

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Code to write to a cell on another sheet

    Hi,

    I've added a small macro to the Workbook Sheet Change event and also named the N15 cells 'Over'

    When you enter a Y in any of the N15 cells Sheet I4 will also be updated. If you delete a Y so too will Sheet I4 have N15 cleared.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    06-16-2010
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    161

    Re: Code to write to a cell on another sheet

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    I've added a small macro to the Workbook Sheet Change event and also named the N15 cells 'Over'

    When you enter a Y in any of the N15 cells Sheet I4 will also be updated. If you delete a Y so too will Sheet I4 have N15 cleared.
    Richard,

    Many thanks.
    At first it wasn't working, the first N15 cell I tried on Sheet I1 came up with a debug error when writing Y or deleting it.
    I noticed that cell didn't have the name 'Over' like the others, and once added it fixed it

    Thanks again.
    Neil

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Code to write to a cell on another sheet

    Neil

    What if it rains?
    Last edited by Norie; 08-21-2013 at 03:36 PM.
    If posting code please use code tags, see here.

  9. #9
    Forum Contributor
    Join Date
    06-16-2010
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    161

    Re: Code to write to a cell on another sheet

    Quote Originally Posted by Norie View Post
    Neil

    What if it rains?
    Well, I will deal with that using the cell that I write 'Y' in for 'Game Over'

    Richard
    Can you upload the workbook with some typical results and indicate which are the results cells and some notes as to how you've arrived at them with reference to the contributory cells.

    Since you can't use a cell for both a formula and manually overwriting it with a constant you will need to use a Sheet Change event macro.
    I'll do that later so you can see where I am at

  10. #10
    Forum Contributor
    Join Date
    06-16-2010
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    161

    Re: Code to write to a cell on another sheet

    Richard

    Bit of a problem

    I added a worksheet, and when I type anything (in any cell) on it, I get a debugger comes up, which highlights the line
    If Not Intersect(Target, Sh.Range("Over")) Is Nothing Then
    How do I overcome this?!
    Last edited by neil40; 08-22-2013 at 04:43 PM.

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Code to write to a cell on another sheet

    Hi,

    The code I wrote resides in the Workbook Sheet Change event, as opposed to a Worksheet Sheet Change event. This means it only needs to be in one place and it will fire whenever any sheet is changed.

    In your example workbook that's exactly what you wanted to see. Now you've added another sheet you've hit the problem that the new sheet presumably doesn't contain the Range Name 'over'

    You can either modify the Workbook sheet change event so that you trap when your new sheet is firing the event with an IF test and exit the macro if the sheet that's firing the event is the new sheet, OR you can remove the code from the Workbook Sheet Change event and add it to your 4 Worksheet Change events.

    Upload the workbook again if you wish. Will you be adding other worksheets?

  12. #12
    Forum Contributor
    Join Date
    06-16-2010
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    161

    Re: Code to write to a cell on another sheet

    Richard

    The one you have serves the same purpose, effectively, the first sheet, instead of being called Objectives, will be Summary (get the same error on that one - was getting the error on I1 until I added the Cell name
    To work around, I can name A1 with the Cell name for now.
    There will be just the 5 sheets in the workbook
    Last edited by neil40; 08-23-2013 at 04:33 AM.

+ 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. Replies: 6
    Last Post: 07-18-2013, 02:34 AM
  2. Code to write formula in 2 cells, copy, paste values, repeat next row until end of sheet
    By Oly Steel Man in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-14-2013, 11:13 AM
  3. [SOLVED] Code to evaluate contents of one cell then write new values into new cell (Simple IF Stmt)
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-05-2012, 11:54 AM
  4. Code to write in specific cell
    By fatalcore in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 07-09-2011, 03:06 PM
  5. Macro creating new worksheet. Is it possible to write running code for sheet
    By Kernal87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-30-2008, 06:17 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