473,395 Members | 1,401 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,395 software developers and data experts.

Excel Error 1004 in vba vlookup function

I hope I haven't posted this in the wrong section, but it is a VBA problem, so please forgive me if it should be elsewhere.
I have two tabs in a spreadsheet, both containing lists of parts codes. I want to programatically compare the first list against the second, and where the code isn't found in the second list I want to append it to the end of the list.
I have tried to trap the error 1004 that is raised when the part cannot be found in the list, and have created an on error goto jump to handle it, and this works on the first time it encounters the problem, but as soon as it hits it again, the error appears and the code stops.

Here is the section of code i have written so far.

Expand|Select|Wrap|Line Numbers
  1.     Sheets("Main").Select
  2.     Range("A1").Select
  3.     Range(Selection, Selection.End(xlDown)).Select
  4.     Selection.Name = "AllParts"
  5.     Sheets("ImportList").Select
  6.     Range("A1").Select
  7.     On Error GoTo AddPart
  8.     FRow = Cells(Application.Rows.Count, 1).End(xlUp).Row
  9. AddPart:
  10.     If Err.Number = 1004 Then
  11.     myPartCode = ActiveCell.Value
  12.     With Main
  13.     With .Cells(.Rows.Count, 1).End(xlUp)(2, 1)
  14.         .Value = myPartCode
  15.     End With
  16.     End With
  17.     End If
  18.  
  19.     ActiveCell.Offset(1, 0).Select
  20.     myPartCode = ""
  21.     On Error GoTo AddPart
  22.  
  23.     For i = ActiveCell.Row To FRow
  24.     myPartCode = Application.WorksheetFunction.VLookup(ActiveCell, Range("AllParts"), 1, False)
  25.  
  26.     Debug.Print myPartCode
  27.     ActiveCell.Offset(1, 0).Select
  28.     Next i
  29.  
The sheet named Main also has its codename set to Main.

I cannot understand why the on error captures it once, then doesn't capture it the second time.
This is completely baffling me, ane left me pulling out my hair.
Jan 10 '12 #1

✓ answered by NeoPa

Error code is supposed to reset the error. One of the Resume statements should be used. Using On Error to handle program flow is only an acceptable idea when you have a very good understanding of the Error handling facilities in VBA.

I would recommend Error Code that does what's expected and manage the flow of the logic entirely separately.

1 4016
NeoPa
32,556 Expert Mod 16PB
Error code is supposed to reset the error. One of the Resume statements should be used. Using On Error to handle program flow is only an acceptable idea when you have a very good understanding of the Error handling facilities in VBA.

I would recommend Error Code that does what's expected and manage the flow of the logic entirely separately.
Jan 11 '12 #2

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

Similar topics

0
by: Mark C | last post by:
All, I have used Excel automation many times in the past without error until now. My machine has both Access 97 and XP installed but this code is written in 97 and I am using Excel XP. The code...
2
by: dailem | last post by:
I have a bit of code tied to a command button in Access that runs a query then transfers it to a new Excel worksheet (& new file). It works fine EVERY OTHER TIME THAT I RUN IT....what the 'heck...
3
by: Maverick | last post by:
I'm new to this, but I have received so much great information here, I had to join. Here is a problem am encountering with the following code in MS Access: objSht.Activate ...
0
by: Jono | last post by:
Hello, I've been getting this message when closing excel (not necessarily when closing the workbook by itself, but when closing Excel and the workbook at the same time): ...
0
by: geiaaa | last post by:
hello and happy new year... I just started a project were I need to call dll functions from within excel. More specifically I have created a custom dll in Visual Basic (visual studio 2005) with the...
2
by: amolbehl | last post by:
I use VB6.0 and when I execute the code I have given below, I get runtime Error 1004 - Specified value is out of range. Set oXL = CreateObject("Excel.Application") oXL.Visible = True ...
4
by: ielamrani | last post by:
Hi, I am getting this error when I try to export to an excel sheet. When I click on a button to export the first time it's fine, I rename the exported excel sheet and I try to export it again and I...
0
by: grego9 | last post by:
When I run the following macro in Excel (using VBA) I get the run time error 1004 application defined or object defined error. I get the usual debug message and when I click end I actually get the...
5
by: titli | last post by:
Hi guys, I have a .mdb which generates , further some mini .mdbs..Later these generated mini mdbs are provided as input to excel viewer.Till yesterday everything was working fine.. But today after...
0
by: Balaji Gurunathan | last post by:
Hi All, I am exporting a datatable to an excel sheet in an existing workbook. In another worksheet in the same workbook, i am using vlookup function that uses data in the exported worksheet. The...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.