Insert new row & column with VBA

dannyok90

Board Regular
Joined
Aug 30, 2016
Messages
106
Hi All,

Im trying to incorporate an add new row and column funtion in my spreadsheet


How can i get the following bits of code to work independantly of each other?


The first part of the code works fine and adds a new row exactly where i want it but im struggling to get the second add column bit right?


I have used row 1 and numbered them using A1, A1+1, A1+2 etc to determine the last column.


I would like it to copy the last column and insert it then hide the copying column in the same way i did the rows.


Many Thanks :)

Dan


Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If (Target.Value = "Double click to add new row") Then
    Target.Worksheet.Unprotect ("TG1")
    Target.Offset(-1, 0).EntireRow.Copy
    Target.Worksheet.Range("A" & Target.Worksheet.Cells(Target.Worksheet.Rows.Count, "B").End(xlUp).Row - 1).Insert
    Target.Worksheet.Range("A" & Target.Worksheet.Cells(Target.Worksheet.Rows.Count, "B").End(xlUp).Row - 2).EntireRow.Hidden = False
    Target.Worksheet.Protect ("TG1")
    Cancel = True
End If
End Sub

____________________________________________________________________________________________________________

Private Sub Worksheet_BeforeDoubleClick2(ByVal Target As Range, Cancel As Boolean)
Dim LastCol As Integer
    With ActiveSheet
If (Target.Value = "Double click to add new") Then
    Target.Worksheet.Unprotect ("TG1")
    Target.Offset(-1, 0).EntireColumn.Copy
    Target.Worksheet.Range("A" & Target.Worksheet.Cells(Target.Worksheet.Rows.Count, "B").End(xlUp).Row - 1).Insert
    Target.Worksheet.Range("A" & Target.Worksheet.Cells(Target.Worksheet.Rows.Count, "B").End(xlUp).Row - 2).EntireRow.Hidden = False
    Target.Worksheet.Protect ("TG1")
    Cancel = True
End If
End Sub
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Is this what you want for the columns?

Code:
Private Sub Worksheet_BeforeDoubleClick2(ByVal Target As Range, Cancel As Boolean)
Dim LastCol As Integer
    With ActiveSheet
If (Target.Value = "Double click to add new") Then
    Target.Worksheet.Unprotect ("TG1")
    Target.Offset(, -1).EntireColumn.Copy
    Cells(1, Columns.Count).End(xlToLeft).Offset(, -1).Insert
    Cells(1, Columns.Count).End(xlToLeft).Offset(, -2).EntireColumn.Hidden = False
    Target.Worksheet.Protect ("TG1")
    Cancel = True
End If
End Sub
 
Last edited:

dannyok90

Board Regular
Joined
Aug 30, 2016
Messages
106
Hi,

Yeah I think so! but because ive got two similar bits of code doing a similar thing cause neither of them are working working?
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
You would have to combine them into one procedure. You are only allowed one of each type event procedure per parent (sheet, workbook, UserForm) code module. The title line in the procedure tells the compiler which event to execute on for which parent object. If you have two procedures with the same title line doing different things, the compiler cannot tell which to execute, so it just ignores both, or errors.
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,127,632
Messages
5,626,000
Members
416,150
Latest member
Dawzzy

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
Top