Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop through columns and clear contents
#1
Example:

Sub ClearContentsInColumns()
    Dim ws As Worksheet
    Dim lastColumn As Long
    Dim col As Long

    'Set the worksheet where the data is located
    Set ws = ThisWorkbook.Sheets("Sheet1")

    'Find the last column with data in the worksheet
    lastColumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

    'Loop through each column and clear the contents
    For col = 1 To lastColumn
        ws.Columns(col).ClearContents
    Next col
End Sub

Explanation:

The code defines a subroutine named ClearContentsInColumns.
It declares variables for the worksheet (ws), the last column with data (lastColumn), and a loop counter (col).
Set ws = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the data is located.
The lastColumn variable is determined using ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column, which finds the last column with data in the worksheet.
The For loop iterates through each column from 1 to lastColumn.
ws.Columns(col).ClearContents clears the contents of each cell in the current column (col) of the worksheet.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concatenate data in columns Qomplainerz 0 215 07-27-2023, 11:01 AM
Last Post: Qomplainerz
  Loop through worksheets and print their names in the immediate window Qomplainerz 0 258 07-27-2023, 09:42 AM
Last Post: Qomplainerz
  Loop to fill numbers in cells with VBA Qomplainerz 0 228 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Loop to display numbers in VBA Qomplainerz 0 226 07-27-2023, 07:33 AM
Last Post: Qomplainerz
  Do Until loop in VBA Qomplainerz 0 177 07-26-2023, 08:34 PM
Last Post: Qomplainerz
  Do While loop in VBA Qomplainerz 0 193 07-26-2023, 08:33 PM
Last Post: Qomplainerz
  For...Next loop in VBA Qomplainerz 0 207 07-26-2023, 08:32 PM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 1 Guest(s)