VBA: Apply border if there is something in the cell?

CC268

Active Member
Joined
Mar 7, 2016
Messages
328
Hey guys,

I have a long spreadsheet with tons of data in all sorts of cells. I just simply want to add a border to anything that has a value (whether it is a number, text, whatever) in the cell. I feel like this should be a fairly simple to do?

Thanks!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

CC268

Active Member
Joined
Mar 7, 2016
Messages
328
I believe I can actually just use conditional formatting for this...currently trying to figure that out.
 

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
12,224
Sure!

1) Select the cells you want to format, you can just click the little triangle between the A and 1 to select the whole sheet.
2) Click Conditional Formatting --> New Rule --> Use a formula
3) Type this formula:
=LEN(A1)>0
where A1 is the upper left corner of your selected range
4) Click Format --> Border, and choose a border.
5) Click OK

That should do it. Let me know how it works.
 

CC268

Active Member
Joined
Mar 7, 2016
Messages
328
Sure!

1) Select the cells you want to format, you can just click the little triangle between the A and 1 to select the whole sheet.
2) Click Conditional Formatting --> New Rule --> Use a formula
3) Type this formula:
=LEN(A1)>0
where A1 is the upper left corner of your selected range
4) Click Format --> Border, and choose a border.
5) Click OK

That should do it. Let me know how it works.

Hmm..that arbitrarily added borders all over the place...some with text, but a lot with no text in them too...not sure why. Maybe there was some formatting previously applied that needs to be cleared? I guess I could try a new clean sheet and see if it works.
 

CC268

Active Member
Joined
Mar 7, 2016
Messages
328

ADVERTISEMENT

It worked! I must have had some weird formatting going on...I just copied it over to a new sheet and it worked perfectly! Thanks! I will have to look up that LEN function so I know what it actually going on there lol
 

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
12,224
The LEN function just returns the length of the cell. If you got a lot of cells being formatted that looked empty, then they probably had some spaces or non printable characters in them, which is why going to a new sheet worked. You can change the formula to

=LEN(TRIM(A1))>0

to handle spaces. You can add the CLEAN function if you think there's non-printable characters. It's also worth noting that this works on the value of the cell. If there's a formula that returns "", then it won't be formatted.

Glad to help!
 

CC268

Active Member
Joined
Mar 7, 2016
Messages
328
Interesting...so much to learn...a bit overwhelming for a complete newbie like me - but I will keep trying stuff out! Thanks!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,625
Messages
5,838,427
Members
430,547
Latest member
jopshio

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