Solved

Code not working

Posted on 2013-05-21
2
191 Views
Last Modified: 2013-05-22
I did not write this code.  Can someone tell me please what it is doing.  Is it looking for duplicates???

Sub AdRec
Dim r As Range, rw As Long
 
Set r = Sheet19.Range("A1")

If r(3, 1) <> "" Then
rw = FindRec("Cusip_tmp", 1, cusip)
r.Offset(rw, col) = r.Offset(rw, col) + 1

End Sub
__________________________________________
Function FindRec(w As String, numcol As Integer, lookval As String) As Long
Dim r As Range, numrows As Long
Dim I As Long

numrows = Worksheets(w).UsedRange.Rows.Count
Set r = Worksheets(w).Range("A1").Offset(0, numcol - 1).Resize(numrows, 1)
 On Error Resume Next
 I = -999
 I = WorksheetFunction.Match(lookval, r, 0)
 On Error GoTo 0
 
FindRec = I - 1
0
Comment
Question by:leezac
2 Comments
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 39186108
As posted, the code will likely fail because:
1. No End If in sub AdRec
2. Variables col and cusip not declared in AdRec. Nor do they appear to have values.
3. Sheet19 is a codename, not a tabname. If you don't know the difference, you should be using the tabname instead.
4. In function FindRec, the code will throw the user into the debugger if a match isn't found.
5. The value for numrows is reliable only if the data starts on row 1
6. There is no test for a negative value of rw when FindRec returns to AdRec. A run-time error will result if that happens.

I've rewritten the code to avoid the above issues. I believe that it is checking worksheet Cusip_tmp column A for the value of cusip. FindRec returns the row number -1 of that match. Adrec then increments the value of a certain cell in that row by 1.
Sub AdRec()
Dim r As Range
Dim col As Long, rw As Long
'col=1      'Need a value for col. It probably shouldn't be 1
Set r = Worksheets("Cusip_tmp").Range("A1")

If r(3, 1) <> "" Then
    rw = FindRec("Cusip_tmp", 1, "cusip")
    If rw > 0 Then
        r.Offset(rw, col) = r.Offset(rw, col) + 1
    End If
End If
End Sub
__________________________________________
Function FindRec(w As String, numcol As Integer, lookval As String) As Long
Dim r As Range, numrows As Long
Dim I As Long

numrows = Worksheets(w).UsedRange.Row + Worksheets(w).UsedRange.Rows.Count - 1
Set r = Worksheets(w).Range("A1").Offset(0, numcol - 1).Resize(numrows, 1)
 On Error Resume Next
 I = -999
 I = WorksheetFunction.Match(lookval, r, 0)
 On Error GoTo 0
 
FindRec = I - 1
End Function

Open in new window

0
 

Author Comment

by:leezac
ID: 39187632
Thank you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now