Consulting

Results 1 to 10 of 10

Thread: A better way to enter heading captions?

  1. #1
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location

    A better way to enter heading captions?

    Hi, All!

    Somewhere, here, in the past week or two, I believe I noticed someone using some code to expedite entering Column headings. As I recall, the heading line (or lines) were concatenated (comma separated?) in a string. When the occasion called for creating the heading line(s), the code then parsed the strings.

    Does this feature ring any bells for you? It was in the code but was not the focus of OP's question. I noodled about with some searches but came up with zero. Can you point me at the title (or words you recall being in it)?

    Of interest is seeing how formating of the heading captions was handled.

    I appreciate any help you can give.

    Thanks!

    Ron McKenzie
    Orlando, FL

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This works for me
    [VBA]myHeaders = Array("Name", "Address", "Phone")
    Range("A1").Resize(1, UBound(myHeaders) + (1 - LBound(myHeaders))).Value = myHeaders[/VBA]

    perhaps this is similar to what you are thinking of
    Dim myHeaders As Variant
    Dim myHeaderString As String
    
    myHeaderString = "Name,Address,Phone"
    myHeaders = Split(myHeaderString, ",")
    Range("A1").Resize(1, UBound(myHeaders) + 1).Value = myHeaders

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    As for formatting, create your own Header Style and apply it
    [VBA]
    Range("A1").Resize(1, UBound(myHeaders) + 1).Style = MyHeaderStyle
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Malcolm,

    How do I get from something like this for a cell's formatting:

    [vba]Private Sub FormatBlock(ByRef Block As Object)

    With Block

    .Fill.Visible = msoTrue
    .Fill.Solid
    .Fill.ForeColor.SchemeColor = 47
    .Fill.Transparency = 0.5
    .Line.Weight = 0.5
    .Line.DashStyle = msoLineSquareDot
    .Line.Style = msoLineSingle
    .Line.Transparency = 0#
    .Line.Visible = msoTrue
    .Line.ForeColor.SchemeColor = 47
    .Line.BackColor.RGB = RGB(255, 255, 255)
    End With
    End Sub[/vba]
    to:

    ... = myHeaderStyle

    ??

    Thanks,

    Ron
    Orlando, FL

  5. #5
    VBAX Regular
    Joined
    Jun 2007
    Posts
    69
    Location
    Mike and Malcolm,

    Thanks for your suggestions, they line up well with what I was remembering and want to do.
    Ron McKenzie
    Windermere, FL
    (living in the huge shadow of a tiny rodent)

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Glad to help Ron. Personally, I've never used styes, but as I understand them, this is what they were designed for.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Jun 2007
    Posts
    69
    Location
    Malcolm,

    How do I assign formatting 'data' to myHeaderStyle ?

    Or, asked a different what, where do I define myHeaderStyle ?

    (I'm feeling dense at the moment.)
    Ron McKenzie
    Windermere, FL
    (living in the huge shadow of a tiny rodent)

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    As I said, I never use them. I would look up the Help file, personally. Let me know what you find. I'm always willing to learn! I've seen very little use of Styles here. Maybe you could become our "style expert"?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I use styles sometimes. Temporary highlights is another use for them.

    [VBA]Range("A1:c10").Style = "HeaderFormat"[/VBA]

    The macro recorder is the best way to get code that defines a style.
    Since styles are stored with the spreadsheet, a VB routine that defines a style sometimes only runs at design time.

    On advantage of useing styles is that the user can change the headers to match their taste. The user can alter the "HeaderFormat" Style without opening the VBEditor.
    Last edited by mikerickson; 02-23-2008 at 10:16 PM.

  10. #10
    VBAX Regular
    Joined
    Jun 2007
    Posts
    69
    Location
    Malcolm,

    This turns out to be very simple.

    Let's create myHeadingStyle:

    ? Select a cell (I chose the first cell in the range of my new heading)

    On the menu bar:
    ? click Format,
    ? click Style,
    ? create a Style Name by typing 'myHeadingStyle' in the TextBox
    ? click Add

    (Note you can click the arrow to see the DropList of styles that already exist. 'Normal' means no custom style was applied.)

    There is one checkbox for each tab on the Format Cells dialog.
    ? Click MODIFY button,

    Excel Opens the Format Cells dialog:
    ? Set the style choices that you want to use for your style.

    I selected the following:
    - Numbers: general (default)
    - Alignment: Center horizontally, center vertically, aligned (word-wrap)
    - Font: Arial 10, Bold
    - Borders - no borders (default)
    - Shading - no shading (default)
    - Protection - locked (default)

    ? click OK

    Now, you have your style established.

    To use my new style in my VBA code, I used the line you gave me:

    Range("I1").Resize(1, UBound(myHeaders) + (1 - LBound(myHeaders))).Style = "myHeadingStyle"

    Note that the style name is enclosed with double quote marks. Without the quotes I get a compile error, variable not defined.

    This seems to be all that there is to the process.
    Ron McKenzie
    Windermere, FL
    (living in the huge shadow of a tiny rodent)

Posting Permissions

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