Need row colour to be purple if a value is in a cell for each row

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,362
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a spreadsheet that copies rows from a table to separate documents depending on several factors. This is the code from my copy button.
Code:
Sub cmdCopy()
        Dim wsDst As Worksheet, wsSrc As Worksheet, tblrow As ListRow
        Dim Combo As String, sht As Worksheet, tbl As ListObject
        Dim LastRow As Long, DocYearName As String, lr As Long
        Dim w As Window
            Application.ScreenUpdating = False
        'assign values to variables
        Set tbl = ThisWorkbook.Worksheets("Costing_tool").ListObjects("tblCosting")
        Set sht = ThisWorkbook.Worksheets("Costing_tool")
        For Each tblrow In tbl.ListRows
            If tblrow.Range.Cells(1, 1).Value = "" Or tblrow.Range.Cells(1, 5).Value = "" Or tblrow.Range.Cells(1, 6).Value = "" Then
                MsgBox "The Date, Service or Requesting Organisation has not been entered for every record in the table"
                Exit Sub
            End If
        Next tblrow
        For Each tblrow In tbl.ListRows
            Combo = tblrow.Range.Cells(1, 26).Value
            
                Select Case tblrow.Range.Cells(1, 6).Value
                    Case "Ang Wes", "Ang Riv", "Yir"
                        DocYearName = tblrow.Range.Cells(1, 37).Value
                    Case Else
                        DocYearName = tblrow.Range.Cells(1, 36).Value
                End Select

            
            
            If Not isFileOpen(DocYearName & ".xlsm") Then Workbooks.Open ThisWorkbook.Path & "" & DocYearName & ".xlsm"

            Set wsDst = Workbooks(DocYearName).Worksheets(Combo)
             lr = wsDst.Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
             With wsDst
                    'This copies the first 16 columns, i.e. A:J, of the current row of the table to column A in the destination sheet.
                    tblrow.Range.Resize(, 16).Copy
                    'This pastes in the figures in the first 10 columns starting in column A
                    .Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormulasAndNumberFormats
                    'Overwrites the numbers pasted to column I with a formula
                    .Range("I" & .Range("I" & .Rows.Count).End(xlUp).Row).Formula = "=IF(R[0]C[-4]=""Activities"",0,RC[-1]*0.1)"
                    'Overwrites the numbers pasted to column J with a formula
                    .Range("J" & .Range("J" & .Rows.Count).End(xlUp).Row).Formula = "=IF(R[1]C[-5]=""Activities"",RC[-2],RC[-1]+RC[-2])"
                    'sort procedure copied from vba
                    wsDst.Sort.SortFields.Clear
                    wsDst.Sort.SortFields.Add Key:=Range("A4:A" & lr), _
                        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                            With Workbooks(DocYearName).Worksheets(Combo).Sort
                                .SetRange Range("A3:AK" & lr)
                                .header = xlYes
                                .MatchCase = False
                                .Orientation = xlTopToBottom
                                .SortMethod = xlPinYin
                                .Apply
                            End With

                End With
        Next tblrow
        sht.Protect
        
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
End Sub

If cell 6 has Yir in it, the row will be copied to the document name stored in cell 37. I need the new whole row (in the filename stored in cell 37) to be -65383 colour. Can someone help me with the code please?

Thanks,
Dave
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Maybe something like this (not tested)

Code:
[COLOR=darkblue]Sub[/COLOR] cmdCopy()
        [COLOR=darkblue]Dim[/COLOR] wsDst [COLOR=darkblue]As[/COLOR] Worksheet, wsSrc [COLOR=darkblue]As[/COLOR] Worksheet, tblrow [COLOR=darkblue]As[/COLOR] ListRow
        [COLOR=darkblue]Dim[/COLOR] Combo [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], sht [COLOR=darkblue]As[/COLOR] Worksheet, tbl [COLOR=darkblue]As[/COLOR] ListObject
        [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], DocYearName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], lr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[B]        [COLOR=darkblue]Dim[/COLOR] RowColor [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR][/B]
        [COLOR=darkblue]Dim[/COLOR] w [COLOR=darkblue]As[/COLOR] Window
            Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
        [COLOR=green]'assign values to variables[/COLOR]
        [COLOR=darkblue]Set[/COLOR] tbl = ThisWorkbook.Worksheets("Costing_tool").ListObjects("tblCosting")
        [COLOR=darkblue]Set[/COLOR] sht = ThisWorkbook.Worksheets("Costing_tool")
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] tblrow [COLOR=darkblue]In[/COLOR] tbl.ListRows
            [COLOR=darkblue]If[/COLOR] tblrow.Range.Cells(1, 1).Value = "" [COLOR=darkblue]Or[/COLOR] tblrow.Range.Cells(1, 5).Value = "" [COLOR=darkblue]Or[/COLOR] tblrow.Range.Cells(1, 6).Value = "" [COLOR=darkblue]Then[/COLOR]
                MsgBox "The Date, Service or Requesting Organisation has not been entered for every record in the table"
                [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]Next[/COLOR] tblrow
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] tblrow [COLOR=darkblue]In[/COLOR] tbl.ListRows
            Combo = tblrow.Range.Cells(1, 26).Value
                [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] tblrow.Range.Cells(1, 6).Value
                    [COLOR=darkblue]Case[/COLOR] "Yir"
[B]                        DocYearName = tblrow.Range.Cells(1, 37).Value[/B]
[B]                        RowColor = -65383[/B]
[B]                    [COLOR=darkblue]Case[/COLOR] "Ang Wes", "Ang Riv"[/B]
[B]                        DocYearName = tblrow.Range.Cells(1, 37).Value[/B]
[B]                        RowColor = 0[/B]
[B]                    [COLOR=darkblue]Case[/COLOR] [COLOR=darkblue]Else[/COLOR][/B]
[B]                        DocYearName = tblrow.Range.Cells(1, 36).Value[/B]
[B]                        RowColor = 0[/B]
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]


            
            
            [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] isFileOpen(DocYearName & ".xlsm") [COLOR=darkblue]Then[/COLOR] Workbooks.Open ThisWorkbook.Path & "" & DocYearName & ".xlsm"


            [COLOR=darkblue]Set[/COLOR] wsDst = Workbooks(DocYearName).Worksheets(Combo)
             lr = wsDst.Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
             [COLOR=darkblue]With[/COLOR] wsDst
                    [COLOR=green]'This copies the first 16 columns, i.e. A:J, of the current row of the table to column A in the destination sheet.[/COLOR]
                    tblrow.Range.Resize(, 16).Copy
                    [COLOR=green]'This pastes in the figures in the first 10 columns starting in column A[/COLOR]
                    .Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormulasAndNumberFormats
[B]                    [COLOR=darkblue]If[/COLOR] RowColor <> 0 [COLOR=darkblue]Then[/COLOR] .Range("A" & Rows.Count).End(xlUp).Resize(, 10).Interior.Color = RowColor[/B]
                    [COLOR=green]'Overwrites the numbers pasted to column I with a formula[/COLOR]
                    .Range("I" & .Range("I" & .Rows.Count).End(xlUp).Row).Formula = "=IF(R[0]C[-4]=""Activities"",0,RC[-1]*0.1)"
                    [COLOR=green]'Overwrites the numbers pasted to column J with a formula[/COLOR]
                    .Range("J" & .Range("J" & .Rows.Count).End(xlUp).Row).Formula = "=IF(R[1]C[-5]=""Activities"",RC[-2],RC[-1]+RC[-2])"
                    [COLOR=green]'sort procedure copied from vba[/COLOR]
                    wsDst.Sort.SortFields.Clear
                    wsDst.Sort.SortFields.Add Key:=Range("A4:A" & lr), _
                        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                            [COLOR=darkblue]With[/COLOR] Workbooks(DocYearName).Worksheets(Combo).Sort
                                .SetRange Range("A3:AK" & lr)
                                .header = xlYes
                                .MatchCase = [COLOR=darkblue]False[/COLOR]
                                .Orientation = xlTopToBottom
                                .SortMethod = xlPinYin
                                .Apply
                            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                            
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
        [COLOR=darkblue]Next[/COLOR] tblrow
        sht.Protect
        
        Application.CutCopyMode = [COLOR=darkblue]False[/COLOR]
        Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Last edited:
Upvote 0
Maybe something like this (not tested)

Code:
                [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] tblrow.Range.Cells(1, 6).Value
                    [COLOR=darkblue]Case[/COLOR] "Yir"
[B]                        DocYearName = tblrow.Range.Cells(1, 37).Value[/B]
[B]                        RowColor = -65383[/B]
[B]                    [COLOR=darkblue]Case[/COLOR] "Ang Wes", "Ang Riv"[/B]
[B]                        DocYearName = tblrow.Range.Cells(1, 37).Value[/B]
[B]                        RowColor = 0[/B]
[B]                    [COLOR=darkblue]Case[/COLOR] [COLOR=darkblue]Else[/COLOR][/B]
[B]                        DocYearName = tblrow.Range.Cells(1, 36).Value[/B]
[B]                        RowColor = 0[/B]
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]

With this code, it highlights rowcolor and says variable not defined.
 
Upvote 0
Did you declare the variable at the top of the code like this?

Code:
[B][COLOR=darkblue]Dim[/COLOR] RowColor [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR][/B]
 
Upvote 0
I did that but it still won't change the colour.
 
Upvote 0
Don't have excel at the moment, but shouldn't it be....

Code:
Select Case tblrow.Range.Cells(1, 6).Value
                    Case "Yir"
                        DocYearName = tblrow.Range.Cells(1, 37).Value
                        tblrow.RowColor = -65383
                    Case "Ang Wes", "Ang Riv"
                        DocYearName = tblrow.Range.Cells(1, 37).Value
                        tblrow.RowColor= 0
                    Case Else
                        DocYearName = tblrow.Range.Cells(1, 36).Value
                        tblrow.RowColor = 0
                End Select
 
Upvote 0
With that I get an error "object doesn't support this property or method". It also highlights the line "tblrow.RowColor = -65383"
 
Last edited:
Upvote 0
Dave
Which version of the quoting workbook is this in ??
 
Last edited:
Upvote 0
Just found out my manager doesn't want this. Thanks anyway Michael.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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