+ Reply to Thread
Results 1 to 22 of 22

Conditional Delete Cells and shift to the left

Hybrid View

markos97 Conditional Delete Cells and... 06-28-2013, 04:17 AM
AndyLitch Re: Conditional Delete Cells... 06-28-2013, 04:27 AM
markos97 Re: Conditional Delete Cells... 06-28-2013, 04:35 AM
HaHoBe Re: Conditional Delete Cells... 06-28-2013, 04:36 AM
mahaveersomani Re: Conditional Delete Cells... 06-28-2013, 04:44 AM
HaHoBe Re: Conditional Delete Cells... 06-28-2013, 04:51 AM
markos97 Re: Conditional Delete Cells... 06-28-2013, 04:47 AM
markos97 Re: Conditional Delete Cells... 06-28-2013, 04:58 AM
mahaveersomani Re: Conditional Delete Cells... 06-28-2013, 04:58 AM
HaHoBe Re: Conditional Delete Cells... 06-28-2013, 05:03 AM
mahaveersomani Re: Conditional Delete Cells... 06-28-2013, 05:10 AM
markos97 Re: Conditional Delete Cells... 07-17-2013, 03:57 PM
HaHoBe Re: Conditional Delete Cells... 07-17-2013, 04:29 PM
markos97 Re: Conditional Delete Cells... 07-18-2013, 01:32 AM
HaHoBe Re: Conditional Delete Cells... 07-18-2013, 01:50 AM
  1. #1
    Registered User
    Join Date
    06-28-2010
    Location
    Posen, Poland
    MS-Off Ver
    Excel 2000
    Posts
    46

    Conditional Delete Cells and shift to the left

    Hi,

    I have an input data like that:
    --A-B-C-D-E-F-G
    1-5-7-7-7-5-7-5
    2-7-5-5-5-7-5-7
    3-5-5-5-7-7-5-5
    and so on...

    an output data I need:
    --A-B-C-D-E-F-G
    1-7-7-7-5-7-5
    2-7-5-5-5-7-5-7
    3-7-7-5-5
    and so on...

    as above, macro must to delete number 5 from left to right until not contain other number, and shift to left

    my problem is similar to below:
    Sub Account_or_something()
    '1. http://www.mrexcel.com/forum/excel-questions/545536-conditional-delete-cells-right.html
    '2. other codes here: http://www.ozgrid.com/forum/showthread.php?t=160748
    'searching by google fraze: "end sub" "End(xlToLeft)" "until" "delete cells"
    
    Do While [a1] <> "5"
            [a1].Delete shift:=xlToLeft
        Loop
    End Sub
    above macro works good, but only for one row...

    any help?

    ps. I have also other variant my data, like this:

    input data:
    --A-B-C-D-E-F-G
    1-5-6-3-8-4-1-5
    2-9-5-5-5-6-5-44
    3-5-5-5-6-4-5-5
    and so on...

    output data:
    --A-B-C-D-E-F-G
    1-6-3-8-4-1-5
    2-9-5-5-5-6-5-44
    3-6-4-5-5
    and so on...
    Last edited by markos97; 06-28-2013 at 04:19 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Conditional Delete Cells and shift to the left

    I'm not clear... Do you want to delete all 5's?
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    06-28-2010
    Location
    Posen, Poland
    MS-Off Ver
    Excel 2000
    Posts
    46

    Re: Conditional Delete Cells and shift to the left

    no! see my date before and after,
    in this case, I want to delete the number 5 starting from left, and if you meet another number, stop removing

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Conditional Delete Cells and shift to the left

    Hi, markos97,

    maybe like this:
    Sub EF934924()
    Dim lngCounter As Long
    
    Const cstrCOL As String = "A"
    Const clngNUM As Long = 5
    
    For lngCounter = 1 To Cells(Rows.Count, cstrCOL).End(xlUp).Row
      Do While Cells(lngCounter, cstrCOL).Value = clngNUM
        Cells(lngCounter, cstrCOL).Delete shift:=xlToLeft
      Loop
    Next lngCounter
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Valued Forum Contributor
    Join Date
    09-04-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    463

    Re: Conditional Delete Cells and shift to the left

    check the attached file

    i have made some sample data for your solution

    press CTRL+r TWO TIME PRESS.

    If it is really helpful to you then dont forget to hit * button for user reputation

    Regards
    CA Mahaveer Somani
    Attached Files Attached Files

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Conditional Delete Cells and shift to the left

    Hi, mahaveersomani,

    your code will delete every occurrance in the sheet, not only those at the beginning of each row until any other number is found.

    Sub autohide()
    
    Application.ScreenUpdating = False
    Cells.Replace What:="5", Replacement:="", LookAt:=xlWhole, SearchOrder _
            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    
    On Error Resume Next
    ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks).Delete shift:=xlToLeft
    Application.ScreenUpdating = True
    End Sub
    Ciao,
    Holger
    Last edited by HaHoBe; 06-28-2013 at 04:55 AM. Reason: code added

  7. #7
    Registered User
    Join Date
    06-28-2010
    Location
    Posen, Poland
    MS-Off Ver
    Excel 2000
    Posts
    46

    Re: Conditional Delete Cells and shift to the left

    Yeah - That's it!
    Macro works perfectly for me!
    Thanks a lot HAHOBE, AndyLitch also many thanks!
    and you - mahaveersomani (yes I remember) - ps, your code not exactly do what I need (save blank space between cells)
    Last edited by markos97; 06-28-2013 at 04:53 AM.

  8. #8
    Registered User
    Join Date
    06-28-2010
    Location
    Posen, Poland
    MS-Off Ver
    Excel 2000
    Posts
    46

    Re: Conditional Delete Cells and shift to the left

    yes, you'r right HAHOBE - now I see it error

  9. #9
    Valued Forum Contributor
    Join Date
    09-04-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    463

    Re: Conditional Delete Cells and shift to the left

    sorry @hahobe........now i see the his question properly........now i m thinking that still i could not understand which kind of this problem.....i mean what is his needs.....confused......by the way its ok...hahobe.......good day dear..


    Regards
    CA Mahaveer Somani

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Conditional Delete Cells and shift to the left

    Hi, mahaveersomani,

    using your approach the code may look like
    Sub Delete5()
    
    Application.ScreenUpdating = False
    
    Do While Err = 0
      With Range("A:A")
        .Replace What:="5", Replacement:="", LookAt:=xlWhole, SearchOrder _
              :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        On Error Resume Next
        .SpecialCells(xlCellTypeBlanks).Delete shift:=xlToLeft
      End With
    Loop
    
    Application.ScreenUpdating = True
    
    End Sub
    Ciao,
    Holger

  11. #11
    Valued Forum Contributor
    Join Date
    09-04-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    463

    Re: Conditional Delete Cells and shift to the left

    thanks to guide me..........@hahobe

  12. #12
    Registered User
    Join Date
    06-28-2010
    Location
    Posen, Poland
    MS-Off Ver
    Excel 2000
    Posts
    46

    Re: Conditional Delete Cells and shift to the left

    one again, thank you HaHoBe, now your code looks like:

    Sub EF934924()
    
    Dim lngCounter As Long
    Application.ScreenUpdating = False 'added
    
    Const cstrCOL As String = "A"
    Const clngNUM As Long = 55
    
    For lngCounter = 1 To Cells(Rows.Count, cstrCOL).End(xlUp).Row
      Do While Cells(lngCounter, cstrCOL).Value = clngNUM
        Cells(lngCounter, cstrCOL).Delete Shift:=xlToLeft
        
      Loop
    Next lngCounter
    Application.ScreenUpdating = True 'added
    
    End Sub
    I need change this part of code
    .Delete Shift:=xlToLeft
    to something like that:
    .ClearContents
    but it does not work like I wanted;
    I need only clear this cells (not shift to left in this moment), I'm try it, but the cells go to up...

    --edit--

    as above my input data looks like

    --A-B-C-D-E-F-G
    1- -7-7-7-5-7-5
    2-7-5-5-5-7-5-7
    3- - - -7-7-5-5
    Last edited by markos97; 07-17-2013 at 04:06 PM.

  13. #13
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Conditional Delete Cells and shift to the left

    Hi, markos97,

    sory but a sample workbook with what it looks like now and what it should look like after the macro would make things clearer. What should be cleared in Column A/B/what cells? The new sample isn´t explaining itself, and there is not 5 or 55 in the fist column as far as I can judge.

    Ciao,
    Holger

  14. #14
    Registered User
    Join Date
    06-28-2010
    Location
    Posen, Poland
    MS-Off Ver
    Excel 2000
    Posts
    46

    Re: Conditional Delete Cells and shift to the left

    yes, you're right - HaHoBe

    see the attach - alles clear :-)
    Attached Files Attached Files

  15. #15
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Conditional Delete Cells and shift to the left

    Hi, markos97,

    to my opinion this is a different problem to what you originally asked help for.

    Code for a standard module:
    Sub EFEF934924_2()
    
    Const cstrValue As String = "99"
    
    Range("A4:AD" & Range("A" & Rows.Count).End(xlUp).Row).Replace What:=cstrValue, Replacement:="", LookAt:=xlWhole, SearchOrder _
            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    
    End Sub
    Ciao,
    Holger

  16. #16
    Registered User
    Join Date
    06-28-2010
    Location
    Posen, Poland
    MS-Off Ver
    Excel 2000
    Posts
    46

    Re: Conditional Delete Cells and shift to the left

    thanks HaHoBe, but something is wrong, macro clear all "99" from A4 to AD2348 and don't touch A2349:AD2377

    see the sample with an error after running your code, here
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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