With Statement and Variables

saltkev

Active Member
Joined
Oct 21, 2010
Messages
324
Office Version
  1. 2013
Platform
  1. Windows
Hello

I wonder could someone help, I seem to be missing a trick. I have a variable which hold the following information. Lets call it variable1

Code:
ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\Users\keames1\Desktop\Configuration Item.csv", Destination:=Range("$A$1"))

If I then say

Code:
With Variable1

The code falls over, however if I write the statement in full still using "With" there are no problems.

Many Thanks

P.S The objective is to import a CSV file.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Re: With Statment and Variables

Where is your line of code assigning something to Variable1?
 
Upvote 0
Re: With Statment and Variables

Where is your line of code assigning something to Variable1?
In addition to Joe's question... did you Dim your variable and, if you did, as what data type?
 
Upvote 0
Re: With Statment and Variables

It might be best to post ALL of the code relevant to this task.
 
Upvote 0
Re: With Statment and Variables

If you mean you've put this string,
Code:
ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\Users\keames1\Desktop\Configuration Item.csv", Destination:=Range("$A$1"))
into a variable then all you have is a string and that can't be used in a With statement,
 
Upvote 0
Re: With Statment and Variables

Good Morning

Yes, I have tried dim with the variable as string and as an object, neither work. Norie has hit the nail on the head with my meaning. However if I type the "With" and the string everything works fine. I have copied the to a spread sheet cell and then cut & paste back to the code window everything is OK, so I feel sure the string held in the variable is OK. So is it possible to move forward by another method.

many thanks
 
Upvote 0
Re: With Statment and Variables

Good Morning

This is the full "With" Statement without the variable which works fine.

Code:
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\Users\keames1\Desktop\Configuration Item.csv", Destination:=Range("$A$1"))
        .Name = "Configuration Item"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
        1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

The file path & name in the first 2 lines will change this is the part I am trying to Automate.

Many thanks
 
Upvote 0
Re: With Statment and Variables

I'm sorry but it doesn't work that way, you might be able to replace the connection string with a variable but that's about it.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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