Solved

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

Posted on 2014-09-16
17
101 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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 …
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…

778 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