PC Review


Reply
Thread Tools Rate Thread

Check last two characters of a string.

 
 
mg_sv_r
Guest
Posts: n/a
 
      29th Jan 2008
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


 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      29th Jan 2008
You nee to use right

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


"mg_sv_r" wrote:

> 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
>
>

 
Reply With Quote
 
mg_sv_r
Guest
Posts: n/a
 
      29th Jan 2008
Thank you Joel!

A lot simpler than I thought!

Very much appreciated.

John

"Joel" wrote:

> You nee to use right
>
> If right(ActiveCell.Offset(Roffset, 0),2) = "78" Then
>
>
> "mg_sv_r" wrote:
>
> > 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
> >
> >

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      29th Jan 2008
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


"mg_sv_r" <(E-Mail Removed)> wrote in message
news:6907322B-5851-4F5D-8235-(E-Mail Removed)...
> Thank you Joel!
>
> A lot simpler than I thought!
>
> Very much appreciated.
>
> John
>
> "Joel" wrote:
>
>> You nee to use right
>>
>> If right(ActiveCell.Offset(Roffset, 0),2) = "78" Then
>>
>>
>> "mg_sv_r" wrote:
>>
>> > 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
>> >
>> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Leading Characters If String Is Only 7 Characters Paperback Writer Microsoft Excel Misc 1 21st Apr 2009 08:47 PM
Check the first 2 characters of string axapta Microsoft VB .NET 5 30th Jul 2008 10:22 PM
Check string for special characters =?Utf-8?B?UmljYXJkbyBKZXN1cw==?= Microsoft Dot NET 4 22nd Feb 2005 10:01 AM
JScript: check if a string contains unicode characters Lala Microsoft ASP .NET 1 17th Nov 2003 05:30 PM
Adding characters to a string : Error 'string.this[int]' cannot be assigned to -- it is read only J. Oliver Microsoft C# .NET 4 29th Sep 2003 07:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:34 AM.