Solved

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

Posted on 2014-09-16
17
99 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

895 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

14 Experts available now in Live!

Get 1:1 Help Now