Add a column before last column of table

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
120
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Here I come again with my questions.

I'm trying to add a column before the last column of a table and insert the name of the column with a value that is inputed by the user.

How can I do that?

Already have code to check if already exists a column with that name in the table, if there are none, find the last column, but can't make the rest of the code.

What I have righ now:

VBA Code:
Dim tblObrasCounter As Integer
Dim lastCol As String

tblObrasCounter = WorksheetFunction.CountIf(Worksheets("Resumo").ListObjects("ResumoKM").Range.Rows(1), MyInputObra)

If tblObrasCounter = 0 Then
    With Worksheets("Resumo").ListObjects("ResumoKm")
         lastCol = Worksheets("Resumo").ListObjects("ResumoKM").Range.Columns.Count
    End With
End If

Can anibody help me, please?

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.
Well, found my solution.

VBA Code:
If tblObrasCounter = 0 Then
     With Worksheets("Resumo").ListObjects("ResumoKm")
          lastCol = Worksheets("Resumo").ListObjects("ResumoKM").Range.Columns.Count
          Worksheets("Resumo").ListObjects("ResumoKM").ListColumns.Add(lastCol).Name = MyInputObra
     End With
End If

Already tested it, and it's working, so, now just need to tidy this code, and write the code to delete a column it the heather isthe same as the value inputed by the user.

Thank you all
 
Upvote 0
Solution

Forum statistics

Threads
1,223,868
Messages
6,175,084
Members
452,611
Latest member
bls2024

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