Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

vba code error

0

plz have a look to the samle file i made a macro but i am unable to find where the error occured plz see the sample file and ran the macro

Answer
Discuss

Answers

0
Selected Answer

You are consistently mixing up the Range object with its Value. Here is a striking example: r = Ws1.Cells(f1.Row, "R")
r is a Range. Therefore it needs a Set statement. But you don't mean the range. You mean its default property, its Value. You also don't want to assign the range Ws1.Cells(f1.Row, "R") to the range r but value to value. Excel somehow seems to figure it out but it's bound to confuse you. Here is the correct syntax for the record.

r.Value = Ws1.Cells(f1.Row, "R").Value

Actually, you don't need Range r at all. Its use is quite convoluted. Try this instead (you will need Dim C As Long in the declarations).

C = Ws2.Cells(f2.Row, Ws2.Columns.Count).End(xlToLeft).Column + 1
If C < 3 Then C = 3
Ws2.Cells(f2.Row, C).Value = Ws1.Cells(f1.Row, "R")

Since your variables have no meaningful names your code is difficult to understand, especially for third parties like me or, as you will find eventually, for recursive tourists, if you come back to your code after an interval. I found that the Application properties are not reset to their original values properly at the end of the code.

However, none of the above appears to be the cause of your complaint. I suspect this part of your code: "r1.Find(f2)". Of course, f2 is a range and you mean its value and Excel/VBA forgives you and fixes the problem. But the Find method has many properties of which you give only one. You should be interested in the LookAt property which takes the values xlWhole and xlPart which I believe you will find self-explanatory.

When you read up on Find in MSDN you will learn that some of these properties are defaults and others are remembered from a previous use. To make things more complex, Find is also used at worksheet level. Even if you set all the properties in VBA, after making a manual search in the worksheet Excel will remember the properties you used in that search and apply some of them to your next search in VBA. For this reason I always set all the properties. I have a function for that which I give to you below.

Function FindRow(FndWhat As Variant, _
                 FndIn As Range, _
                 Optional FndAft As Long = 1, _
                 Optional FndVal As Boolean, _
                 Optional FndPart As Boolean, _
                 Optional FndHow As Long = xlByColumns, _
                 Optional FndWay As Long = xlNext, _
                 Optional FndCase As Boolean = False, _
                 Optional Fnd As Range) As Long
    ' SSY 040 10 Jan 2019
    ' returns a sheet row if FndIn is a named range or DataBodyRange

    ' ==================================================
    '   Parameters:
    '       FndWhat         = Text/Number to search for
    '       FndIn           = Range to search in
    '       FndAft          = Cell of FndIn to start the search from
    '       FndVal          = Cell property to search
    '                         [True = Value, False = Formula]
    '       FndPart         = True = xlPart, False = xlWhole]
    '       FndHow          = Search method
    '                         [= xlByRows or xlByColumns]
    '       FndWay          = Search direction
    '                         [= xlNext or XlPrevious]
    '       FndCase         = Match case
    '       Fnd             = Return range
    ' ==================================================


    If FndAft = 0 Then FndAft = FndIn.Cells.Count
    With FndIn
        Set Fnd = .Find(What:=FndWhat, _
                        After:=.Cells(FndAft), _
                        LookIn:=IIf(FndVal, xlValues, xlFormulas), _
                        LookAt:=IIf(FndPart, xlPart, xlWhole), _
                        SearchOrder:=FndHow, _
                        SearchDirection:=FndWay, _
                        MatchCase:=FndCase)
    End With
    On Error Resume Next                      ' return 0, if not found
    FindRow = Fnd.Row
    Err.Clear
End Function

Call the function with code like this.

Dim R As Long
R = FindRow(f2.Value, F1, FindPart:=False)
' You must name the argument if it isn't given in the sequence
' the function expects.

All the Find properties you don't pass in the function call will be set in the function (Actually, FindPart=False by default). None of the preset or remebered properties will survive. The function returns either the row number where a match was found or 0, if there was no match. If you pass a range object as an argument, "Fnd:=r", r will contain the Fnd range (mind that R and r are identical to VBA) if FindRow was successful but you don't need that capability here.

If you want to deploy this function you would need to delete your declaration of r As Range, declare R As Long, C As Long, and then replace the block of code in your For ... Next loop with the code below.

        R = FindRow(f2.Value, r1)
        If R Then
            C = Ws2.Cells(f2.Row, Ws2.Columns.Count).End(xlToLeft).Column + 1
            If C < 3 Then C = 3
            Ws2.Cells(f2.Row, C).Value = Ws1.Cells(R, "R")
        End If
Discuss

Discussion

Thnx alot mam problem solved
rider1234 (rep: 10) Mar 2, '20 at 6:10 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login