+ Reply to Thread
Results 1 to 14 of 14

Run code (Len-Instr-Mid) in all nonempty cells

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-22-2014
    Location
    Turrialba, Costa Rica
    MS-Off Ver
    Excel 2010
    Posts
    108

    Run code (Len-Instr-Mid) in all nonempty cells

    Hello.
    In Excel 2016 I have a Sheet with a range of cells with data and others cells with no data.
    I have a code which works well in a cell with data.
    However, I need to write the code into a loop to apply the code in all cells with data.
    I've searched the web trying to find help on how to complete my script, but I just can't seem to get how to do it.
    Any help is welcome

    Sub lenInstrMid()
    Dim Z As String
    Dim X, y as Integer
    
    Z= Sheets(1).Cells(1,11)
    x= Len(Z)
    Y= Instr(Z, "_")
    Z= Mid(Z,1, y-1)
    Sheets(1).Cells(2,11) = Z
    
    End Sub
    Last edited by Leith Ross; 08-01-2018 at 04:33 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    Hello gipsyfkls14,

    Does your data start in cell "A11" and continue down column "A" ?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    04-22-2014
    Location
    Turrialba, Costa Rica
    MS-Off Ver
    Excel 2010
    Posts
    108

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    I really appreciate your reply.
    I apologyze.
    My data starts at G2 and continue down and right in a range until the last cell with data.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    Hello gipsyfkls14,

    One more thing, what do you consider to be an empty cell? I know sometimes people consider a zero value as empty.

  5. #5
    Forum Contributor
    Join Date
    04-22-2014
    Location
    Turrialba, Costa Rica
    MS-Off Ver
    Excel 2010
    Posts
    108

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    I consider an empty cell with no data at all.
    Many thanks

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    Hello gipsyfkls14,

    This should work for you ...

    Note: You may need to change the worksheet name from "Sheet1" to whichever sheet the data is on.

    Sub Macro1()
    
        Dim Cell    As Range
        Dim Rng     As Range
        Dim RngEnd  As Range
        Dim Wks     As Worksheet
        
            Set Wks = Worksheets("Sheet1")
            
            Set Rng = Wks.Range("G2")
            Set RngEnd = Wks.Cells(Rows.Count, "G").End(xlUp)
            
            If RngEnd.Row < Rng.Row Then Exit Sub
            
            Set Rng = Wks.Range(Rng, RngEnd)
            
                For Each Cell In Rng.SpecialCells(xlCellTypeConstants)
                    Cell.Offset(0, 1).Value = Left(Cell, InStr(1, Cell, "_") - 1)
                Next Cell
            
    End Sub

  7. #7
    Forum Contributor
    Join Date
    04-22-2014
    Location
    Turrialba, Costa Rica
    MS-Off Ver
    Excel 2010
    Posts
    108

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    Leith Ross.
    I think I did not explain myself correctly. I apologize.
    If I run my code in Cells(2, 9) returns all character left to the period (,)
    Note.
    See last cells with data to the right
    I enclose a workbook where Sheet1 has the original data and Sheet2 the data I am looking for.

    Thanks againg
    Attached Files Attached Files

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    Hello Hello gipsyfkls14,

    The attached workbook has the macro below added to it.

    Sub Macro1()
    
        Dim Cell    As Range
        Dim LastCol As Long
        Dim LastRow As Long
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = Worksheets("Sheet1")
            
            LastRow = Wks.Cells(Rows.Count, "A").End(xlUp).Row
            LastCol = Wks.Cells(1, Columns.Count).End(xlToLeft).Column
            
            Set Rng = Wks.Range("G2").Resize(LastRow - 1, LastCol - 6)
            
            On Error Resume Next
            
                For Each Cell In Rng.SpecialCells(xlCellTypeConstants)
                    Cell.Value = Left(Cell, InStr(1, Cell, "_") - 1)
                Next Cell
            
            On Error GoTo 0
            
    End Sub
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    04-22-2014
    Location
    Turrialba, Costa Rica
    MS-Off Ver
    Excel 2010
    Posts
    108

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    Leith Ross.
    Your code works as I expected.
    Thank you very much indeed.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    Hello gipsyfkls14,

    De nada.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Another way …


    As a beginner starter :

    PHP Code: 
    Sub Demo1()
                 
    Dim Rg As Range
                 Application
    .ScreenUpdating False
        With Sheet1
    .Range("G2:DK" Sheet1.UsedRange.Rows.Count)
                 
    Set Rg = .Find("_", , xlValuesxlPart)
            Do 
    Until Rg Is Nothing
                     Rg
    .Value2 Val(Rg.Value2)
                 
    Set Rg = .FindNext(Rg)
            
    Loop
        End With
                 Application
    .ScreenUpdating True
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    Baby level for the data uploaded.
    Sub test()
        Sheets("sheet1").UsedRange.Offset(1).Replace "_*", "", 2
    End Sub

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Run code (Len-Instr-Mid) in all nonempty cells

    gipsyfkls14

    Thanks for the rep and glad you like it.

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Run code (Len-Instr-Mid) in all nonempty cells


    gipsy, thanks for the rep' !

    But jindon's way is the best one as following the main first rule : Think Excel Before VBA ! …

+ 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. Replies: 1
    Last Post: 09-29-2017, 12:10 AM
  2. [SOLVED] If Instr(Cells(x,1), "String") or InStr(cells(x,1),"string") Then.... won't work
    By DAKevin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-19-2016, 06:10 PM
  3. [SOLVED] Highlight entire row based on nonempty cell
    By surePac in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-24-2014, 03:19 PM
  4. [SOLVED] VBA code not returning correct string using MID and InStr
    By kramtelbuab in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-04-2013, 11:30 PM
  5. Trouble with macro (empty and nonempty cells)
    By katrinaaa90 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-07-2012, 03:36 PM
  6. lock nonempty cells
    By Aleksandra in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-25-2009, 07:28 AM
  7. Delete empty cells within row until you hit nonempty cell
    By as_sass in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-17-2008, 12:55 PM

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