Modify VBA to select Cells with Red Font Copy only cells not row

baz9d93

New Member
Joined
Jun 16, 2014
Messages
29
Office Version
  1. 2019
Platform
  1. Windows
Thank you for taking time to read this

I am wotking with a tutorial first time using VBA

At the moment it works when All the text in the row has red font - I need it so that

1. It only selects the cells with red font and copy those only NOT the entire row and also put the cell in same position on the new sheet it copies to.
2. Also copy into new sheet the code value in the row Col A see mockup image

vba-mockup.png


VBA Code:
Sub CopyColouredFontTransactions()

Dim TransIDField As Range
Dim TransIDCell As Range
Dim ATransWS As Worksheet
Dim HTransWS As Worksheet
Dim x As Long

Set ATransWS = Worksheets("All Transactions")
Set TransIDField = ATransWS.Range("A2", ATransWS.Range("A2").End(xlDown))
Set HTransWS = Worksheets("Highlighted Transactions")


For Each TransIDCell In TransIDField

    If TransIDCell.Font.Color = RGB(255, 0, 0) Then
       
        TransIDCell.Resize(1, 10).Copy Destination:= _
            HTransWS.Range("A1").Offset(HTransWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)
           
    End If

Next TransIDCell

HTransWS.Columns.AutoFit

End Sub
 

Attachments

  • vba-mockup.png
    vba-mockup.png
    70.2 KB · Views: 9

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Perhaps this will work...
VBA Code:
Sub baz9d93()
Dim ce As Range, lastrow As Long, lastcol As Long, i As Long
Dim wsA As Worksheet, wsH As Worksheet

Set wsA = Sheets("All Transactions")
Set wsH = Sheets("Highlighted Transactions")

lastrow = wsA.Range("A" & Rows.Count).End(xlUp).Row
lastcol = wsA.Cells.SpecialCells(xlCellTypeLastCell).Column
Application.ScreenUpdating = False

For Each ce In wsA.Range(wsA.Cells(2, 2), wsA.Cells(lastrow, lastcol))
    If ce.Font.Color = RGB(255, 0, 0) Then
        ce.Copy wsH.Range(ce.Address)
        wsH.Range("A" & ce.Row).Value = wsA.Range("A" & ce.Row).Value
    End If
Next ce

lastrow = wsH.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow
    If wsH.Cells(i, 1).Value = "" Then wsH.Cells(i, 1).EntireRow.Delete
Next i

wsH.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Perhaps this will work...
VBA Code:
Sub baz9d93()
Dim ce As Range, lastrow As Long, lastcol As Long, i As Long
Dim wsA As Worksheet, wsH As Worksheet

Set wsA = Sheets("All Transactions")
Set wsH = Sheets("Highlighted Transactions")

lastrow = wsA.Range("A" & Rows.Count).End(xlUp).Row
lastcol = wsA.Cells.SpecialCells(xlCellTypeLastCell).Column
Application.ScreenUpdating = False

For Each ce In wsA.Range(wsA.Cells(2, 2), wsA.Cells(lastrow, lastcol))
    If ce.Font.Color = RGB(255, 0, 0) Then
        ce.Copy wsH.Range(ce.Address)
        wsH.Range("A" & ce.Row).Value = wsA.Range("A" & ce.Row).Value
    End If
Next ce

lastrow = wsH.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow
    If wsH.Cells(i, 1).Value = "" Then wsH.Cells(i, 1).EntireRow.Delete
Next i

wsH.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
Thank you for your quick reply this is working correctly thanks but 1 thing I notice if Col A does not have have Code but other cells in that row do have text with red font they dont get copied over. Can this be done?

Also - as I mentioned I am new to VBA and this was from a basic tutorial I was learning from - but in the actual worksheet that I have it has 35000 rows and 60 Columns with different values - the way I am doing it now is using Find Replace entering the Word I need to find - in each cell - then CTRL A - then changing the font color to Red then using this Code you have kindly provided. So I need to run this process 60 times

Is it possible for me to get this done faster by putting in Sheet 1 in another Column for example Col E all the values I need it to look for and then Put them each in a new Sheet?

Or can anyone kindly suggest a more efficient way of doing this task. thank you
 
Upvote 0
My code deletes all rows on the results sheet where column A is blank (I didn't realize you'd have a row of data without a code in the first column), so just remove this portion of the code and it should keep all rows where red font is encountered:
VBA Code:
For i = 2 To lastrow
    If wsH.Cells(i, 1).Value = "" Then wsH.Cells(i, 1).EntireRow.Delete
Next i
 
Upvote 0
Also, I'd recommend putting your additional functionality requests in a new post.
 
Upvote 0
Also, I'd recommend putting your additional functionality requests in a new post.
Thank you very much for your help with this really appreciate it - i will create a new post as you kindly suggested. Much Appreciated
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,003
Members
448,935
Latest member
ijat

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