[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

object required

Posted on 2012-09-05
2
Medium Priority
?
503 Views
Last Modified: 2012-09-05
I am trying to loop through all csv files in a directory and successively read them into sheets in new instances of a template. So first file will open in the  first instance of the template, then second file will open in a second instance of the  instance of the template, and the third file will open in a third instance of the template.
I am getting an error of "object required" pointing to the line:
With sh.QueryTables.Add(Connection:="TEXT;" & fName, Destination:=ws.Range("A1")) 'it says "object required"
   

Here is the code I am running:
 Sub Go()

Dim sh As Worksheet, sPath As String, sName As String
Dim fName As String
Set sh = ActiveWorkbook.Sheets("perST") 'set to current worksheet name
sPath = "C:\Users\Bill\Desktop\testLongTermTest\"
sName = Dir(sPath & "*.csv")
Do While sName <> ""
 'Set fName = sPath & sName
 fName = sPath & sName
  With sh.QueryTables.Add(Connection:="TEXT;" & fName, Destination:=ws.Range("A1")) 'it says "object required"
     .TextFileParseType = xlDelimited
     .TextFileCommaDelimiter = True
     .Refresh
 End With
 Workbooks.Add Template:= _
        "C:\Users\Bill\AppData\Roaming\Microsoft\Templates\sizesWithFormV2.xltm" 'for each file in the directory, I want to open it in a new instance of the template.
 sName = Dir()
Loop

 End Sub

Open in new window


Anyone know what is wrong here?
Thank you.
  Sub Gomi()

Dim sh As Worksheet, sPath As String, sName As String
Dim fName As String
Set sh = ActiveWorkbook.Sheets("perStockTweets") 'set to current worksheet name
sPath = "C:\Users\Bill\Desktop\testLongTermTest\"
sName = Dir(sPath & "*.csv")
Do While sName <> ""
 'Set fName = sPath & sName
 fName = sPath & sName
  With sh.QueryTables.Add(Connection:="TEXT;" & fName, Destination:=ws.Range("A1")) 'it says "object required"
     .TextFileParseType = xlDelimited
     .TextFileCommaDelimiter = True
     .Refresh
 End With
 Workbooks.Add Template:= _
        "C:\Users\Bill\AppData\Roaming\Microsoft\Templates\sizesWithFormV2.xltm" 'for each file in the directory, I want to open it in a new instance of the template.
 sName = Dir()
Loop

 End Sub

Open in new window

0
Comment
Question by:willie108
2 Comments
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 2000 total points
ID: 38370868
Hi

You haven't dimmed 'ws'.

may be replace
ws.Range("A1")

Open in new window

with
sh.Range("A1")

Open in new window

0
 

Author Comment

by:willie108
ID: 38370918
Thanks!!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question