Results 1 to 8 of 8

Macro to Autofit Those Columns containing Indicator #####

Threaded View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,876

    Macro to Autofit Those Columns containing Indicator #####

    I have tried to write code to autofit only those cols containing ####

    However, these cols are not being autofitted


    It would be appreciated if you someone could kindly amend my code


     Sub AutofitColumnsWithHashes()
        Dim ws As Worksheet
        Dim rng As Range
        Dim cell As Range
        
        ' Set the worksheet variable to the "Sales Data" sheet
        Set ws = ThisWorkbook.Sheets("Sales Data")
        
        ' Loop through each column in the worksheet
        For Each rng In ws.UsedRange.Columns
            ' Check if any cell in the column displays "#####"
            For Each cell In rng.Cells
                If cell.Value = "#####" Then
                    ' Autofit the column width
                    rng.EntireColumn.AutoFit
                    Exit For
                End If
            Next cell
        Next rng
    End Sub

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Autofit 3 columns
    By Aland2929 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2023, 04:06 AM
  2. Why won't columns autofit?
    By Curious Dude in forum Excel General
    Replies: 1
    Last Post: 04-12-2019, 10:56 AM
  3. [SOLVED] Need Formula to Place Indicator After Comparing Two Columns
    By JSpree in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-01-2017, 08:38 PM
  4. [SOLVED] Can I determine the effect of autofit to .columnwidth prior to executing the autofit?
    By kadeo in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-13-2016, 10:29 AM
  5. AutoFit columns with macro
    By deano3141 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-14-2014, 08:58 AM
  6. Autofit all columns
    By cultavix in forum Excel General
    Replies: 2
    Last Post: 11-25-2008, 07:26 AM
  7. Autofit some columns
    By Me in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-20-2005, 05:06 PM

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