[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

formatting using VBA

Posted on 2016-10-16
3
Medium Priority
?
69 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
[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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

650 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