Help continuing vba code onto a new line..

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
Hello,

I have some code and I can't figure out how to continue it on a new line. I have to be able to do it somehow otherwise it's going to be a really long (horizontally) row. I've tried a bunch of combinations of " _ , "_, _, etc and nothing seems to work for some reason. Any help would be much appreciated.

Code:
dim Resp as string
 
.CommandText = "Select " & Resp & ".X FROM " & Resp & ".txt " & Resp & " WHERE " & Resp & ".Tel=1492"

That code works fine, but I need to add multiple more Selected columns to pull. Currently that code is all in one row. So I'd like to be able to do something like what I indicated below, but nothing seems to work.

Code:
dim Resp as string
 
.CommandText = "SELECT" & Resp & ".X, " & Resp & ".Y, " & Resp & ".Z, _
" & Resp & ".M, " & Resp & ".N, " & Resp & ".O _
FROM " & Resp & ".txt " & Resp & " _ 
WHERE " & Resp & ".Tel=1492"

This would be ideal, where all the columns I'm selecting could be on a few rows, then the FROM part on a separate row, then the WHERE part on another row.

Is this possible?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
PS- I tried using the record feature and it looks like you can split it on different lines, but it creates the same code in a very different way using arrays and other things and there are some rows that are blank except for , _ so I'm still thinking there must be a clearer way.
 
Upvote 0
Hello

try like this, you can't split a line inside double quotes.

Code:
Sub test()
  .CommandText = "Select " & Resp & _
  ".X FROM " & Resp & _
  ".txt " & Resp & _
  " WHERE " & Resp & _
  ".Tel=1492"
End Sub
 
Upvote 0
hi.

2 things.

1) the line continuation characters is simply space then underscore<SPACE><UNDERSCORE>
You're probably getting the ,_ type of thing from the recorder because of a multiple input function breaking after the ,
2) You can't put the line continutation within a string: the editor will ignore it.

Code:
    .CommandText = "SELECT" & ResP & ".X, " & ResP & ".Y, " & ResP & ".Z," _
            & ResP & ".M, " & ResP & ".N, " & ResP & ".O " _
            & "FROM " & ResP & ".txt " & ResP _
            & "WHERE " & ResP & ".Tel=1492"
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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