Copying parts of a line

barissh

Board Regular
Joined
Aug 10, 2006
Messages
94
Hi guys,

I need a macro which copies a part of info from a line in sheet1 and pastes it to Sheet2.

XXXX1234567 - Routing: [CNQND CNQND ITTRA - TRKUM TRKUM] Carrier: HML


1) I need to insert a control at 44 and 45th digits. If here, 2 digits start with TR then it will copy it with next 3 digits (TRKUM) and paste to Sheet2 B1. If not, it will check same control at 50 and 51th digits. If again, it can not find TR then it will disregard step 2&3 and go to item 4.

2) Copy first 11 digits(XXXX1234567) and paste it to Sheet2 A1

3) It will find the wording "Carrier:" and it will copy next 3 digits (HML) and copy to Sheet2 C1

4) It will go 3 lines down (A1,A4,A7...) and starts to same action again till the end of the list at A column.
 
try
Code:
Sub test()
Dim r As Range, Rex As Object
Set Rex = CreateObject("VBScript.RegExp")
With Sheets("Sheet1")
   For Each r In .Range("a1",.Range("a" & Rows.Count).End(xlUp))
      Rex.Pattern = "(TR)\S+"
      Sheets("Sheett2").Range(r.Address).Resize(,2).Value = _
            Array(Left(r.Value,11),Rex.execute(r.Value)(0))
   Next
End With
Set Rex = Nothing
End Sub
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Code:
      Sheets("Sheett2").Range(r.Address).Resize(,2).Value = _
            Array(Left(r.Value,11),Rex.execute(r.Value)(0))

Thnx but macro has given type mismatch error at quoted lines.
 
Upvote 0
Hummm
How about
Code:
Sub sample()
Dim r As Range, Rex As Object
Set Rex = CreateObject("VBScript.RegExp")
With Sheets("Sheet1")
   For Each r In .Range("a1",.Range("a" & Rows.Count).End(xlUp))
      Rex.Pattern = "(TR)\S+"
      Sheets("Sheett2").Range(r.Address).Value = Left(r.Value,11)
      With Rex
         .Pattern = "(TR)\S+"
         If .test(r.Value) Then
            Sheets("Sheet2").Range("r.Address).Offset(,1).Value = .execute(r.Value)(0)
         End If
      End With
   Next
End With
Set Rex = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,902
Messages
6,168,938
Members
452,227
Latest member
sam1121

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