Solved

Finding rows that contain telephone format + specific word in all caps and pasting it into another worksheet

Posted on 2014-09-16
17
104 Views
Last Modified: 2014-09-16
I've unsuccessfully tried my hand at modifying a macro (see code below) so it performs as follows:

Finds all rows in Sheet 1 that contain the following 2 criteria in the row, and copy those rows into Sheet 5:
The word "CENTRE" in all caps
A 10-digit telephone formatted as follows with a closing bracket only "???-???-????)"


Open in new window

Option Explicit
Sub Copy_CENTRE()
    Dim rng As Range
    Dim cl As Object
    Dim x As Integer
    Dim r As Long
    Dim arrCENTRE() As Variant
   
    arrCENTRE() = Array("???-???-????", "CENTRE")

    r = 2
    Sheets("Sheet1").Activate
    Set rng = Range("A2:A" & Cells.SpecialCells(xlLastCell).Row)
    For Each cl In rng
        For x = 0 To UBound(arrCENTRE())
            If InStr(1, cl.Value, arrCENTRE(x), vbTextCompare) > 0 Then
                Sheets("Sheet5").Cells(r, 1).Value = cl.Value
                r = r + 1
            End If
        Next x
    Next cl
End Sub

Open in new window


Thanks in advance for your help,
Andrea
0
Comment
Question by:Andreamary
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 5
17 Comments
 
LVL 30

Expert Comment

by:gowflow
ID: 40325852
If I understand well CENTRE and the phone number could or can be in different cells on the same row right ? or it is either one of them ?
gowflow
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40325875
Try this

Sub Copy_CENTRE()
    Dim rng As Range
    Dim cl As Object
    Dim x As Integer
    Dim r As Long
    Dim arrCENTRE() As Variant
    
    arrCENTRE() = Array("*???-???-????)*", "CENTRE")

    r = 2
    Sheets("Sheet1").Activate
    Set rng = Range("A2:A" & Cells.SpecialCells(xlLastCell).Row)
    For Each cl In rng
        For x = 0 To UBound(arrCENTRE())
            If cl.Value Like arrCENTRE(x) Then
                Sheets("Sheet5").Cells(r, 1).Value = cl.Value
                r = r + 1
            End If
        Next x
    Next cl
End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:gowflow
ID: 40325877
ok here it is:

replace this line
If InStr(1, cl.Value, arrCENTRE(x), vbTextCompare) > 0 Then

by this line
If LCase(cl.Value) Like LCase(arrCENTRE(x)) Then


here is the amended code

Sub Copy_CENTRE()
     Dim rng As Range
     Dim cl As Object
     Dim x As Integer
     Dim r As Long
     Dim arrCENTRE() As Variant
     
     arrCENTRE() = Array("???-???-????", "CENTRE")

     r = 2
     Sheets("Sheet1").Activate
     Set rng = Range("A2:A" & Cells.SpecialCells(xlLastCell).Row)
     For Each cl In rng
         For x = 0 To UBound(arrCENTRE())
             If LCase(cl.Value) Like LCase(arrCENTRE(x)) Then
             'If InStr(1, cl.Value, arrCENTRE(x), vbTextCompare) > 0 Then
                 Sheets("Sheet5").Cells(r, 1).Value = cl.Value
                 r = r + 1
             End If
         Next x
     Next cl
 End Sub

Open in new window


gowflow
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Martin Liss
ID: 40325893
In my code please change line 8 to

    arrCENTRE() = Array("*???-???-????)*", "*CENTRE*")
0
 
LVL 30

Expert Comment

by:gowflow
ID: 40325894
Martin, didn't he mention
The word "CENTRE" in all caps
?
need to lcase or else you wont trap them with the like alone.
gowflow
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40325909
Actually if you change my line 8 to

    arrCENTRE() = Array("*???-???-????)*", "*[CENTRE]*")

you ignore "centre" or anything that's not all upper case.
0
 

Author Comment

by:Andreamary
ID: 40325911
Thanks for the quick responses.

I tried your solution, Martin, and while every row pasted into Sheet5 met the second criteria ("???-???-????"), most did not meet the first criteria ("CENTRE").

And Gowflow, I tried your solution and did not get any results in Sheet5.

And just to confirm, Gowflow, that all the rows of data in Sheet1 is in one cell per row — all in column A1 x 54,000 rows— and not spanning several cells/columns.

Hope that helps...

Thanks,
Andrea
0
 

Author Comment

by:Andreamary
ID: 40325917
I tried changing line 8 as suggested, and my results on Sheet5 do not meet either criteria...

Andrea
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40325919
I tried your solution, Martin, and while every row pasted into Sheet5 met the second criteria ("???-???-????"), most did not meet the first criteria ("CENTRE").
Please see post ID: 40325909
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40325922
And if that doesn't work then please show a line that is a problem.
0
 
LVL 30

Expert Comment

by:gowflow
ID: 40326030
don't understand how it didn't work here it is in the attach workbook try it

gowflow
copy.xlsm
0
 

Author Comment

by:Andreamary
ID: 40326039
Hi MartinLiss,

I've attached a sample spreadsheet with your macro inserted:

Sheet1 - sample data
Sheet5 - results of running the current macro
Sheet2 - the results I am looking for

Hope this helps...

Thanks,
Andrea
Centre-Tel-Macro.xlsm
0
 
LVL 30

Expert Comment

by:gowflow
ID: 40326044
Did you try mine ? the one I attached is the same that I posted earlier !
gowflow
0
 

Author Comment

by:Andreamary
ID: 40326058
Hi Gowflow,

I tried your copy.xlsm but it wasn't doing what I'm looking for. Hopefully my sample Excel file will help illustrate what I'm after...

Thanks,
Andrea
0
 
LVL 30

Accepted Solution

by:
gowflow earned 500 total points
ID: 40326117
ok her eit is

this is the new macro

Sub Copy_CENTRE()
    Dim rng As Range
    Dim cl As Object
    Dim x As Integer
    Dim r As Long
    Dim arrCENTRE() As Variant
    Dim bPhone As Boolean, bCentre As Boolean
    
    arrCENTRE() = Array("???-???-????", "CENTRE")

    r = 2
    Sheets("Sheet1").Activate
    Set rng = Range("A2:A" & Cells.SpecialCells(xlLastCell).Row)
    For Each cl In rng
        For x = 0 To UBound(arrCENTRE())
            Select Case x
                Case 0  'phone
                    If cl.Value Like "*" & arrCENTRE(x) & "*" Then
                        'Sheets("Sheet3").Cells(r, 1).Value = cl.Value
                        'r = r + 1
                        bPhone = True
                    End If
                    
                Case 1  'centre
                    If InStr(1, cl.Value, arrCENTRE(x)) <> 0 Then
                        'Sheets("Sheet3").Cells(r, 1).Value = cl.Value
                        'r = r + 1
                        bCentre = True
                    End If
            End Select
        Next x
        
        If bPhone And bCentre Then
            Sheets("Sheet3").Cells(r, 1).Value = cl.Value
            r = r + 1
        End If
        
        bPhone = False
        bCentre = False
        
    Next cl
End Sub

Open in new window


it will give results in sheet3.

For next time pls give a sample from the start to avoid waste of time.
Tks
gowflow
Centre-Tel-Macro-V01.xlsm
0
 

Author Closing Comment

by:Andreamary
ID: 40326155
Thanks so much, Gowflow...works like a charm. My apologies for not providing a sample spreadsheet at the outset...

Andrea
0
 
LVL 30

Expert Comment

by:gowflow
ID: 40326213
Glad we were able to pull it thru !
gowflow
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question