[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

formatting using VBA

Posted on 2016-10-16
3
Medium Priority
?
70 Views
Last Modified: 2016-10-26
I had this question after viewing How to edit the macro code to limit the number of characters when creating worksheet tab name.

In the attached file, I have VBA code that I would like to add formatting as well.

1) Bolding the titles I have bolded in the attached (ignore the red, i just did that so it's easy to find)
2) wrap text for the title rows with longer titles ("Customer Totals" and "Contract adjustment")
3) Add top border to row with "Customer Totals".

Is there a way to do this in VBA?
VBA_Excel_-Test_FileQ28972223-Rev-1.xlsm
0
Comment
Question by:Judy Deo
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
Roy Cox earned 2000 total points
ID: 41846161
See if this does what you want.
Option Explicit

Sub UpdateSheets()
    Dim ws As Worksheet
    Dim cel As Range, rgData As Range, rgHeaders As Range, Total As Range
    Dim i As Long, n As Long, nRows As Long, lRw As Long
    Application.ScreenUpdating = False
    With Worksheets("Master")
        Set rgData = .Range("A2")
        Set rgData = Range(rgData, .Cells(.Rows.Count, rgData.Column).End(xlUp))
        Set rgData = Intersect(rgData.EntireRow, .UsedRange)
        Set rgHeaders = rgData.EntireColumn.Rows(1)
    End With
    n = rgData.Rows.Count

    For i = 1 To n
        Set cel = rgData.Cells(i, 1)
        'If Left(cel.Value, 10) = "Customer :" Then
        If Left(cel.Value, 15) = "Establishment :" Then
            Set Total = Nothing
            On Error Resume Next
            Set Total = rgData.Columns(1).Find("Customer Total", LookAt:=xlPart, MatchCase:=False, LookIn:=xlValues, after:=rgData.Cells(i, 1))
            If Not Total Is Nothing Then
                nRows = Total.Row - cel.Row + 1
                Set ws = Nothing
                'Set ws = Worksheets(Mid(cel.Value, 12))    'Customer :
                Set ws = Worksheets(Mid(cel.Value, 17))     'Establishment :
                If ws Is Nothing Then
                    Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count))
                    'ws.Name = Mid(cel.Value, 12)           'Customer :
                    ws.Name = Left(Mid(cel.Value, 17), 31)            'Establishment :
                    rgHeaders.Copy ws.Range("A1")
                End If
                With ws
                    ''/// wrap text in header row
                    .Range("A1:M1").WrapText = True
                    Intersect(.UsedRange, .Range("2:1048576")).ClearContents
                    rgData.Rows(i).Resize(nRows, rgData.Columns.Count).Copy
                    .Range("A2").PasteSpecial xlPasteValues

                    .Cells(nRows + 4, "F").Resize(7, 1).Value = Application.Transpose( _
                                                                Array("Total Supply", "Sales Cut", "Net", "", "Each", "Contract adjustment", "Final Rev"))
                    ''/// bold cells
                    .Cells(nRows + 4, "F").Font.Bold = True
                    .Cells(nRows + 4, "G").FormulaR1C1 = "=R[-3]C"
                    .Cells(nRows + 5, "G").FormulaR1C1 = "=R[-4]C[5]"
                    .Cells(nRows + 6, "G").FormulaR1C1 = "=R[-2]C-R[-1]C"
                    .Cells(nRows + 8, "G").FormulaR1C1 = "=R[-2]C/2"
                    .Cells(nRows + 10, "G").FormulaR1C1 = "=R[-2]C-R[-1]C"
                    ''/// top border
                    lRw = .Range("A1").CurrentRegion.Rows.Count
                    '
                    .Range("A" & lRw & ":M" & lRw).Select

                    With Selection.Borders(xlEdgeTop)
                        .LineStyle = xlContinuous
                        .ColorIndex = xlAutomatic
                        .TintAndShade = 0
                        .Weight = xlMedium
                    End With
                    ''/// auto fit columns
                    .Columns.AutoFit
                End With
            End If
            On Error GoTo 0

        End If
    Next
End Sub

Open in new window

0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 41846431
if not, record a macro as you do the formatting manually and it can then be merged into your code
0
 
LVL 22

Expert Comment

by:Roy Cox
ID: 41861590
Pleased to help
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

868 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