Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application Defined or Object Defined Error 1

Status
Not open for further replies.

bajo71

Programmer
Aug 6, 2007
135
US
Hello,

Using Excel as an automation client, I have the following code that loops through a directory and copies data from a set of workbooks. However, upon trying to reference the newly Set workbook I receive the above referenced error.
Any ideas?

While strFile <> ""
Set wb = xlObj.Workbooks.Open(strPath & strFile)
Set ws = wb.Sheets("Datax")
With wb.ActiveSheet
FinalRow = .Cells(Rows.Count, 1).End(xlUp).Row
FinalCol = .Cells(1, Columns.Count).End(xlToLeft).Column
If FinalCol > 26 Then FinalColLtr = Chr(Int((FinalCol - 1) / 26) + 64) & Chr(((FinalCol - 1) Mod 26) + 65)
If FinalCol < 27 Then FinalColLtr = Chr(FinalCol + 64)
Range("A1", FinalColLtr & FinalRow).Copy
End With
 
Which line of code raises the error ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Is the sheet, Datax, the one you expect to be active? If so, I suggest you change
With wb.ActiveSheet
to
Code:
With [red]ws[/red]

_________________
Bob Rashkin
 
[tt][!].[/!]Range("A1", FinalColLtr & FinalRow).Copy[/tt]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Change
Code:
FinalCol > 26
to
Code:
FinalCol >= 26
and
Code:
FinalCol < 27
to
Code:
FinalCol <= 27

as your code is now, if FinalCol=26 or 27 FinalColLtr=0

ska
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top