Hi, I wonder whether someone may be able to help me please.
I've put together the following code which performs a number of actions within given columns in a sheet array.
Sub MonthlyFormatSheets()
Dim ws As Worksheet
Dim rng As Range
Dim LastRow As Long
Const StartRow As Long = 5
For Each ws In Worksheets(Array("Monthly Projects", "Monthly Direct Activities", "Monthly Enhancements", "Monthly Indirect Activities", "Monthly Overheads"))
LastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row
If LastRow >= StartRow Then
If ws.Name = "Monthly Projects" Then
ws.Range("B5:G" & LastRow).Cells.Font.Name = "Lucinda Sans"
ws.Range("B5:G" & LastRow).Cells.Font.Size = 10
ws.Range("E5:G" & LastRow).NumberFormat = "#,##0.000"
ws.Range("E5:G" & LastRow).HorizontalAlignment = xlCenter
With Range("C5:C" & LastRow)
.Replace " ", ""
.Replace "_", ""
End With
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range("B5"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.Sort
.SetRange Range("B5:F" & LastRow)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ws.Range("G5:G" & LastRow).FormulaR1C1 = "=RC5-RC6"
With ws.Range("D5", Range("D" & Rows.Count).End(xlUp))
.Value = Evaluate("if(row(" & .Address & "),replace(" & .Address & ",1,10,""""))")
End With
ws.Columns("B:G").AutoFit
Else
ws.Range("B5:F" & LastRow).Cells.Font.Name = "Lucinda Sans"
ws.Range("B5:F" & LastRow).Cells.Font.Size = 10
ws.Range("D5:F" & LastRow).NumberFormat = "#,##0.000"
ws.Range("D5:F" & LastRow).HorizontalAlignment = xlCenter
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range("B5"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.Sort
.SetRange Range("B5:E" & LastRow)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ws.Range("F5:F" & LastRow).FormulaR1C1 = "=RC4-RC5"
With ws.Range("C5", Range("C" & Rows.Count).End(xlUp))
.Value = Evaluate("if(row(" & .Address & "),replace(" & .Address & ",1,10,""""))")
End With
ws.Columns("B:F").AutoFit
End If
End If
Next ws
End Sub
Display More
Unfortunately though I'm having a problem with the following sections of code which removes the first 10 characters from a text string:
With ws.Range("D5", Range("D" & Rows.Count).End(xlUp))
.Value = Evaluate("if(row(" & .Address & "),replace(" & .Address & ",1,10,""""))")
End With
and
With ws.Range("C5", Range("C" & Rows.Count).End(xlUp))
.Value = Evaluate("if(row(" & .Address & "),replace(" & .Address & ",1,10,""""))")
End With
When I run this, I receive Run time error '1004' Method Range of Object_worksheet failed' with debug highlighting this line as the cause
With ws.Range("D5", Range("D" & Rows.Count).End(xlUp)
) and I've no idea why despite spending several hours on this with different permutations of code.
In addition, I would like to add an 'IF' statement which states that the first character must be / before it removes the 10 characters.
I just wondered whether someone may be able to look at this and let me know where I'm going wrong.
Many thanks and the kindest regards