Consulting

Results 1 to 3 of 3

Thread: Change the range to all data range in column

  1. #1
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    Question Change the range to all data range in column

    I have code that will remove the comma at start and end of string but it will be work on rage D1:D40000 and i need check all the row of the column that has data maybe 200000 and etc ,,,
    So how can i change this code to check all the data on column D

    Thank uoi

    Sub Replacer2()
        Dim rCell As Range, strChar As String
        strChar = ","
        For Each rCell In Worksheets("Sheet2").Range("D1:D40000").Cells
            If Left(rCell.Value, 1) = strChar Then
                rCell.Value = Mid(rCell.Value, 2, Len(rCell.Value) - 1)
            ElseIf Right(rCell.Value, 1) = strChar Then
                rCell.Value = Left(rCell.Value, Len(rCell.Value) - 1)
            End If
        Next rCell
    End Sub
    Last edited by Jacob Hilderbrand; 09-23-2014 at 10:06 PM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    I'd do something like this

    Option Explicit
    Sub Replacer2()
        Dim rCell As Range, strChar As String
        Dim rConstantText As Range
        strChar = ","
        
        'only get the cells in col D that have constants and text in them
        Set rConstantText = Nothing
        On Error Resume Next
        Set rConstantText = Worksheets("Sheet2").Columns(4).SpecialCells(xlCellTypeConstants, xlTextValues)
        On Error GoTo 0
        
        'if there aren't any then get out
        If rConstantText Is Nothing Then Exit Sub
        
        Application.ScreenUpdating = False
        
        'for each of the cells
        'this removes a comma from the front and/or end
        For Each rCell In rConstantText.Cells
            With rCell
                Application.StatusBar = "Checking " & .Address
                If Left(.Value, 1) = strChar Then .Value = Mid(.Value, 2, Len(.Value) - 1)
                If Right(.Value, 1) = strChar Then .Value = Left(.Value, Len(.Value) - 1)
            End With
        Next rCell
        Application.ScreenUpdating = False
        Application.StatusBar = False
    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

  3. #3
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Thank you very much.

Posting Permissions

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