Insert new row & column with VBA

dannyok90

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

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
11,291
Office Version
2013
Platform
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
105
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
11,291
Office Version
2013
Platform
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:

Forum statistics

Threads
1,082,297
Messages
5,364,372
Members
400,793
Latest member
hamzayagiz

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top