Help with VBA to delete columns in new tabs

zlomchk

New Member
Joined
Apr 8, 2015
Messages
4
Hello,

I'm trying to make a macro that simply creates new tabs and assigns names to each by referencing a list. After doing so, I need it to delete Columns A:F per each new tab.

The macro works fine if I take out
Code:
Columns("A:F").Delete
but obviously I need that in there. Any ideas what to do? Thanks in advance. Here's the code:

Code:
Sub Macro2()
'
' Macro2 Macro

Dim i As Integer
Dim last_SUP As Integer
Dim SUP_nme As String

i = 2

Set sht = ThisWorkbook.Worksheets(Sheet2.Name)
last_SUP = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

Do While i <= last_SUP

    SUP_nme = Cells(i, 1).Value
    Columns("A:F").Delete
    Sheets("SUP List").Copy After:=Sheets(1)
    Sheets("SUP List (2)").Name = SUP_nme
    
    
    i = i + 1
Loop
    
    
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
It sounds like you want to delete columns A-F after you create the new sheet. If that is the case, try this:

Code:
  SUP_nme = Cells(i, 1).Value
  Sheets("SUP List").Copy After:=Sheets(1)
  Sheets("SUP List (2)").Name = SUP_nme
  Sheets(SUP_nme).Columns("A:F").Delete
  i = i + 1


Tim
 
Upvote 0
Tim,

That returns a Run-time error '1004': Application-defined or object-defined error. This is located on the line containing:
Code:
Sheets("SUP List (2)").Name = SUP_nme

Thanks for taking time to help!
 
Upvote 0
Welcome to the Board!

I'd use ActiveSheet.Name = SUP_nme instead, since a newly copied sheet will always be the active sheet, so you don't need to worry about its name.

HTH,
 
Upvote 0
Hm, that still returned the same error on that line.

Thanks for the help, that is useful idea.
 
Upvote 0
Is SUP_nme a valid name?

Note that you're not explicitly referencing sht with SUP_nme, so it might not even have a value, which would cause an error.
 
Upvote 0
I think you are right, it returns as blank; do you have any suggestions? Not sure how to work it to reference sht with SUP_nme
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,460
Members
448,965
Latest member
grijken

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