[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Code not working

Posted on 2013-05-21
2
Medium Priority
?
239 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

Independent Software Vendors: 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

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!
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

650 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