?
Solved

Code not working

Posted on 2013-05-21
2
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

764 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