Solved

formatting using VBA

Posted on 2016-10-16
3
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

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 19

Expert Comment

by:Roy_Cox
ID: 41861590
Pleased to help
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

749 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