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
Solved

Code not working

Posted on 2013-05-21
2
215 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 81

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

856 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