Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you explain this code snipet? 1

Status
Not open for further replies.

snowcold

Programmer
Dec 15, 2004
107
US
I have been researching different code snipets to copy text from one worksheet to another and I have this one snipet ...but I do not quite understand what is going on...

Code:
 Lastcol = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column

and this

Code:
For Each sh In Worksheets
    If sh.UsedRange.Count > 1 Then
       Last = Lastcol(WS)
       With sh.Columns("A:A")
         WS.Columns(Last + 1).Resize(, _
         .Columns.Count).Value = .Value
         WS.Columns(Last +1 ).Columns.AutoFit
       End With
   End If
Next
 
The F1 key with the cursor inside the appropriate keyword (like Find, UsedRange, Resize, ...) didn't help ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Also, the LastCol part is finding the last used column (which contains data) in the specified sheet (in this case "sh"). This is NOT the same thing as Excel's Last Cell [column]. To see what I mean, press F5 | Special | Last Cell. If you've added/deleted data, this may go past your actual last column. The way you've posted via code is probably the most accurate way to detect the last cell (column). To do rows, just change two parts...

Code:
LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row

HTH

-----------
Regards,
Zack Barresse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top