Adjustin code: fill color and border

harveya915

Board Regular
Joined
Sep 4, 2015
Messages
141
I have the following code to which I need help adjusting it to fit my needs. I need it for the fill in color to go from C3-G3 but as far down as there is data in column A. The same with the border from C3-G3 but as far down as there is data in column A.

If possible i would like to extend up to C2-G2, however those cells are merged together. Don't know if it would still be possible. IF it is great! if not, not a big deal.

Code:
Private Sub InsertNewWeek_Click()
    With Sheets("Sheet1")
        .Unprotect Password:="2019"
        .Range("C:H").EntireColumn.Insert
        .Range("C3:H3") = Array("End Week Total", "Used", "Restocked", "Price Per Item", "Purchase Total", "")
        .Range("C4").Formula = "=I4-D4+E4"
        .Range("C4", "C" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
        .Range("G4").Formula = "=E4*F4"
        .Range("G4", "G" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
        .Range("C:G").columnWidth = 12
        .Range("C:G").Interior.Color = RGB(221, 235, 247)
        .Range("F:G").NumberFormat = "$#,##0.00"
        .Range("C:G").WrapText = True
        .Range("C2:G2").MergeCells = True
        .Range("C2").Value = Date
        .Range("A1").Interior.ColorIndex = 3
        .Range("C:G").Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Range("C:G").Borders(xlEdgeRight).LineStyle = xlContinuous
        .Range("C:G").Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Range("C:G").Borders(xlEdgeTop).LineStyle = xlContinuous
        .Protect Password:="2019"
    End With
    Unload UserForm1
End Sub

Thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
hello,
just use the row count
Code:
 With Sheets("Sheet1")
        .Unprotect Password:="2019"
        .Range("C:H").EntireColumn.Insert
        .Range("C3:H3") = Array("End Week Total", "Used", "Restocked", "Price Per Item", "Purchase Total", "")
        .Range("C4").Formula = "=I4-D4+E4"
        .Range("C4", "C" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
        .Range("G4").Formula = "=E4*F4"
        .Range("G4", "G" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
        .Range("C:G").ColumnWidth = 12
        .Range("F:G").NumberFormat = "$#,##0.00"
        .Range("C2:G2").MergeCells = True
        .Range("C2").Value = Date
        .Range("A1").Interior.ColorIndex = 3
        With .Range("C3:G" & Cells(Rows.Count, 1).End(xlUp).Row)
            .WrapText = True
            .Interior.Color = RGB(221, 235, 247)
            .Borders(xlEdgeLeft).LineStyle = xlContinuous
            .Borders(xlEdgeRight).LineStyle = xlContinuous
            .Borders(xlEdgeBottom).LineStyle = xlContinuous
            .Borders(xlEdgeTop).LineStyle = xlContinuous
        End With
        .Protect Password:="2019"
    End With
 
Upvote 0
The code portion for the border worked, however the fill in color goes on infinitely down and begins with row 1. I need it to begin with row 3 and finish with the last row in column A with data. Thanks!
 
Upvote 0
Hi ,
tested the code and its OK ,, maybe reset the worksheet to no colour and then rerun the sub
Code:
Sub InsertNewWeek_Click()
    With Sheets("Sheet1")
        .Unprotect Password:="2019"
        .Range("C:H").EntireColumn.Insert
        .Range("C3:H3") = Array("End Week Total", "Used", "Restocked", "Price Per Item", "Purchase Total", "")
        .Range("C4").Formula = "=I4-D4+E4"
        .Range("C4", "C" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
        .Range("G4").Formula = "=E4*F4"
        .Range("G4", "G" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
        .Range("C:G").ColumnWidth = 12
        .Range("F:G").NumberFormat = "$#,##0.00"
        .Range("C2:G2").MergeCells = True
        .Range("C2").Value = Date
        .Range("A1").Interior.ColorIndex = 3
        With .Range("C3:G" & Cells(Rows.Count, 1).End(xlUp).Row)
            .WrapText = True
            .Interior.Color = RGB(221, 235, 247)
            .Borders(xlEdgeLeft).LineStyle = xlContinuous
            .Borders(xlEdgeRight).LineStyle = xlContinuous
            .Borders(xlEdgeBottom).LineStyle = xlContinuous
            .Borders(xlEdgeTop).LineStyle = xlContinuous
        End With
        .Protect Password:="2019"
    End With
    Unload UserForm1
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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