Deleting Columns

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
756
Office Version
  1. 365
Hi,

I have 12 Sheets representing each month and are hidden .

And each sheet have current and previous month values for 2021.

And I want to prepare by clearing values.

Ex. February sheet Also has previous month which is January the values in January is a formula just want to delete current month values.

Can I do this using VBA?

Thank you
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi Josros60,

Is it possible to share the sheet and explain. It would help to provide solution quickly.

Thanks,
Saurabh
 
Upvote 0
each sheet has previous month and current month but previous is a formula current enter manually what I want is to delete all values for all current column the enter manually in each month.
here sample spreadsheet (Will not let me select minisheet the adding giving an error 1004 message " Method GoTo of object_Application failed", that's why pasting table.

SAGE IDVendor NameAccount Number Dec Jan
M.A.D.M.A.D.E. MEDIA INCM.A.D.#REF!
MASAHMMASROOR AHMAD LONEMASAHM#REF!
MDM BUMDM BUSINESS SOLUTIONSMDM BU#REF!
MELTWAMELTWATER SOCIAL SOLUTIONSMELTWA#REF!
METASWITMETASWITCH NETWORKSMETASWIT#REF!
METASCADMETASWITCH NETWORKS LTD.METASCAD#REF!
METCALMETCALFE REALTY COMPANY LIMITEDMETCAL#REF!
MICROPEPMICROPEER SOLUTIONSMICROPEP#REF!
MICROSMICROSOFT ONLINE INC.MICROS#REF!
MLT AIMLT AIKINS LLPMLT AI#REF!
MODERNMODERN NIAGARA BUILDING SERVICES A DIV OF MODERN NIAGARA TORMODERN#REF!
SHUKLAMOHIT SHUKLASHUKLA#REF!
MOOREMOORE CANADA CORPORATION (Marketing)MOORE#REF!
NETALINETALICO LLCNETALI#REF!
NETTOYNETTOYEUR LAPLANTE CLEANING INC.NETTOY#REF!
NTIRETNTIRETY INCNTIRET#REF!



thank you
 
Upvote 0
Hi,

As per my understanding you would like to delete the last column having current month data.

Is the current month always in the last column of the sheet?

Thanks,
Saurabh
 
Upvote 0
If you'd like to delete the last column of any specific sheet, try the following, which has two options:

VBA Code:
Sub Gone()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") 'Modify as needed
Dim lCol As Long: lCol = ws.UsedRange.Columns(ws.UsedRange.Columns.Count).Column

  'option 1 (deletes column and shifts all subsequent data left
    ws.Columns(lCol).Delete Shift:=xlToLeft
 
  'option 2 (clears column contents and leaves column in place)
    ws.Columns(lCol).ClearContents

End Sub
 
Upvote 0
Hi,

To delete last column from all sheets, use below code:

VBA Code:
Sub delColumn()
Dim totalSheets As Integer, colToDel As Integer, shNum As Integer

totalSheets = ThisWorkbook.Sheets.Count

For shNum = 1 To totalSheets
    colToDel = Sheets(shNum).Cells(1, Columns.Count).End(xlToLeft).Column
    Sheets(shNum).Columns(colToDel).Delete
Next
End Sub
 
Upvote 0
Sorry my English no that good just to clear the contents no the columns.

Thank you
 
Upvote 0
Some assumptions that I have made. Please give details of any that are not correct assumptions.
  • The worksheet names are full month names. eg 'January', not 'Jan' or '1' etc
  • The data is part of a formal Excel table (ListObject) as it looks like that from the colour banding in your XL2BB sheets
  • The column to clear is the final column in the ListObject table
  • You do not want the heading from that column deleted

If all of those are correct, then try this with a copy of your workbook.

VBA Code:
Sub Clear_Column()
  Dim i As Long
  
  For i = 1 To 12
    With Sheets(MonthName(i)).ListObjects(1).DataBodyRange
      .Columns(.Columns.Count).ClearContents
    End With
  Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,665
Messages
6,056,638
Members
444,879
Latest member
suzndush

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