Insert new row & column with VBA

dannyok90

Board Regular
Joined
Aug 30, 2016
Messages
115
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
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
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:
Upvote 0
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?
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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