Solved

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

Posted on 2014-09-16
17
98 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 45

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

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 45

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

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 45

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now