Adding a sub using vba code

Kabasauls

New Member
Joined
Jul 12, 2012
Messages
24
Hello,

I am trying to write a module that runs when a new page is created. I want it add a sub that activates before you double click a cell. This is the code that I have wrote so far. It does not let me run it because of a syntax error.

Code:
Sub AddCodeForRange()
    With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
        N = .CountOfLines
        .InsertLines N + 1, "Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)"
        .InsertLines N + 2, vbNewLine
        .InsertLines N + 3, vbTab & "If Not Application.Intersect(Target, Range("M4:AF23")) Is Nothing Then"
        .InsertLines N + 4, vbTab & vbTab & "Cancel = True"
        .InsertLines N + 5, vbTab & vbTab & "InputFrm.Show"
        .InsertLines N + 6, vbTab & "End If"
        .InsertLines N + 7, vbNewLine
        .InsertLines N + 8, "End Sub"
    End With
    
End Sub

This line:

Code:
        .InsertLines N + 3, vbTab & "If Not Application.Intersect(Target, Range("M4:AF23")) Is Nothing Then"

It gives me the error becuase of the quotation marks I'm using for "M4:AF23". The problem is that I need them to be in the code, so it reads the range correctly. Is there a way to fix this issue??

Thank you for your time.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
use double quotes like: Range(""M4:AF23"")
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,362
Members
449,155
Latest member
ravioli44

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