VBA Code help

superfb

Active Member
Joined
Oct 5, 2011
Messages
251
Office Version
  1. 2007
Platform
  1. Windows
Hi,

I have this code that helps to insert a column in various tables (copies and then paste special the data)

Is there a way to adjust this code to delete the previous column? in stead off inserting the column - bare in mind the last column has formulas, i would like the column before this to be deleted

VBA Code:
    Sub Insert_Col()
Dim ws As Worksheet
Dim lr As Long
Dim lc As Long
Dim myrng As Range
Dim myvar As Variant

Application.ScreenUpdating = False
On Error GoTo Oops:
'assuming there are are no sheets that need to be ignored ???
For Each ws In ThisWorkbook.Sheets

Select Case ws.Name

Case "Main Menu", "Notes", "Contents", "Table 8", "Table 10_SOC10", "Table 10_SOC00", "Table 7_SIC03", "Table 11", "Table 13", "Table 14" ' Sheets to be ignored
GoTo Ignore:

Case Else  'Otherwise do insert
    With ws
    
        'last column  by interrogating row 3!!!!!!!!!!
        lc = .Cells(3, Columns.Count).End(xlToLeft).Column
        'last row
        lr = .Cells(Rows.Count, lc).End(xlUp).Row
      
        'range of interest
        Set myrng = .Range(.Cells(1, lc), .Cells(lr, lc))
        'Assign range values to array myvar
        myvar = myrng.Value
        'insert a new column
        myrng.Columns(1).EntireColumn.Insert
        ' range values to new column
        myrng.Offset(0, -1).Value = myvar
        'Copy format to new column
     myrng.Copy
     myrng.Offset(0, -1).PasteSpecial xlPasteFormats
     Application.CutCopyMode = False
     myrng.Offset(0, -1).ColumnWidth = myrng.ColumnWidth
     
    End With

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
superfb said:
I have this code that helps to insert a column in various tables

Are you really working with Tables or are they ranges of data on different sheets...
 
Upvote 0
Are you really working with Tables or are they ranges of data on different sheets...
Sorry not sure what you mean, but if this helps i have almost 15 tabs in a workbook, all different tables that have an array formula linked to various OTHER workbooks - when i open them and press F9 it will do the calculation. As i need a snapshot of that data according to date, i use the above code to copy it in to the column before.

However, at a later date...that data gets revised, due to this i want to delete the column before the formula column (if that make sense) then repeat the process described just now!

I hope that helps
 
Upvote 0
Ok, look at the attached link and in the section- How Do I Find Existing Tables? Go to Step 4, and see what that code does for you.

Also, this is very good reference in general for working with Tables...

 
Upvote 0
Ok, look at the attached link and in the section- How Do I Find Existing Tables? Go to Step 4, and see what that code does for you.

Also, this is very good reference in general for working with Tables...

Thank you - this would be helpful but im not using the table as per the excel, but i have created a table summary manually
 
Upvote 0

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

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