Solved

Excel 2010 VBA to format cells

Posted on 2013-06-25
11
673 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:Skip Sleeper
  • 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
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)

 
LVL 27

Expert Comment

by:MacroShadow
ID: 39275011
@fargus47

Your code works fine for me.
0
 

Author Comment

by:Skip Sleeper
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
 
LVL 27

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:Skip Sleeper
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:Skip Sleeper
ID: 39275926
OK, thanks for everything!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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