Solved

Excel 2010 VBA to format cells

Posted on 2013-06-25
11
653 Views
Last Modified: 2013-06-25
Hi Experts,

I've written a simple code based on macro recordings to create a border around a range of cells if the active cell is not blank and is within the row range of 4-50.  There are no bugs found in the code but it doesn't work.  Any help is appreciated, thank you!
000-Project-Estimator.xlsm
0
Comment
Question by:fargus47
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 24

Accepted Solution

by:
Steve earned 500 total points
ID: 39274940
Is there a reason why you do not use Conditional Formatting?

Do you want the code to 'fire' automatically?

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
'
' FormatCells Macro
'

'
    
  If Target.Column = 1 Then
            If Target.Row >= 4 And Target.Row <= 50 And _
            IsEmpty(Target.Value) Then
            End
            Else
            With Target
                
                .Borders(xlDiagonalDown).LineStyle = xlNone
                .Borders(xlDiagonalUp).LineStyle = xlNone
                With .Borders(xlEdgeLeft)
                    .LineStyle = xlContinuous
                    .ThemeColor = 1
                    .TintAndShade = -0.349986266670736
                    .Weight = xlThin
                End With
                With .Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .ThemeColor = 1
                    .TintAndShade = -0.349986266670736
                    .Weight = xlThin
                End With
                With .Borders(xlEdgeBottom)
                    .LineStyle = xlContinuous
                    .ThemeColor = 1
                    .TintAndShade = -0.349986266670736
                    .Weight = xlThin
                End With
                With .Borders(xlEdgeRight)
                    .LineStyle = xlContinuous
                    .ThemeColor = 1
                    .TintAndShade = -0.349986266670736
                    .Weight = xlThin
                End With
                With .Borders(xlInsideVertical)
                    .LineStyle = xlContinuous
                    .ThemeColor = 1
                    .TintAndShade = -0.349986266670736
                    .Weight = xlThin
                End With
            End With
            End If
            
    End If
End Sub

Open in new window

0
 
LVL 29

Expert Comment

by:gowflow
ID: 39274954
Is this what you want ??

type anything in  a cell form row 4 to row 50 in Column 1 and then press on macro and select it, it will give you what you designed a very light border.

gowflow
000-Project-Estimator.xlsm
0
 
LVL 29

Expert Comment

by:gowflow
ID: 39274973
No problem with your code Barman
gowflow
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39275011
@fargus47

Your code works fine for me.
0
 

Author Comment

by:fargus47
ID: 39275041
Hi,

I thought the code would "fire" automatically and not need a command button.  What would the correct code be to do that?  Also, I didn't know conditional formatting would just do borders.  

Thanks!
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 26

Expert Comment

by:MacroShadow
ID: 39275053
That's the code Barman gave you.
0
 
LVL 5

Expert Comment

by:DOSLover
ID: 39275080
It works for me. I don't see any problem with the code. May be the new border is not visible bcz it is lighter. I also tried including the following code, it shows up new color!
Selection.Font.ColorIndex = 3
0
 
LVL 24

Expert Comment

by:Steve
ID: 39275388
Conditional formatting can do just borders and is far better than code as it will "undo" automatically too.

The code I did at the start is for the auto fire macro too.

If either need explaining then let us know, we are happy to help.
0
 

Author Comment

by:fargus47
ID: 39275459
It works great, thanks!  Why didn't my code work?  I've not used "Target" before.  Also once i looked at bit more at conditional formatting it makes much more sense to use that.

Thanks!
0
 
LVL 24

Expert Comment

by:Steve
ID: 39275568
You need to pick the right workshhet event for excel to work automatically. Once you get that, objects like Target are set automatic too.
If you research worksheet events you will see that there are a few to choose from.
0
 

Author Comment

by:fargus47
ID: 39275926
OK, thanks for everything!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now