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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
use double quotes like: Range(""M4:AF23"")
 
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,319
Members
449,154
Latest member
pollardxlsm

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