Multiple lines of code

tracktor

Board Regular
Joined
Sep 13, 2009
Messages
62
I have this code
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
'This code puts a "g" in the cells that are cliked on
    If Target.Cells.Count > 1 Then Exit Sub
        If Not Intersect(Target, Range("A1:A338,D4,F4,H4,D5,F5,D6,F6,H6,J6,D7,F7,D9,F9")) Is Nothing Then
                If Target = "c" Then
 
                    Target = "g"
                Else
                    Target = "c"
                End If
        End If
End Sub

The complete Range of cells I have is over a few hundred. They are: A1:A338,D4,F4,H4,D5,F5,D6,F6,H6,J6,D7,F7,D9,F9,D25,F25,H25,D26,F26,D30,F30,D41,F41,H41,D54,F54,H54,D59,F59,D69,F69,H69,D84,F84,D116,F116,H116,J116,D120,F120,H120,J120,D124,F124,H124,J124,D125,F125,D127,F127,H127,J127,D133,F133,H133,D135,F135,D140,F140,H140,J140,D142,F142,H142,D145,F145,D147,F147,H147,J147,D149,F149,D154,F154,H154,J154,D156,F156,D163,F163,D165,F165,D167,F167,D169,F169,H169,J169,D170,F170,D172,F172,D174,F174,D180,F180,H180,J180,D181,F181,H181,J181,D182,F182,H182,J182,D183,F183,H183,J183,D184,F184,H184,J184,D186,F186,H186,D187,F187,H187,D188,F188,H188,D189,F189,H189,D190,F190,H190,D191,F191,H191,D193,F193,H193,D194,F194,H194,D195,F195,H195,D196,F196,H196,D197,F197,H197,D198,F198,H198,D199,F199,H199,D200,F200,H200,D201,F201,H201,D202,F202,H202,D203,F203,H203,D204,F204,H204,D205,F205,H205,D206,F206,H206,D208,F208,H208,D209,F209,H209,D210,F210,H210,D211,F211,H211,D212,F212,H212,D215,F215,H215,D218,F218,D219,F219,D220,F220,D228,F228,H228,D230,D231,D232,D233,D234,D235,D236,D237,D238,D240,D241,D242,D243,D245,F245,D251,F251,D255,B257,B258,B260,B261,B262,B263,B264,B265,B266,B267,B268,B269,B270,B271,B272,B273,B274,B275,B276

When I paste them into the code it is over multiple lines and I get a compile error.

How do I do this? And is there a better way to do what I am doing?

Appologize for my ignorance.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You might try this:

Select all those cells on your worksheet, then give that selection a Name (in the Name box to the left of the formula/function bar). Then in your code just reference that named range, e.g.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
'This code puts a "g" in the cells that are cliked on
    If Target.Cells.Count > 1 Then Exit Sub
        If Not Intersect(Target, Range("your_named_range")) Is Nothing Then
                If Target = "c" Then
                     Target = "g"
                Else
                    Target = "c"
                End If
        End If
End Sub
 
Upvote 0
Hi

A few thoughts on your cell range.

1, On copying the range into a spreadsheet there are "unknown characters" mainly at the "end of line" which may be causing your problem.

2, On trying a consolidation exercise the number of entries reduced from 238 to 108 and would give you an easier management of the range.

hth

Mike
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,167
Members
448,554
Latest member
Gleisner2

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