• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How this VBA code is working?

DJ

Member
Hi,


I was checking code of a macro but unable to understand the following code:


LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row


Please assist.


Thanks
 
Breaking it down:

Cells.Find...

First, we see that the method is being applied to the Cells Object. As there is no stated hierarchy, the default is that it applies to the activesheet.


Next, the Find Method will return a Range. The first arguement lets us know that we're looking for any text (via a wildcard). We're searing by rows and we're searching in Previous direction, aka, top to bottom. Putting all those arguements together, you would say "Find the first cell with text, starting at the bottom row and working your way up".


Finally, once we have that Range object, we're returning the row value.


My guess is the line is meant to find the last row of the "used range".
 
Hi DJ,


I am not too sure if I can explain you this fully.


The find function returns Range and has following structure:


Function Find(What, [After], [LookIn], [LookAt], [SearchOrder], [SearchDirection As XlSearchDirection = xlNext], [MatchCase], [MatchByte], [SearchFormat]) As Range


In the given code, we have

What = "*" wild card, means searches for any data/text

searchOrder:=xlByRows, means search row wise, you can also give xlbycolumn which would search by column wise.

SearchDirection as xlPrevious, would stop the code at the last instance of search criteria. the other option is xlNext which stop at the first instance of search criteria.


Since they want to find the last row of the sheet they have given xlprevious and to find any text/data by using "*", and find function would return Range, that range's row number is stored in LastRow variable.


The parameters in [] given are optional.


I hope this gives some idea for interpreting the code.


Regards,

Prasad
 
Back
Top