Consulting

Results 1 to 13 of 13

Thread: Offset in an Array - Replacement

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Offset in an Array - Replacement

    Good Evening,

    now i made an array.

    I put my offset values in an array but so far theres a type mismatch

        oSearch = Array("A", "B", "C", "D")
        
        oReplace = Array("i-5", "i-3", "i-9", "i-4")
        
        For i = LBound(oSearch) To UBound(oSearch)
        For Each oCell In ThisWorkbook.Worksheets("Test").Range("K1:K10").Cells
        oCell.Replace What:=oSearch(i), Replacement:=oCell.Offset(, oReplace(i)).Value                           
    
        Next oCell
        Next i
        End Sub
    I cant seem to work out why?
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    ?

    oSearch = Array("A", "B", "C", "D")
     
    oReplace = Array(-5, -3, -9, -4)
     
    For i = LBound(oSearch) To UBound(oSearch)
        For Each oCell In ThisWorkbook.Worksheets("Test").Range("K1:K10").Cells
            oCell.Replace What:=oSearch(i), Replacement:=oCell.Offset(, i + oReplace(i)).Value
        Next oCell
    Next i

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello M,

    thank you for your help.

    now that looks like a better idea than mine.

    However the values dont seem to enter correctly from the replace

    Results.jpg



    Using the below as a test

       oSearch = Array("A", "B", "C", "D")
    
        oReplace = Array("-4", "-3", "-2", "-1")
    
    
        For i = LBound(oSearch) To UBound(oSearch)
        For Each oCell In ThisWorkbook.Worksheets("Test").Range("K1:K10").Cells
            oCell.Replace What:=oSearch(i), Replacement:=oCell.Offset(, i + oReplace(i)).Value
        Next oCell
        Next i


    I did try to use i- as well but that didnt work
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    "-4": string
    -4: number
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello Mancubus,

    oh yes well spotted

    now i used this

    oSearch = Array("A", "B", "C", "D")

    oReplace = Array(-5, -4, -3, -2)

    on the same set as above

    but the result is 5,3,1 ' D is missing

    it should be 5,4,3,2

    there is something quite not right but i cant put my finger on it
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    why don't you upload your workbook with the desired output?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    I have attached my workbook
    thank you

    Offset - Array.xlsm
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i dont understand what is input and what is output from the sample data.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I think this is what you're looking for

    The orange is starting, blue is result


    Capture.JPG


    Option Explicit
    
     Sub Search_Replace_Array()
         Dim oCell As Range
         Dim i As Long
         Dim vOffset As Variant, vSearch As Variant
        
        vSearch = Array("A", "B", "C", "D")
        vOffset = Array(-9, -4, -7, -6)
        
        For i = LBound(vSearch) To UBound(vSearch)
            For Each oCell In ThisWorkbook.Worksheets("Test").Range("K2:K5").Cells
                Call oCell.Replace(vSearch(i), oCell.Offset(, vOffset(i)).Value, xlPart)
            Next oCell
        Next i
        
        End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello Paul and Mancubus,

    I am terribly sorry, at my usual inablity to explain.

    Well it was meant to be very simple - so i thought


    it was meant to be 9 4 7 6 in each of the cells in Column K

    becuase in the array I set it to be oReplace=Array(-9,-4,-7-6)

    In each cell we have A B C D

    replace with the array offset.

    so it would be the same in each cell in column K

    Each cell would be 9,4,7,6

    For the test data i made the values all the same thats why the confusion I am sorry
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  11. #11
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Image attached
    Result.jpg
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Is post #9 wrong?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  13. #13
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Pardon me Paul,

    its been a long week, and begone are my spectacles.

    No you did it perfecto as usual thank you very much

    I cannot explain why mine didnt work, becasuse i did it similar, amiss the vSearch variant
    Well i get really anxious over these arrays all the time as you know

    it started off well and then it didnt


    and i did check all my other arrays for help but that was a soup


    Thank you for your generous help and other folk too

    Now all have a great weekend

    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •