VBA Clear Formatting in all columns AFTER first blank column

powercell99

Board Regular
Joined
May 14, 2014
Messages
75
Morning,

I am trying to write the code to clear all formatting in all columns after the first blank column in the current active worksheet.

Example: Current worksheet has text in columns A:K, but the formatting continues beyond K, and I'd like to clear the formatting in all columns starting in column L. But the next cycle there may only be text in columns A:F, so then I'd like to clear all formatting in all columns starting in column G.
I know xlToRight is involved, but when i use it, it also clears the last column that contains text. Maybe xlToLeft would work??? Hellllppppp please.

Thanks in advance
 

Some videos you may like

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,278
Use xlToRight but .Offset( ,1) or add 1 to the result column number so it returns the empty column next to the Last used column.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
54,503
Office Version
  1. 365
Platform
  1. Windows
Welcome to the Board!

This code should do it:
Code:
Sub ClearUnusedColumnFormats()


    Dim LastCell As Range
    Dim LastColumn As Integer


'   Find last used column
    Set LastCell = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
    LastColumn = LastCell.Column
    
'   Clear formatting for all columns after last column
    Range(Columns(LastColumn + 1), Columns(Columns.Count)).ClearFormats
        
End Sub
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
54,503
Office Version
  1. 365
Platform
  1. Windows
I know xlToRight is involved, but when i use it, it also clears the last column that contains text.

Not necessarily. That is only true if you know which row contains the last column with data. If your rows can be of variable, how do you know which row to look at?

The code I posted will work, regardless of that fact, as it is not dependent upon you supplying the row to look at ahead of time (it is more dynamic and figures it out itself).
 

powercell99

Board Regular
Joined
May 14, 2014
Messages
75

ADVERTISEMENT


Not necessarily. That is only true if you know which row contains the last column with data. If your rows can be of variable, how do you know which row to look at?

The code I posted will work, regardless of that fact, as it is not dependent upon you supplying the row to look at ahead of time (it is more dynamic and figures it out itself).

THANKS JOE! It works perfectly. I could figure out how to get to the last column with data, but not how to get to 1 column beyond that. You Da Man! I appreciate your very quick response.

Thanks
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
54,503
Office Version
  1. 365
Platform
  1. Windows
I could figure out how to get to the last column with data, but not how to get to 1 column beyond that.
Your welcome.

As AlphaFrog was alluding to, if you know which row you can look at the find the last column, you can use your methodology, you just need to move over (offset) one column, as you are finding the last populated column, not the first unpopulated column when using xlToRight.

So, if you knew you could always use row 1 to find the last column, you could use this could to find the first unpopulated column, i.e.
Range("A1").End(xlToRight).Offset(0,1).Column
 

powercell99

Board Regular
Joined
May 14, 2014
Messages
75
Your welcome.

As AlphaFrog was alluding to, if you know which row you can look at the find the last column, you can use your methodology, you just need to move over (offset) one column, as you are finding the last populated column, not the first unpopulated column when using xlToRight.

So, if you knew you could always use row 1 to find the last column, you could use this could to find the first unpopulated column, i.e.
Range("A1").End(xlToRight).Offset(0,1).Column


Awesome, great information and thanks for the instructions. I really like the forum here. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,109,400
Messages
5,528,511
Members
409,821
Latest member
decibelpilot

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top