+ Reply to Thread
Results 1 to 9 of 9

Is target.offset my answer? Adapting code to format an adjacent cell.

  1. #1
    Registered User
    Join Date
    01-16-2012
    Location
    DC
    MS-Off Ver
    Excel 2010
    Posts
    4

    Angry Is target.offset my answer? Adapting code to format an adjacent cell.

    Hi All,

    I am a novice when it comes to coding in excel but had managed to write the below which does a few things to a cell in my range (G3:G32) when a number between 0 and 100 is entered:

    It gets rid of the "Enter Maths %" in red font
    It formats the cell as black font
    It replaces the score with a category such as well below average (such as my vba skills!)
    It fills the cell with color corresponding to the category

    However, I now realize that I want to reuse the percentages for the class which are added to G3:G32. As such, I would prefer to apply all of these formats, which refer to a specific row in column G, to the adjacent cell in column H, thus retaining the percentage scores. How can this be done? I was playing a little with target.offset but I don't know whether that is my answer. Any advice would be greatly appreciated

    Thanks all.

    Mike

    Please Login or Register  to view this content.
    Last edited by thespianchef; 01-19-2012 at 04:07 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,377

    Re: Is target.offset my answer? Adapting code to format an adjacent cell.

    It wouldn't be Target.Offset. It would be I7.Offset(0,1). Target is used for the range in some event handlers.

    But you're doing comparisons and changing things on the basis of what was there or what it's been changed to so the logic might need to change.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-16-2012
    Location
    DC
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Is target.offset my answer? Adapting code to format an adjacent cell.

    Thanks TM! I Think I am nearly there although each time I now run the below code it formats the adjacent cell with the text but not the fill color but then crashes. Any ideas?

    Thanks once again.

    Michael

    Please Login or Register  to view this content.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,377

    Re: Is target.offset my answer? Adapting code to format an adjacent cell.

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook. Don't upload a picture when you have a workbook question. None of us is inclined to recreate your data. Upload the workbook and manually add an 'after' situation so that we can see what you expect. In addition clearly explain how you get the results..
    To attach a file to your post, you need to be using the main 'New Post' or 'New Thread' page and not 'Quick Reply'.
    To use the main 'New Post' page, click the 'Post Reply' button in the relevant thread.

    On this page, below the message box, you will find a button labelled 'Manage Attachments'.
    Clicking this button will open a new window for uploading attachments.

    You can upload an attachment either from your computer or from another URL by using the appropriate box on this page.
    Alternatively you can click the Attachment Icon to open this page.

    To upload a file from your computer, click the 'Browse' button and locate the file.

    To upload a file from another URL, enter the full URL for the file in the second box on this page.
    Once you have completed one of the boxes, click 'Upload'.

    Once the upload is completed the file name will appear below the input boxes in this window.
    You can then close the window to return to the new post screen.

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Is target.offset my answer? Adapting code to format an adjacent cell.

    Is this what you mean?


    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-16-2012
    Location
    DC
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Is target.offset my answer? Adapting code to format an adjacent cell.

    Thanks TMS and Jolivanes. I didn't upload a dataset as the code populates the cells automatically. However, with Jolivanes code and stopping the recursive steps by using enableevents I have the below code which I use in the worksheet change module and which works! Thank you both so much for your help. The code is very simple but populates A1:A10 and then changes values in adjacent B cells accordingly depending on the value of the score.

    Thanks once again.

    Michael

    Please Login or Register  to view this content.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,377

    Re: Is target.offset my answer? Adapting code to format an adjacent cell.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Is target.offset my answer? Adapting code to format an adjacent cell.

    BTW, I would change the "Case 16 To 30" to "Case 17 To 30". You have 16 already in "Case 0 To 16".
    Same for the other Cases.

    HTH
    John

  9. #9
    Registered User
    Join Date
    01-16-2012
    Location
    DC
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Is target.offset my answer? Adapting code to format an adjacent cell.

    THanks John and TMS. I have the select code "overlapping" as in the instance of 16 because on the upper end excel treats 16 as 16.00, on the lower end it treats 16 as 16.01. So I put 17 as the lower bound for the second category and then there is a score of 16.5% this does not get formatted.

    So my final code is:

    Private Sub Worksheet_Change(ByVal Target As Range)

    'Maths % code
    Dim myRng As Range
    Dim c As Range
    Set myRng = Range("A1:A10")
    For Each c In myRng
    Application.EnableEvents = False
    If IsEmpty(c) Then
    c.Value = "Enter Maths %"
    c.Font.ColorIndex = 3
    Else
    c.Font.ColorIndex = 1
    End If

    If c.Value = "Enter Maths %" Then
    c.Font.ColorIndex = 3
    End If

    Select Case c.Value
    Case 0 To 16
    c.Offset(, 1).Value = "Well below av"
    c.Offset(, 1).Interior.ColorIndex = 3
    Case 16 To 30
    c.Offset(, 1).Value = "Below average"
    c.Offset(, 1).Interior.ColorIndex = 45
    Case 30 To 70
    c.Offset(, 1).Value = "Average"
    c.Offset(, 1).Interior.ColorIndex = 6
    Case 70 To 84
    c.Offset(, 1).Value = "Above average"
    c.Offset(, 1).Interior.ColorIndex = 43
    Case 84 To 100
    c.Offset(, 1).Value = "Well above av"
    c.Offset(, 1).Interior.ColorIndex = 4
    Case ""
    c.Offset(, 1).Value = ""
    c.Offset(, 1).Interior.ColorIndex = xlNone
    Case "Enter Maths %"
    c.Offset(, 1).Value = ""
    c.Offset(, 1).Interior.ColorIndex = xlNone
    Application.EnableEvents = True
    End Select
    Next c

    End Sub
    Best wishes,

    Michael

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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