Consulting

Results 1 to 3 of 3

Thread: Search repated word in excel using VBA

  1. #1

    Search repated word in excel using VBA

    Dear, Sir,

    I need to autofited some columns that its postion not constant. I wrote "Cln_Autofit" after the last cell in the column. Now i need to print these columns but i need to refit them automaticly.
    So i need to VBA to seach for "Cln_Autofit" then i will use Columns(ActiveCell.Column).AutoFit
    go next to find "Cln_Autofit"
    and so on for all "Cln_Autofit" in the sheet

    I can write like this "Cln_Autofit_01" for the first column then "Cln_Autofit_02" for the 2nd and so one then i will search for each "Cln_Autofit_01" & "Cln_Autofit_02" seperatly but this could make the VBA long

    Thank you

  2. #2
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    How about:

    Sub marine()
        Dim r As Range, rFit As Range
        For Each r In ActiveSheet.UsedRange
            If r.Value = "Cln_Autofit" Then
                If rFit Is Nothing Then
                    Set rFit = r
                Else
                    Set rFit = Union(rFit, r)
                End If
            End If
        Next r
        rFit.EntireColumn.AutoFit
    End Sub
    Have a Great Day!

  3. #3
    Thank you. Great

Posting Permissions

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