Declaring a Variable

bhsoundman

New Member
Joined
Jul 17, 2010
Messages
32
Office Version
  1. 365
Platform
  1. MacOS
Can someone tell me what I'm doing wrong? This macro returns a 1004 error. I know the problem is regarding addr. Thanks in advance

Sub Venue_Rolodex()

Dim venue As String
Dim finalrow As Integer
Dim i As Integer
Dim addr as string

venue = Sheets("rolo").Range("f1").Value
finalrow = Sheets("rolo").Range("a1000").End(xlUp).Row
addr = Sheets("Rolo").Range("A" & i).Value


For i = 2 To finalrow
If Cells(i, 1) = venue Then
Range("G5").Value = addr

End If

Next i
Application.CutCopyMode = False
Range("f1").Select

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
There are a few issues with your code but try this.
VBA Code:
Sub Venue_Rolodex()

    Dim venue As String
    Dim finalrow As Long
    Dim i As Long

    With Sheets("rolo")
        venue = .Range("F1").Value
        finalrow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 2 To finalrow
            If .Cells(i, 1).Value = venue Then
                .Range("G5").Value = .Cells(i, 1).Value
                Exit For
            End If
        Next i
    End With

End Sub
 
Upvote 0
There are a few issues with your code but try this.
VBA Code:
Sub Venue_Rolodex()

    Dim venue As String
    Dim finalrow As Long
    Dim i As Long

    With Sheets("rolo")
        venue = .Range("F1").Value
        finalrow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 2 To finalrow
            If .Cells(i, 1).Value = venue Then
                .Range("G5").Value = .Cells(i, 1).Value
                Exit For
            End If
        Next i
    End With

End Sub
Thanks so much for the clean up! Is using addr not advised?

Thanks again!
 
Upvote 0
Thanks so much for the clean up! Is using addr not advised?

Thanks again!
It is not that it's not advised but your syntax isn't doing what you expected it to be doing. Two issues:
You declared addr outside the loop so it doesn't increment with the row. It should be inside the for loop.
Secondly, as it's not getting incremented, the default value for declaring an integer is 0 so you're assigning the value of cell A0 to addr but cell A0 doesn't exist.
 
Upvote 0
It is not that it's not advised but your syntax isn't doing what you expected it to be doing. Two issues:
You declared addr outside the loop so it doesn't increment with the row. It should be inside the for loop.
Secondly, as it's not getting incremented, the default value for declaring an integer is 0 so you're assigning the value of cell A0 to addr but cell A0 doesn't exist.
Thanks for the explanation! I understand now.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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