Results 1 to 11 of 11

Thread: [RESOLVED] Dynamic Height of Cell using Excel Automation

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Resolved [RESOLVED] Dynamic Height of Cell using Excel Automation

    Hello,

    I have data from a database and inserting it into Excel. Then I adjust the properties like this:

    Code:
            With xlsApp.Selection
                'Position and size of the textbox
                .Left = xlsSheet.Cells(cPVCRRowFirstPts + lngNbPts + 4, 3).Left + 3
                .Top = xlsSheet.Cells(cPVCRRowFirstPts + lngNbPts + 4, 1).Top
                .Width = 420#
                .Height = 114.75
                .Font.Size = 11
            End With
    I would like the height of the cell to be dynamic depending on the contents of the cell. Anybody know how to do this?

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Dynamic Height of Cell using Excel Automation

    Thanks RhinoBull, this Property works to modify the whole column, is there a way of modifying just the selected cell?

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Dynamic Height of Cell using Excel Automation

    I'm afraid not - you cannot make "A1" and "A2" cells to have different width or "A1" and "B1" to have different height.
    Unless I'm missing something...

    It's like in the any other grid: when you resize one cell you're actually resizing entire column (width) or row (height).

  5. #5

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Dynamic Height of Cell using Excel Automation

    Thanks RhinoBull again, I see what you mean.

    I just noticed that it is not a cell, it is a textbox object that is over the grid, and that is why I can change the height or width in my code above.

    I was thinking that if I count the number of lines that is in the textbox object, I could then multiply that by a factor (I will have to mesure the height of one line) and then adjust my height accordingly.

    So my second question would be, how would I calculate the number of lines in my textbox?

    Thanks for your assistance it is appreciated.

  6. #6

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Dynamic Height of Cell using Excel Automation

    In the end I counted the number of lines and multiplyed that by a factor of 13. Here is the code if anybody else every needs this in the future:

    I used the following API call. However this does not work with a TextBox you must use a Rich Edit Control for this to return the correct results.

    Code:
    Private Declare Function SendMessageAsLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const EM_GETLINECOUNT = 186
    Code:
    MsgBox "Height Of Excel TextBox = " & 13 * SendMessageAsLong(MyRichEditBox.hWnd, EM_GETLINECOUNT, 0, 0)

  7. #7

  8. #8

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: [RESOLVED] Dynamic Height of Cell using Excel Automation

    I found that number by trial and error, 10 was too short, 15 too big, 13 was just right. I know it sounds like the modern IT version of the Golldilocks story.

    Thanks for your help RhinoBull.

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] Dynamic Height of Cell using Excel Automation

    I figured that much ... ... so you confirmed.

    Anyway, you can set your Form's Font to whatever font you have in the yuor textbox so you can use TextHeight:
    Code:
    Dim iHeight As Integer
    
    Set Me.Font = Text1.Font 'or you can go with Font.Name/Size/etc...
    
    iHeight = TextHeight("A") '<<<--- here is your precise multiplyier - no guessing ;)
    'if value of iHeight is not enough for some reason then increase it:
    iHeight = iHeight * 1.1 'or whatever but do not use fixed (guessed) numbers like 13

  10. #10

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: [RESOLVED] Dynamic Height of Cell using Excel Automation

    Nice one, RhinoBull.

    I finally did it like this:

    Code:
    Debug.Print MyForm.TextHeight("A") / Screen.TwipsPerPixelY
    and guess what number it returned on my machine? The famous 13.

  11. #11

Posting Permissions

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



Click Here to Expand Forum to Full Width