473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

vba error : 'Object variable not set...'

I am attempting to automate Excel from Access.The code below works the first
time it is run but fails with the above error on subsequent attempts.To re run
the code successfully the database must be re opened.
The break occurs where the line is highlighted below.The problem seems to be with Excel's Selection Method.

Most grateful for any information on how to work round this problem

Expand|Select|Wrap|Line Numbers
  1.  Sub Combo0_AfterUpdate()
  2.     Dim xlApp As Excel.Application
  3.     Dim xlBook As Excel.Workbook
  4.     Dim xlWkSht As Excel.Worksheet
  5.     Dim MyRng As Excel.Range
  6.  
  7.     Dim strMyPath As String
  8.     Dim i As Integer
  9.     Set xlApp = New Excel.Application    ' ("Excel.Application")
  10.     Set xlBook = xlApp.Workbooks.Open(strMyPath)
  11.     Set xlWkSht = xlBook.Worksheets("Milk Production")
  12.  
  13.  
  14.     strMyPath = Forms!switchboard!txtFileLocation.Value & "\" &     Me.Combo0.Text
  15.     xlApp.Visible = True
  16.     With xlWkSht
  17.  
  18.         'xlApp.ScreenUpdating = False
  19.         .Activate 'goes to specific xl worksheet ,even if another worksheet in collection was
  20.         'active when workbook was saved and closed
  21.         .Range("h3").Value = "=right(c2,22)"
  22.         .Columns("A:A").Select
  23.         For i = 1 To 3
  24.             .Columns("A:A").Insert Shift:=xlToRight
  25.         Next
  26.         .Range("A5").Select
  27.         .Range("a5").Value = "ClientID"
  28.         .Range("b5").Value = "Farm"
  29.         .Range("c5").Value = "Year"
  30.         .Range("a6").Value = "=mid(k3,4,5)"
  31.         .Range("b6").Value = "=mid(k3,10,2)"
  32.         .Range("c6").Value = "=left(k3,3)"
  33.         .Range("a6:c6").Copy
  34.         .Range("a6:c6").PasteSpecial Paste:=xlPasteValues
  35.         .Rows("1:4").Select
  36.         .Rows("1:4").Delete
  37.         .Range("d2").Select
  38.         .Range("d2").End(xlDown).Select
  39.         .Application.ActiveCell.Offset(-2, -3).Range("a1:c1").Select
  40.         .Range(Selection, Selection.End(xlUp)).Select
  41.         Selection.FillDown
  42.     End With
  43.  
  44.     End Sub
Dec 24 '07 #1
3 1754
Killer42
8,435 Expert 8TB
Sorry to see you don't appear to be getting any response to this question.

I'm afraid it's a bit beyond what I've done in VBA, but I do wonder - is it possible that the changes made the first time 'round are changing the way the selection operates?




Note, I changed the asterisks to bolding, so it stands out. Oh, and another thing. I think you might find more VBA expertise in the Access forum than the VB one. No guarantees, but it might be worth a try.
Dec 29 '07 #2
The strMyPath variable is not assigned until after you use it to open the workbook.
How are you getting the workbook to open using an empty variable?
'------------------

I am attempting to automate Excel from Access.The code below works the first ...
Dec 30 '07 #3
Killer42
8,435 Expert 8TB
Good point, ubentook. I missed that.
Dec 31 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ike | last post by:
Anyone knows what cuases this under 6.0 ? -Ike
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
2
by: Mary | last post by:
Hello, I am having a problem with the cl compiler. I have written a C class (RegConnect.c) which uses Win32 API functions such as RegOpenKey, RegCloseKey etc. Initially when I was trying to...
7
by: William Apple | last post by:
Despite the fact this deals with webservices I believe it is a VB question. I am working on a test application that passes data to a webservice. The webservices takes a variable type that is...
0
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
4
by: majo | last post by:
Hi, I had posted this once earlier, but couldnt get my problem solved. When i run an asp.net application in windows 2003 server with IIS 6.0, it "SOMETIMES" gives me the below given error....
3
by: Richard Hollenbeck | last post by:
I've marked the line in this subroutine where I've been getting this error. It may be something stupid but I've been staring at this error trying to fix it for over an hour. I'm pretty sure the...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
3
by: Newbie19 | last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message: Object variable or With block variable not set. Description: An unhandled...
5
by: Al G | last post by:
Hi, I'm converting a bit of POP3 VB6 code to VB2005, and have run into this error with the following code. Can someone help me find out what I'm missing/doing wrong? 'holds the attachments...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.