Solved

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

Posted on 2014-09-16
17
102 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
  • 7
  • 5
  • 5
17 Comments
 
LVL 29

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 46

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 29

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 46

Expert Comment

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

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

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 46

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 46

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 46

Expert Comment

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

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 29

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 29

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 29

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

821 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