+ Reply to Thread
Results 1 to 10 of 10

replace x number of cells in row with y

  1. #1
    Forum Contributor
    Join Date
    09-09-2014
    Location
    England
    MS-Off Ver
    2013
    Posts
    102

    replace x number of cells in row with y

    Hello,

    I am trying to program my macro to find 9's and replace with 8's. However it's not quite that simple, I would like my macro to search the row and then replace from the end in. For example I would like my macro to replace for example:

    0 9 9 1 1 0 1 0 9 9 9 9

    With:

    0 9 9 1 1 0 1 0 8 8 8 8

    I.e. search from the end of the row if there are 6 9's at the end of the row replace with 6 8's, however those 9's earlier on in the row should remain unchanged.

    Is this possible?

    Thanks

    Z

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: replace x number of cells in row with y

    Assuming your data starts with column A row 1 try such macro:
    Please Login or Register  to view this content.
    Best Regards,

    Kaper

  3. #3
    Forum Contributor
    Join Date
    09-09-2014
    Location
    England
    MS-Off Ver
    2013
    Posts
    102

    Re: replace x number of cells in row with y

    Kaper,

    If I wanted to change what I was replacing 9 to 8 to Z to Y, could I just swap out the 9s for Zs etc. like:

    [code]
    Sub ReplaceZwithY()
    Dim rng As Range
    Worksheets("Data").Activate
    Set rng = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)

    MsgBox "The cells selected were " & rng.Address

    rng.Replace What:=" ", _
    Replacement:="Z", _
    LookAt:=xlPart, _
    SearchOrder:=xlByRows, _
    MatchCase:=False, _
    SearchFormat:=False, _
    ReplaceFormat:=False

    Dim i As Long, j As Long, maxcol As Long, currentrow()
    Application.ScreenUpdating = False
    For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    maxcol = Cells(i, Columns.Count).End(xlToLeft).Column
    currentrow = Application.Transpose(Cells(i, "A").Resize(1, maxcol).Value)
    For j = UBound(currentrow) To 1 Step -1
    If currentrow(j, 1) = Z Then
    currentrow(j, 1) = Y
    Else
    Exit For
    End If
    Next j
    Cells(i, "A").Resize(1, maxcol).Value = Application.Transpose(currentrow)
    Next i
    End Sub
    [code]

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: replace x number of cells in row with y

    I do not understand why you replace all slingle spaces to Z first.

    The coge using proposed in post #2 metodology could look like:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    09-09-2014
    Location
    England
    MS-Off Ver
    2013
    Posts
    102

    Re: replace x number of cells in row with y

    Thank you Kaper.

    I am trying to distinguish between not reached and omitted. To do this I need to code all blanks as Z so that there are no blank spaces in my data.

    I need to distinguish between not reached and omitted i.e. if a candidate answers questions 1,2,4 and 5 out of 15, I want to code question 3 as Z (omitted) but code questions 6-15 as Y (not reached). The code below works for doing this when omit is 9 and not reached as 8, but when I change 9 to Z and 8 to Y it falls over. Any ideas why or how to get it to work? Thanks.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: replace x number of cells in row with y

    Honestly, probably half of the discussion would not be necessary if you'd attach sample wprkbook, so please consider doing so. See explanation in http://www.excelforum.com/forum-rule...rum-rules.html

  7. #7
    Forum Contributor
    Join Date
    09-09-2014
    Location
    England
    MS-Off Ver
    2013
    Posts
    102

    Re: replace x number of cells in row with y

    replace 9 with 8 start - Copy.xlsm

    Kaper, I attach my workbook here (hopefully). As I say the idea is to turn all of the blanks into Z and then anything that is not followed by an A,B,C or D into a Y.

    Thanks again for your help on this one.

    Z

  8. #8
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: replace x number of cells in row with y

    Try:
    Please Login or Register  to view this content.
    Note that there is no intermediate step. For each row entries are analyzed rigt to left. Blanks are replaced with Y or Z (until first non-blank is met - with Y, thhen with Z).
    Last edited by Kaper; 10-20-2015 at 03:40 AM.

  9. #9
    Forum Contributor
    Join Date
    09-09-2014
    Location
    England
    MS-Off Ver
    2013
    Posts
    102

    Re: replace x number of cells in row with y

    Perfect! Thanks Kaper. I have one slight addition to the end of the code, how would I perform a count of my changes in a msgbox? To count how many Z's and how many Ys there are in the spreadsheet?

  10. #10
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: replace x number of cells in row with y

    1) How about just simple COUNTIF?
    2) if not in the sheet then in the code (just before end sub):
    Please Login or Register  to view this content.
    3) Depending on data layout in real sheet may be instead of these 2 lines:
    Please Login or Register  to view this content.
    such one can be used
    Please Login or Register  to view this content.

+ 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. [SOLVED] Replace 2 cells into 1 with a number
    By Jangorage in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-15-2014, 08:09 AM
  2. [SOLVED] Replace blank cells with ZERO using Find Replace
    By makinmomb in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-25-2014, 04:47 PM
  3. Find a number and replace the adjacent cell with other number
    By shafath03 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-20-2013, 09:09 PM
  4. [SOLVED] Replace text number with some other number based on the sheet
    By amandeepsharma89 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-03-2013, 09:52 PM
  5. Replace various number values in cells with a text string
    By tommygray in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2013, 05:35 AM
  6. [SOLVED] Compare columns, replace matching number with reference number and fill down random amount
    By datadigger in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-04-2012, 10:53 PM
  7. Find, and replace but copy cells before replace
    By raehippychick in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-05-2007, 08:16 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