Clear contents of column except column header

user125

New Member
Joined
Feb 20, 2020
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am having trouble with my VBA code where I am supposed to clear contents of the entire column except the column header. The code I have so far only clears the column header and I am not sure how exactly to change it to help with my issue. P.S. this is part of a larger procedure and that is why I have not included the part where I dim variables. Thank you.

VBA Code:
lastcolumn = wb.Sheets(destinationSheetName).Cells(columnheader, Columns.count).End(xlToLeft).Column
            For count = 1 To lastcolumn
                If wb.Sheets(destinationSheetName).Cells(1, count).Value <> Workbooks(FileName).Sheets(importFile).Cells(1, count).Value Then
                    wb.Sheets(destinationSheetName).Cells(1, count).ClearContents
                End If
            Next count
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
How about
VBA Code:
   With wb.Sheets(destinationSheetName)
      lastcolumn = .Cells(columnheader, Columns.Count).End(xlToLeft).Column
      For Count = 1 To lastcolumn
         If .Cells(1, Count).Value <> Workbooks(Filename).Sheets(importFile).Cells(1, Count).Value Then
            .Cells(2, Count).Resize(.Rows.Count - 1).ClearContents
         End If
      Next Count
   End With
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
What am I missing? Why not just this inside the With..End With block?.
VBA Code:
.Range(.Rows(2), .Rows(Rows.Count)).ClearContents
 
Upvote 0
That would clear all the columns, I think the OP is only try to clear some of them.
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,155
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top