lastrow value is nil

rdev

Active Member
Joined
Dec 3, 2007
Messages
273
Please advise why the variable 'lastrow' is storing a value of 0 when there are data up to row 100 in column A

Sub usingFind()
Dim ms As Worksheet
Dim lastrow As Long, i As Long, c
Dim myCell As Range
Set ms = Sheets("mathew")
lastrow = ms.Range("A" & Rows.Count).End(xlUp).Row
x = 2
y = 3

c = ms.Cells(x, y).Value
u = ms.Cells(x, y).Offset(0, -1).Value
If u = "Purchase of goods" Then u1 = "Sales of goods"
If u = "Sales of goods" Then u1 = "Purchase of goods"
Set myCell = ms.Range("A1:A" & 1000).Find(What:=c, LookIn:=xlValues)

If Not myCell Is Nothing And myCell.Offset(0, 1).Value = u1 And _
myCell.Offset(0, 2).Value = c Then
Cells(x, 5).Value = c
Cells(x, 5).Value = myCell.Offset(0, 3).Value
myCell.Offset(0, 4).Value = Cells(x, 1).Value
myCell.Offset(0, 4).Value = Cells(x, 1).Value

End If
Exit Sub
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello, the above is fixed
I want to test 3 conditions for the code to proceed and return a value . Two AND is not working as per below. What could be the way round
If Not myCell Is Nothing And myCell.Offset(0, 1).Value = u1 And _
myCell.Offset(0, 2).Value = c Then
Cells(x, 5).Value = c
Cells(x, 5).Value = myCell.Offset(0, 3).Value
myCell.Offset(0, 4).Value = Cells(x, 1).Value
myCell.Offset(0, 4).Value = Cells(x, 1).Value
End If
 
Upvote 0
try amending

lastrow = ms.Range("A" & Rows.Count).End(xlUp).Row

to



lastrow
= ms.Cells(Rows.Count, "A").End(xlUp).Row
 
Upvote 0
Two AND is not working as per below.

What do you mean by "not working". Not doing what you want it to do? Or producing an error?

If the .Find doesn't find a value, then myCell will be Nothing, and you will get an error message here when you try using mycell.Offset:

Code:
If Not myCell Is Nothing And myCell.Offset(0, 1).Value = u1 And _
    myCell.Offset(0, 2).Value = c Then

Do it in two steps

Code:
If Not myCell Is Nothing Then
    If myCell.Offset(0, 1).Value = u1 And myCell.Offset(0, 2).Value = c Then
 
Upvote 0
What do you mean by "not working". Not doing what you want it to do? Or producing an error?

If the .Find doesn't find a value, then myCell will be Nothing, and you will get an error message here when you try using mycell.Offset:

Code:
If Not myCell Is Nothing And myCell.Offset(0, 1).Value = u1 And _
    myCell.Offset(0, 2).Value = c Then

Do it in two steps

Code:
If Not myCell Is Nothing Then
    If myCell.Offset(0, 1).Value = u1 And myCell.Offset(0, 2).Value = c Then

Please help me with the code . The file link is shared, the code is not getting through the 3rd condition . The code should go to the last row of data in column A . If a value of c is not found , then return in column E and F .. 'no match'
Code:
Sub usingFind()
Dim ms As Worksheet
Dim lastrow As Long, i As Long
Dim myCell As Range
Set ms = Sheets("mathew")
lastrow = Range("A" & Rows.Count).End(xlUp).Row
x = 2
y = 3
For x = 2 To 5 Step 1
    With ms.Range("A1:A" & lastrow)
        c = ms.Cells(x, y).Value
        u = ms.Cells(x, y).Offset(0, -1).Value
        If u = "Purchase of goods" Then u1 = "Sales of goods"
        If u = "Sales of goods" Then u1 = "Purchase of goods"
        Set myCell = .Find(c, LookIn:=xlValues, lookat:=xlWhole)
        If Not myCell Is Nothing Then
        If myCell.Offset(0, 1).Value = u1 Then
           If myCell.Offset(0, 2).Value = c Then
                Cells(x, 5).Value = myCell.Value
                Cells(x, 6).Value = myCell.Offset(0, 3).Value
                myCell.Offset(0, 4).Value = Cells(x, 1).Value
                myCell.Offset(0, 5).Value = Cells(x, 4).Value
            End If
         End If
        End If
    End With
    u = ""
    u1 = ""
    
Next x

Data2.xlsm - Google Drive
 
Last edited:
Upvote 0
You can probably do this with some simple formulae, rather than VBA.

But at the moment, I'm not clear what you're tyying to do?

For the data illustrated, what results do you want to see, and why?


Excel 2010
ABCDEFG
1CompanyTransactionTransaction PartyAmountMatch CompanyCounter Amount
2AlpaPurchase of goodsSilicon135,000
3TonySales of goodsCinbata12,000
4AlpaSales of goodsNaturo167,000
5AlpaSales of goodsCinbata78,000
6SiliconSales of goodsAlpa270,000
7NaturoPurchase of goodsAlpa167,000
8CinbataPurchase of goodsAlpa80,000
mathew
 
Last edited:
Upvote 0
A different approach:

Code:
Dim lFirstRow As Long, lLastRow As Long

lFirstRow = 2
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
With Range("E" & lFirstRow & ":F" & lLastRow)
    .Columns(1).Formula = "=IF(F" & lFirstRow & "=0,""No result"",C" & lFirstRow & ")"
    .Columns(2).Formula = "=SUMIFS(D$" & lFirstRow & ":D$" & lLastRow & ",A$" & lFirstRow & ":A$" & lLastRow & ",C2,C$" & lFirstRow & ":C$" & lLastRow & ",A2)"
    .Value = .Value
End With


Excel 2010
ABCDEF
1CompanyTransactionTransaction PartyAmountMatch CompanyCounter Amount
2AlpaPurchase of goodsSilicon135,000Silicon270,000
3TonySales of goodsCinbata12,000No result-
4AlpaSales of goodsNaturo167,000Naturo167,000
5AlpaSales of goodsCinbata78,000Cinbata80,000
6SiliconSales of goodsAlpa270,000Alpa135,000
7NaturoPurchase of goodsAlpa167,000Alpa167,000
8CinbataPurchase of goodsAlpa80,000Alpa78,000
mathew


But will there always only one set of transactions for any pair of companies, i.e.

A --> B, and
B --> A

i.e. rather than multiple transactions between A and B?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,184
Messages
6,123,533
Members
449,106
Latest member
techog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top