Solved

Target.Value Runtime Error

Posted on 2014-01-17
6
210 Views
Last Modified: 2014-01-23
I recently upgraded to Excell 2010 from Excell 2003 and I am getting a runtime error in the 2010 workbook on VBA code that works fine in the 2003 workbook. The error occurs at the Target.Value line of the following:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 8 And Target.Row = 25 Then
temp = Target.Value
For AB = 66 To 7000
If InStr(1, temp, Cells(AB, 29).Value) Then
Target.Value = Cells(AB, 28).Value
Exit For
End If
Next
End If

Any clues on what to do? I have tried to add DoEvents at the beginning of the statement with no luck.
0
Comment
Question by:castlerj
6 Comments
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39790490
Hi,

pls try

If Target.Address = "$H$25" Then
    temp = Target.Value
    For AB = 66 To 7000
        If InStr(1, temp, Cells(AB, 29).Value) Then
            Application.EnableEvents = False
            Range(Target.Address).Value = Cells(AB, 28).Value
            Application.EnableEvents = True
            Exit For
        End If
    Next
End If

Open in new window

Regards
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39790497
Can you tell what the error description is?
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39790503
For your line:

If InStr(1, temp, Cells(AB, 29).Value) Then

Try

If InStr(1, temp, Cells(AB, 29).Value) > 0 Then

Chris
0
Industry Leaders: 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!

 
LVL 47

Expert Comment

by:Martin Liss
ID: 39790647
Your code works fine for me in Excel 2010 using some simple data that I made up. Can you post the actual workbook, or a sample workbook that illustrates the problem?
0
 
LVL 14

Expert Comment

by:Faustulus
ID: 39793349
Look for the problem in the data types. The Instr function handles Strings. Temp, since it is not dimensioned, is a Variant. There is no guarantee that the value assigned to it will be  a String.. Similarly, Cells(AB, 29).Value is a Variant without assurance of its being of VarType(vbString). So, there is a great chance of comparing apples with oranges.
Start by defining the types of your variables.
Dim Temp As String
Dim AB As Long
Then convert Cells(AB, 29).Value into a String before processing it in the Instr function.
If Instr(1, Temp, CStr(Cells(AB, 29).Value)) Then

This may not cure your problem because between row 66 and row 7000 a lot of different values are assigned to Temp, and only one of these may cause the problem. The difference you will find is that the error message will be more specific. Most important, when an error occurs press the Debug button in the message box and look at the highlighted line of code. Also hover your cursor over the variable AB. This will display its current value, pointing you, in fact, at the cell that is at the source of the error.
0
 

Author Comment

by:castlerj
ID: 39804117
I have been traveling and unable to try your solutions...Thank Uou all...I will try them today...
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel vba question 7 44
Macro that Locks & Unlocks Worksheet 4 30
Set a Range to a Cell in Excel VBA 2 14
How to always round a decimal up 5 22
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,…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

733 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