简体   繁体   中英

VBA to bold numbers within a cell

Weekly I need to perform an activity at work that basically consists on extracting a report, with the activities that will happen on the following week, and format it to the communication template that our company uses. As we need to send one communication per site and there are a few of them, I've automated most of the work using macro however there's one last thing to make this spreadsheet work perfectly - as standard on our internal communications, the timings of the activities need to be in bold and that's where I'm stuck.

I don't know much about programming in VBA but I'm trying a few things, so far I got to bold part of a text within a cell but when it comes to numbers, nothing happens and I don't know exactly why - I'm presuming that's because HH:MM or MM/DD/YYYY are just representative forms of showing a number, for example, 254225 would be 09/02/2010.

the cells I'm trying to change come with the following information

10/04/2018 15:22

and I need them to be

10/04/2018 15:22

It'd drastically reduce the time taken to perform this activity - from 2 hours to around 30 minutes.

Really appreciate all the replies, thank you in advance.

I don't know of any method to format only the time bold if a number/date format like MM/DD/YYYY HH:MM is used. I'm pretty sure that this is only possible with text.

So I recommend to split date and time in 2 different columns:

Therfore duplicate eg the date of column A with the formula =A:A into column B and then format the columns

A                   B
10/04/2018 15:22    =A:A
  1. Number format column A MM/DD/YYYY so it only shows the date part.
  2. Number format column B HH:MM so it only shows the time part.
  3. Format column B as bold.

So the result would be

A                   B
10/04/2018          15:22

in 2 different columns (where column B is bold).

Say we have real date/times in column A like:

10/10/2018 23:30
10/31/2018 06:30
09/26/2018 20:44
11/17/2018 07:11
10/13/2018 16:16
09/20/2018 13:26
10/08/2018 01:06
11/10/2018 14:33
10/16/2018 11:28
11/16/2018 11:53
10/20/2018 08:51
11/23/2018 04:25
10/21/2018 12:41
10/29/2018 14:35
10/14/2018 16:09
11/15/2018 11:56
11/20/2018 10:16
09/21/2018 22:45
10/21/2018 17:55
10/21/2018 19:29

running this simple macro will:

  • convert them to text
  • enbolden the time-part


Sub marine()
    Dim rng As Range, r As Range
    Set rng = Intersect(Columns(1), ActiveSheet.UsedRange)
    For Each r In rng
        r.Value = "'" & r.Text
        r.Characters(12, 5).Font.Bold = True
    Next r
End Sub

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM