Solved

formatting using VBA

Posted on 2016-10-16
3
50 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 18

Accepted Solution

by:
Roy_Cox earned 500 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 18

Expert Comment

by:Roy_Cox
ID: 41861590
Pleased to help
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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

863 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

27 Experts available now in Live!

Get 1:1 Help Now