Finding Position of Row VBA

WindsorKnot

Board Regular
Joined
Jan 4, 2009
Messages
160
Hi,

I'm trying to do the equivalent of Row() in VBA, but am having some trouble.

My code is below and I keep gettin an error message 13, type mismatch. Any help would be appreciated.

Code:
Sub GetRowPosition()
    Dim I As Integer
    Dim J As Integer
    
    For I = 1 To 100
        If Cells(I, "B").Value = "Fill" Then
            J = Application.Rows(I)
            Exit For
            J = J + 1
        End If
    Next I
    
MsgBox J
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi Windsor,

If you're just trying to get the row where "Fill" is found, you don't need nearly that much code. Either of the macros below would suffice:
Code:
Sub GetRowPosition()
Dim I As Long
For I = 1 To 100
    If Cells(I, "B").Value = "Fill" Then MsgBox I
Next
End Sub
 
Sub GetRowPosition2()
MsgBox Range("B1:B100").Find("Fill", Range("B1"), xlValues, _
    xlWhole, xlByRows, xlNext).Row
End Sub
 
Upvote 0
The type mismatch is in "J = Application.Rows(I)"
Since the keyword Set was not used, Excel attempts to set J to Application.Rows(i).VALUE, which is an array.
Dimensioning J to Variant will result in J being typename Variant() after that statement.

You might try
Code:
Dim J as Long

J = Val(CStr(Application.Match("Fill", Range("B1:B100"), 0)))
 
Last edited:
Upvote 0
Hi Windsor,

If you're just trying to get the row where "Fill" is found, you don't need nearly that much code. Either of the macros below would suffice:
Code:
Sub GetRowPosition()
Dim I As Long
For I = 1 To 100
    If Cells(I, "B").Value = "Fill" Then MsgBox I
Next
End Sub
 
Sub GetRowPosition2()
MsgBox Range("B1:B100").Find("Fill", Range("B1"), xlValues, _
    xlWhole, xlByRows, xlNext).Row
End Sub
I'm obviously not aware of what the OP's data is like, but the original code would find the first row containing "Fill". So, if it is possible that there are multiple "Fill" values, Sub GetRowPosition2() may need this alteration
Rich (BB code):
MsgBox Range("B1:B100").Find("Fill", Range("B100"), xlValues, _
    xlWhole, xlByRows, xlNext).Row
And if it is possible there are no "Fill" rows it would clearly need more to deal with that as well.
 
Upvote 0

Forum statistics

Threads
1,223,053
Messages
6,169,831
Members
452,284
Latest member
TKM623

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