Solved

Excel 2010 VBA to format cells

Posted on 2013-06-25
11
661 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 27

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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: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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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....
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

932 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

14 Experts available now in Live!

Get 1:1 Help Now