Solved

formatting using VBA

Posted on 2016-10-16
3
43 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:sagardeo
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
Roy_Cox earned 500 total points
Comment Utility
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)
Comment Utility
if not, record a macro as you do the formatting manually and it can then be merged into your code
0
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
Pleased to help
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
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.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

763 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

10 Experts available now in Live!

Get 1:1 Help Now