Getting RunTime Error 1004

get2noesks

New Member
Joined
Jul 15, 2015
Messages
24
Hi,

I am trying to make a collection of rows and columns on a successful search of the text found. While executing the code I get a run time error message "Run-time error '1004' Application Defined or object defined error".

Code is as followed:

Function getValuesToSort() As Collection
Dim findString As String
Dim cntrlRow, cntrlCol As Integer
Dim WS1 As Worksheet
Dim WS2 As Worksheet

Dim var As Collection
Set var = New Collection

Set WS1 = Worksheets("Sheet1")
Set WS2 = Worksheets("Sheet2")

cntrlRow = 3
cntrlCol = 2

Dim iRow, iCol, i, j As Integer

iRow = iCol = 1

While WS2.Cells(cntrlRow, cntrlCol) <> ""
findString = WS2.Cells(cntrlRow, cntrlCol)
While WS1.Cells(iRow, iCol).Value <> ""
If findString = WS1.Cells(iRow, iCol) Then
var.Add (iRow)
var.Add (iCol)
End If
MsgBox var
iCol = iCol + 1
Wend
cntrlRow = cntrlRow + 1
Wend
Set getValuesToSort = var
End Function


Please advise.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
please use CODE tags to display code, tell us where the code fails. 1004 normally means that something you expect to be referenced isn't obvious to the code, a sheetname or similar
 
Upvote 0
Hi there,

I am not exactly following, but did notice that where you have:

Rich (BB code):
Dim iRow, iCol, i, j As Integer

iRow, iCol and I are being declared as Variants. To have them as Integers (which I would suggest simply declaring them as Longs), it needs to be like this:
Rich (BB code):
Dim iRow As Integer, iCol As Integer, i As Integer, j As Integer

As you have things currently declared and because of:
Code:
iRow = iCol = 1

...at this line...
Code:
[font=Courier New][color=darkblue]While[/color] WS1.Cells(iRow, iCol).Value <> ""[/font]
...iRow has been sub-typed to a Boolean and iCol is Empty, so it falls over.

With the variables all dim'ed to Long (or Integer), iRow will return 0 at this point, as where iRow is assigned the return of iCol = 1 (which it doesn't, as iCol currently is 0), so iRow will be zero after converting the FALSE return to it's numeric equivalent (which I just misspelled). This will of course still fall over, as Row or Column being 0 will fail.

Anyways, hope that helps a little at least,

Mark
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top