dcsimg
Results 1 to 9 of 9

Thread: vba required to shift 2 cells right

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    Post vba required to shift 2 cells right

    Hi Friends,
    I need a help, actually i have list of data in a excel sheet, which contains some alphabets in the A:A column, those cells need to shifted two cells right as shown in attached excel sample file, eg:row25 should become like row 44 as shown in attached file.
    Kindly review it & help me to complete this task.
    testing1.zip
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,818

    Re: vba required to shift 2 cells right

    the attached file failed to open
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    Re: vba required to shift 2 cells right

    Quote Originally Posted by westconn1 View Post
    the attached file failed to open
    Hi westconn,
    Thanks for looking my request, testing1.zip is working, please use it

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Java (JSP) Hell!
    Posts
    5,304

    Re: vba required to shift 2 cells right

    What code have you got?

    Have a look at the Range method : Insert
    It has a shift type which you can set to Shift:=xlToRight

    Something like this.. but if would depend on your rules as what/when gets shifted
    Code:
    dim rng AS range
    
    set rng = activeworkbook.cells(25,1)
    rng.Insert Shift:=xlToRight
    
    set rng=nothing
    Last edited by Ecniv; Sep 14th, 2018 at 07:13 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    Post Re: vba required to shift 2 cells right

    Quote Originally Posted by westconn1 View Post
    the attached file failed to open

    Hi Westconn,
    sorry, its my mistake, i have attached the new file file again, please check with this one.
    sample.zip

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,818

    Re: vba required to shift 2 cells right

    try like
    Code:
    Dim cel As Range, lastrow as Long
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    For Each cel In Range("a1").Resize(lastrow)
        If cel = "DMT2" Then
            cel.Resize(, 2).Insert xlShiftToRight
        End If
    Next
    rows 44, 46 to 48 now look like row 25
    if the value DMT2 could vary some changes would have to be made
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    Re: vba required to shift 2 cells right

    Quote Originally Posted by westconn1 View Post
    try like
    Code:
    Dim cel As Range, lastrow as Long
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    For Each cel In Range("a1").Resize(lastrow)
        If cel = "DMT2" Then
            cel.Resize(, 2).Insert xlShiftToRight
        End If
    Next
    rows 44, 46 to 48 now look like row 25
    if the value DMT2 could vary some changes would have to be made
    Hi Westconn,
    Thank you so much for the code, actually I need a slight modification, if the range A:A contains any alphabets (not only DMT2), it should shift 2 cells right, could you please help me to do this task, Thanks in Advance.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,818

    Re: vba required to shift 2 cells right

    how about
    Code:
    Dim cel As Range, lastrow as Long
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    For Each cel In Range("a1").Resize(lastrow)
        If Not IsNumeric(cel)  Then
            cel.Resize(, 2).Insert xlShiftToRight
        End If
    Next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    Re: vba required to shift 2 cells right

    Thank you so much genius, it worked like a charm.....

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width