VBA Select all cells in column that contain any of these words

yits05

Board Regular
Joined
Jul 17, 2020
Messages
56
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have been struggling to figure this out. I have column AA which contains a list of cells. Some of those cells contain specific words, and I would like to select & copy those cells. Here is what I have so far, that is not working:

VBA Code:
Dim ranCell As Range
        Dim rRng4 As Range
        Dim lastceAA As Range
        lastceAA = ActiveSheet.Range("AA" & Rows.Count).End(xlUp).Row
            For Each ranCell In Range("AA1" & lastceAA)
                If ranCell.Value = "*January*" Or_
                ranCell.Value = "*February*" Or _
                ranCell.Value = "*March*" Or _
                ranCell.Value = "*April*" Or _
                    ranCell.Value = "*May*" Or _
                    ranCell.Value = "*June*" Or _
                    ranCell.Value = "*July*" Or _
                    ranCell.Value = "*August*" Or _
                    ranCell.Value = "*September*" Or _
                    ranCell.Value = "*October*" Or _
                    ranCell.Value = "*November*" Or _
                    ranCell.Value = "*December" Then
                
                    IrRng4.Select
                    
                    Else
                        Set rRng4 = Application.Union(rRng4, ranCell)
                    End If
                End If
            Next ranCell
           
            Selection.Copy

Any help would be appreciated!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi,
see if this update to your code does what you want


Rich (BB code):
Sub CopyCells()
    Dim ranCell     As Range, rRng4 As Range
    Dim lastceAA    As Long
    Dim i           As Integer
    Dim m           As Variant
    Dim ws          As Worksheet
    
    'master sheet - change sheet name as required
    Set ws = Worksheets("Sheet1")
    
    lastceAA = ws.Cells(ws.Rows.Count, "AA").End(xlUp).Row
    
    For Each ranCell In ws.Range("AA1:A" & lastceAA).Cells
        For i = 1 To 12
            m = Application.Match("*" & MonthName(i, False) & "*", Array(ranCell.Value), 0)
            If Not IsError(m) Then
                If rRng4 Is Nothing Then
                    Set rRng4 = ranCell
                Else
                    Set rRng4 = Application.Union(ranCell, rRng4)
                End If
                Exit For
            End If
        Next i
        
    Next ranCell
    
    'copy cells to destination sheet - change as required
    If Not rRng4 Is Nothing Then rRng4.Copy Worksheets("Sheet2").Range("A1")

End Sub

Change sheet names shown in BOLD as required
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top