Deleting the same column nr's across multiple sheets based on dynamic cell values

Viper147

New Member
Joined
Apr 19, 2018
Messages
34
Good day all
Been working on this piece of code that is supposed to delete a dynamic range of columns based on values in a range of cells that will differ from time to time, across multiple sheets. The values will be the headings of each column and the column numbers will be the same across all the worksheets, which it makes a little easier. Also, the sheets will be all worksheets, not just a select few. I've now tried various options like arrays to loops and just cannot get it to delete the columns in the rest of the sheets.

Below is the piece of code that I have now, but getting stuck on the line noted below with a Type mismatch error. Thanks in advance for the assistance.

VBA Code:
Option Explicit
Sub DeleteColumns()
    Dim VarArr As Variant
    Dim u As Range
    Dim Cel As Range
    Dim TestRng As Range
    Dim TestSht As Variant
    Dim b As Integer
    Dim ws As Worksheet
    Dim a As Long
    Dim rng As Range
    
    a = Range("M1").Value
    b = 1
    
    VarArr = Array(Range(Cells(1, 14), Cells(1, 14 + a - 1)))
    Set TestRng = Range(Cells(1, 1), Cells(1, 10))
    For Each Cel In TestRng
        If IsError(Application.Match(Cel.Value, VarArr, 0)) Then
            If Not u Is Nothing Then
              Set u = Union(u, Cel)
            Else
              Set u = Cel
            End If
        End If
    Next
    If Not u Is Nothing Then
    
    Set rng = u.Address 'Type mismatch error on this line
    For Each ws In Worksheets
        ws.Range(rng).EntireColumn.Delete
    Next ws
    
    End If
    
    End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hard to understand what you're trying to do by reading your code.
So, are you trying to delete the same column in all the worksheets in your workbook?
If so, what column are you wanting to delete in all worksheets?

If you're wanting to delete column 3 in all worksheets in your workbook the code would look like this:

VBA Code:
Sub Rectangle1_Click()
Dim i As Long
For i = 1 To Sheets.Count
    Sheets(i).Columns(3).Delete
Next
End Sub
 
Last edited:
Upvote 0
Try these two changes

Rich (BB code):
Dim rng As Range
Dim rng As String

Rich (BB code):
Set rng = u.Address
rng = u.Address
 
Upvote 0
Solution
Sorry about the cryptic post, should've expanded on the problem.
Nonetheless, I've tried the suggestions of Peter above and all works just fine now. Thank you both for taking the time to assist, greatly appreciate it.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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