Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2008

Tip: Looking for answers? Try searching our database.

Check last two characters of a string.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mg_sv_r - 29 Jan 2008 14:17 GMT
Hi I have a macro where I am trying to copy the entire row of data to a new
sheet if the last two characters of a cell meets one of two conditions.

What I have so far is...

------------------------

Sub MoveRowsToBLM10()

Dim srcRange As Range
Dim dstRange As Range
Const srcSheetName = "INTERLINE_COUPON LISTING_JA (2)"
Const dstSheetName = "sheet100"
Dim Roffset As Long
Dim dstRow As Long

Worksheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "sheet100"

Worksheets(srcSheetName).Select
Range("B1").Select

Do Until ActiveCell.Row = 65535

 If ActiveCell.Offset(Roffset, 0) = "78" Then
    Set srcRange = Worksheets(srcSheetName). _
     Rows(Roffset + 1 & ":" & Roffset + 1)
   'find available row on dest sheet
     dstRow = Worksheets(dstSheetName).Range("N" _
      & Rows.Count).End(xlUp).Row + 1
     Set dstRange = Worksheets(dstSheetName). _
      Rows(dstRow & ":" & dstRow)
     dstRange.Value = srcRange.Value
 End If
 If ActiveCell.Offset(Roffset, 0) = "88" Then
    Set srcRange = Worksheets(srcSheetName). _
     Rows(Roffset + 1 & ":" & Roffset + 1)
   'find available row on dest sheet
     dstRow = Worksheets(dstSheetName).Range("N" _
      & Rows.Count).End(xlUp).Row + 1
     Set dstRange = Worksheets(dstSheetName). _
      Rows(dstRow & ":" & dstRow)
     dstRange.Value = srcRange.Value
 End If
 Roffset = Roffset + 1
Loop

Set srcRange = Nothing
Set dstRange = Nothing
End Sub

----------------------

But I need to change this somehow so instead reading

If ActiveCell.Offset(Roffset, 0) = "78"

And

If ActiveCell.Offset(Roffset, 0) = "88"

I need it to look to see if just the last two characters of the cell are
"78" or "88"

Can anyone point give me some tips on doing this please.

Regards
John
Joel - 29 Jan 2008 14:46 GMT
You nee to use right

If right(ActiveCell.Offset(Roffset, 0),2) = "78" Then

> Hi I have a macro where I am trying to copy the entire row of data to a new
> sheet if the last two characters of a cell meets one of two conditions.
[quoted text clipped - 64 lines]
> Regards
> John
mg_sv_r - 29 Jan 2008 15:05 GMT
Thank you Joel!

A lot simpler than I thought!

Very much appreciated.

John

> You nee to use right
>
[quoted text clipped - 68 lines]
> > Regards
> > John
Rick Rothstein (MVP - VB) - 29 Jan 2008 16:34 GMT
Unless I am misreading your code, it looks like you are executing the
**identical** code when the last two numbers are either 78 or 88. If I have
read your code correctly, you do not need two separate If-Then blocks (one
for each matching last two values), you can use a single If-Then block with
either of these If-Then statements as the testing condition...

If your right two numbers are as shown (78 and 88)
===================================================
If Right(ActiveCell.Offset(Roffset, 0),2) Like "[78]8" Then

If your number were representative (not necessarily sharing digits)
===================================================
If Right(ActiveCell.Offset(Roffset, 0),2) = "78" Or _
     Right(ActiveCell.Offset(Roffset, 0),2) = "88" Then

Rick

> Thank you Joel!
>
[quoted text clipped - 78 lines]
>> > Regards
>> > John
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.