Consulting

Results 1 to 3 of 3

Thread: Format Column Labels

  1. #1
    VBAX Regular
    Joined
    Jan 2016
    Posts
    55
    Location

    Format Column Labels

    I have this piece of code that I would like to improve. There must be a better way to do it.

    Appreciate any help on this.

    Thanks.

    ActiveSheet.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = "Products1"
        Columns(1).EntireColumn.Insert
        Columns(1).Cells(1, 1).Value = "ID"
        Columns(7).Cells(1, 1).Value = "Column1"
        Columns(7).Cells(1, 1).Interior.ColorIndex = 27
        Columns(7).Cells(1, 1).Font.Bold = True
        Columns(8).Cells(1, 1).Value = "Column2"
        Columns(8).Cells(1, 1).Interior.ColorIndex = 27
        Columns(8).Cells(1, 1).Font.Bold = True
        Columns(9).Cells(1, 1).Value = "Column3"
        Columns(9).Cells(1, 1).Interior.ColorIndex = 27
        Columns(9).Cells(1, 1).Font.Bold = True
        Columns(10).Cells(1, 1).Value = "Column4"
        Columns(10).Cells(1, 1).Interior.ColorIndex = 27
        Columns(10).Cells(1, 1).Font.Bold = True
        Columns(11).Cells(1, 1).Value = "Column5"
        Columns(11).Cells(1, 1).Interior.ColorIndex = 27
        Columns(11).Cells(1, 1).Font.Bold = True
        Columns(12).Cells(1, 1).Value = "Date"
        Columns(12).Cells(1, 1).Interior.ColorIndex = 27
        Columns(12).Cells(1, 1).Font.Bold = True
        Columns(12).Cells(2, 1).Value = Format(Now(), "mm/dd/yyyy")

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Many ways -- here's one


    Option Explicit
    
    Sub test()
        'ActiveSheet.Copy After:=Sheets(Sheets.Count)
        'ActiveSheet.Name = "Products1"
        'Columns(1).EntireColumn.Insert
        
        Dim aryColHeaders As Variant
        'base = 0
        aryColHeaders = Array("Column1", "Column2", "Column3", "Column4", "Column5", "Date")
        
        With Worksheets("Products1")
            .Cells(1, 1).Value = "ID"
            With .Cells(1, 7).Resize(1, UBound(aryColHeaders) + 1)
                .Value = aryColHeaders
                .Interior.ColorIndex = 27
                .Font.Bold = True
            End With
            .Cells(2, 12).Value = Format(Now(), "mm/dd/yyyy")
        End With
    End Sub
    
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Jan 2016
    Posts
    55
    Location
    Thank you Paul. It works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •