Apply formatting to user selected range

robdav

New Member
Joined
Dec 1, 2017
Messages
15
Hi

I'm trying to apply the following formatting to a user selected (via Application.InputBox) range of cells.

It keeps bugging out on the "Cell.Value = Replace(Expression:=Cell.Value, Find:=".", Replace:="/")" line

I'd be grateful if someone could help please.

Thanks

Rob

Code:
Sub FormatCells()


Dim rng As Range
Dim DefaultRange As Range
Dim FormatRuleInput As String


'Determine a default range based on user's Selection
  If TypeName(Selection) = "Range" Then
    Set DefaultRange = Selection
  Else
    Set DefaultRange = ActiveCell
  End If


'Get A Cell Address From The User to Get Number Format From
  On Error Resume Next
    Set rng = Application.InputBox( _
      Title:="Highlight Cells Yellow", _
      Prompt:="Select a cell range to highlight yellow", _
      Default:=DefaultRange.Address, _
      Type:=8)
  On Error GoTo 0


'Test to ensure User Did not cancel
  If rng Is Nothing Then Exit Sub
  
'Highlight Cell Range
        For Each Cell In rng
        Cell.Offset(0, 1).Value = Right(Cell, 8)
        Cell = Mid(Cell, 1, Len(Cell) - 9)
        Cell.Value = Replace(Expression:=Cell.Value, Find:=".", Replace:="/")
        Next


End Sub
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,670
Office Version
  1. 365
Platform
  1. Windows
What is the cell value when you get the error?
At a guess it doesn't have a "." in it.
 

robdav

New Member
Joined
Dec 1, 2017
Messages
15
What is the cell value when you get the error?
At a guess it doesn't have a "." in it.

Sample cell content:
2019.04.25 14:07:27

<tbody>
</tbody>

It works if I use it on a manual range selection.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,670
Office Version
  1. 365
Platform
  1. Windows
Try
Code:
        For Each cell In rng
        cell.Offset(0, 1).Value = Right(cell, 8)
        cell.Value = Mid(cell.Value, 1, Len(cell.Value) - 9)
        cell.Value = Replace(cell.Value, ".", "/")
        Next
 

robdav

New Member
Joined
Dec 1, 2017
Messages
15
Try
Code:
        For Each cell In rng
        cell.Offset(0, 1).Value = Right(cell, 8)
        cell.Value = Mid(cell.Value, 1, Len(cell.Value) - 9)
        cell.Value = Replace(cell.Value, ".", "/")
        Next

Perfect, many thanks Fluff.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,670
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,127,832
Messages
5,627,150
Members
416,223
Latest member
RichardHell

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
Top