Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Looping question

I have the following code and I am experiencing a Run-time error 1004
Application-defined or object-defined error. The loop executes fine
the first time through but fails on the second pass. Any ideas are
appreciated. MyVar is valid the second time around too.

Public Sub Parse()
Dim myArr, myVar As Variant
Dim Unit As Variant


Set MyRange = Worksheets("Units").Range("A1:B11")
myArr = Split(ActiveCell.Value, ",")

For Each myVar In myArr ' Iterate through the array

'What do we do with each element?
x = Application.WorksheetFunction.VLookup(myVar, MyRange, 2,
False)

Next

End Sub

Thanks,
Charles

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Looping question

The vlookup is not going to be happy if it does not find something... Try
this instead...

Public Sub Parse()
Dim myArr As Variant
Dim x As Variant
Dim myVar As Variant
Dim MyRange As Range
Dim rngFound As Range

Set MyRange = Worksheets("Sheet1").Range("A1:B11")
myArr = Split(ActiveCell.Value, ",")

For Each myVar In myArr ' Iterate through the array

'What do we do with each element?
Set rngFound = MyRange.Find(myVar).Offset(0, 1)
If rngFound Is Not Nothing Then MsgBox rngFound.Value

Next
end sub

"Charles" wrote:

I have the following code and I am experiencing a Run-time error 1004
Application-defined or object-defined error. The loop executes fine
the first time through but fails on the second pass. Any ideas are
appreciated. MyVar is valid the second time around too.

Public Sub Parse()
Dim myArr, myVar As Variant
Dim Unit As Variant


Set MyRange = Worksheets("Units").Range("A1:B11")
myArr = Split(ActiveCell.Value, ",")

For Each myVar In myArr ' Iterate through the array

'What do we do with each element?
x = Application.WorksheetFunction.VLookup(myVar, MyRange, 2,
False)

Next

End Sub

Thanks,
Charles


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Looping question

sorry just noticed one line is incorrect...

If Not rngFound Is Nothing Then MsgBox rngFound.Value

"Jim Thomlinson" wrote:

The vlookup is not going to be happy if it does not find something... Try
this instead...

Public Sub Parse()
Dim myArr As Variant
Dim x As Variant
Dim myVar As Variant
Dim MyRange As Range
Dim rngFound As Range

Set MyRange = Worksheets("Sheet1").Range("A1:B11")
myArr = Split(ActiveCell.Value, ",")

For Each myVar In myArr ' Iterate through the array

'What do we do with each element?
Set rngFound = MyRange.Find(myVar).Offset(0, 1)
If rngFound Is Not Nothing Then MsgBox rngFound.Value

Next
end sub

"Charles" wrote:

I have the following code and I am experiencing a Run-time error 1004
Application-defined or object-defined error. The loop executes fine
the first time through but fails on the second pass. Any ideas are
appreciated. MyVar is valid the second time around too.

Public Sub Parse()
Dim myArr, myVar As Variant
Dim Unit As Variant


Set MyRange = Worksheets("Units").Range("A1:B11")
myArr = Split(ActiveCell.Value, ",")

For Each myVar In myArr ' Iterate through the array

'What do we do with each element?
x = Application.WorksheetFunction.VLookup(myVar, MyRange, 2,
False)

Next

End Sub

Thanks,
Charles


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping question Jase Excel Discussion (Misc queries) 2 March 19th 08 02:03 PM
Looping Question lee New Users to Excel 3 August 4th 06 04:23 PM
Looping Question MWS Excel Discussion (Misc queries) 1 June 5th 06 07:23 PM
Looping Question MWS Excel Programming 3 November 12th 04 05:32 PM
Looping Question (I think?) MWS Excel Programming 2 October 27th 04 02:09 PM


All times are GMT +1. The time now is 03:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"