Compile Error

dtrynoski

New Member
Joined
Aug 23, 2017
Messages
9
I'm new to VBA in excel, I'm getting a compile error with the following code:

Range("M1, N1, O1, Q1, S1, T1, U1, V1, W1, X1, Z1, AB1, AC1, AD1, AE1, AF1, AG1, AI1, AK1, _
AL1, AM1, AN1, AO1, AP1, AR1, AT1, AU1, AV1, AW1, AX1, AY1, BA1, BC1, BD1, BE1, BF1, _
BG1, BH1, BJ1, BL1, BM1, BN1, BO1, BP1, BQ1, BS1, BU1, BV1, BW1, BX1, BY1, BZ1, CB1, CD1, _
CE1, CF1, CG1, CH1, CI1, CK1, CM1, CN1, CO1, CP1, CQ1, CR1, CT1, CV1, CW1, CX1, CY1, CZ1, _
DA1, DC1, DE1, DF1, DG1, DH1, DI1, DJ1, DL1, DN1, DO1, DP1, DQ1, DR1, DT1, DV1, DW1, DX1, _
DY1, DZ1").EntireColumn.Select

What am I doing wrong?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
two things.
Too many arguments and incorrect line breaks.

This works.

Code:
Sub t()
Range("M1, N1, O1, Q1, S1, T1, U1, V1, W1, X1, Z1, AB1, AC1, AD1, AE1, AF1, AG1, AI1, AK1," _
 & "AL1, AM1, AN1, AO1, AP1, AR1, AT1, AU1, AV1, AW1, AX1, AY1, BA1, BC1, BD1, BE1, BF1," _
 & "BG1, BH1, BJ1, BL1, BM1, BN1, BO1, BP1, BQ1, BS1, BU1, BV1, BW1, BX1, BY1, BZ1, CB1, CD1"). _
 EntireColumn.Select
End Sub

but it is only half the arguments.
 
Upvote 0
As explained, incomplete strings can not span broken lines.

You can shorten code like that since some spans are continuous.
Code:
Sub Test()
  Dim s As String, r As Range
  s = "M1:O1,Q1,S1:Z1,AB1:AI1,AK1:AP1,AR1,AT1:AY1," & _
    "BA1,BC1:BJ1,BL1:BZ1,CB1,CD1:CI1,CK1,CM1:CT1," & _
    "CV1:DA1,DC1,DE1:DJ1,DL1,DN1:DZ1"
  Set r = Range(s)
  r.EntireColumn.Select
End Sub

There is a maximum of 24 line continuation characters. https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/too-many-line-continuations

If more than 3 lines, I just concatenate the strings. Of course there is always Union() for joining ranges.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,254
Members
448,556
Latest member
peterhess2002

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