+ Reply to Thread
Results 1 to 8 of 8

Resizing cell in inches

Hybrid View

  1. #1
    Registered User
    Join Date
    01-23-2009
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2000
    Posts
    31

    Resizing cell in inches

    Hi Everybody,

    I need to redo cell sizes – the width and the height in inches
    While searching, I came across the a macro at http://processtrends.com/pg_charts_cell_size.htm

    As per this line of code, it seems cell width can be set only in number of characters the cell can accommodate.
    Columns(what_col).ColumnWidth = wide_char

    OK. So, with Application.DefaultWebOptions.PixelsPerInch, it is possible to convert the reqd cell width in inches to pix and use the formula
    Say,
    ColumnWidthInPix = ColumnWidthInInches * Application.DefaultWebOptions.PixelsPerInch

    And then define cell width in characters with
    wide_char = (wide_pix - 5) / 5.

    Now the question is
    1. Why this “-5” and the division by 5 while converting from pix to character ? Pixel sizes vary. So why this constant “-5” and division by 5 ?
    2. There seems to some connection with the width of standard font and cell sizes. If so what? How is it possible to find the width of the standard font via VBA ?
    3. Or is there a way to define cell width in inches directly ?

    Thanks in advance,

    Geetha

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Resizing cell in inches

    Hi,

    Does the following help
    http://support.microsoft.com/kb/73591

    Remember 1inch = 96 pixels = 72 points

    Rgds
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Resizing cell in inches

    I think pixels/inch varies by display.

    Perhaps you can adapt this:

    Sub MakeSquareCells()
        ' shg 2006
        ' for my LaserJet:
        Const fHScal    As Single = 1.08
        Const fVScal    As Single = 0.99
    
        Dim fPts        As Single
        Dim fCnv        As Single
        Dim sUnits      As String
        Dim r           As Range
    
        sUnits = LCase(Application.InputBox(Prompt:="Inches (inch) or millimeters (mm)?", _
                                            Title:="Units?", _
                                            Default:="inch", _
                                            Type:=2))
        If sUnits <> "inch" And sUnits <> "mm" Then Exit Sub
    
        fPts = Application.InputBox(Prompt:="Cell size, in " & sUnits & "?", _
                                    Title:="Size?", _
                                    Default:="1", _
                                    Type:=1)
    
        fCnv = IIf(sUnits = "inch", 72, 72 / 25.4)
        fPts = fCnv * fPts  ' convert to points
    
        If fPts < 0.01 Or fPts > 409 Then Exit Sub
    
        Set r = ActiveWindow.RangeSelection
        SquareCells r, fPts, fHScal, fVScal
    
        MsgBox Title:="SquareCells", _
               Prompt:=Format(fPts / fCnv, "0.000") & " (Target size, " & sUnits & ")" & vbLf & _
                       Format(r.Columns(1).Width / fCnv, "0.000") & " (Actual width)" & vbLf & _
                       Format(r.Rows(1).Height / fCnv, "0.000") & " (Actual height)" & vbLf & _
                       Format(r.Columns(1).Width / r.Rows(1).Height, "0.000") & " (Aspect)"
    End Sub
    
    Function SquareCells(r As Range, _
                         ByVal fPts As Single, _
                         Optional fHScal As Single = 1, _
                         Optional fVScal As Single = 1) As Single
        ' Sets all cells in r to be approximately fPts x fPts (points)
        ' Returns the actual aspect H/W
        ' fHScal and fVScal compensate for the printer; obtain by measuring
    
        ' For scale factors:
        ' Make a sheet with cells of nominal size (e.g., 1") and scale factors set to 1
        ' Measure the horizontal size as printed; aay it's 1.03 inches. Use 1.03 as fHScal
        ' Ditto for vertical size and fVScal
    
        Dim i           As Long
        Dim sDim        As String
    
        r.Select
    
        For i = 1 To 4
            r.Columns.ColumnWidth = r.Columns(1).ColumnWidth / r.Columns(1).Width * fPts / fHScal
            r.Rows.RowHeight = fPts / fVScal
        Next i
    
        SquareCells = r.Columns(1).Width / r.Rows(1).Height
    End Function
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    01-23-2009
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2000
    Posts
    31

    Re: Resizing cell in inches

    Hi Richard,
    Thanks for that prompt reply. That link helped me understand better how characters fit into a cell.

    Hi shg,
    That code is great, especially fVsacl & fHSacl And the flexibilty between Cms & Inches. Wonder how long it took you to come up with this. I was not comfortable working with pixels as they vary.
    But i think it needs one correction. In this line ---- fCnv = IIf(sUnits = "inch", 72, 72 / 25.4) ----- there is a typo. For cms, fCnv should be 72 / 2.54. Correct me if I am wrong.

    One more thing, since I am still learning, why is SquareCells a function and not a regular sub ? I thought functions were required when a procedure needs to give out a result, like UDFs. Please, can you explain ?

    I found this too, at http://www.dailydoseofexcel.com/arch...ths-in-points/ . The suggestion that setting column width needs to be done 3times atleast seems to be true.

    Thanks once again and regards to both of you,

    Geetha
    Last edited by Geetha Gupta; 11-27-2010 at 09:15 PM.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Resizing cell in inches

    That code is great, ...
    You're welcome, glad it helped.
    For cms, fCnv should be 72 / 2.54
    The alternative units are mm, not cm
    why is SquareCells a function and not a regular sub
    It returns the actual aspect of the cells.

  6. #6
    Registered User
    Join Date
    01-23-2009
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2000
    Posts
    31

    Re: Resizing cell in inches

    Dear shg,
    I am sorry, I did'nt notice that "mm". For some reason , i assumed it was cm.

    I think your code sets columns and rows 4times. So, rows too need repeated setting, is it ? but since rows are set in points, which is already being done, why do it again and again ?

    The actual aspect of the cells - what does it mean ? And how does a Function procedure do that ? I think I am deviating. So if the answer is short, it is OK. Else i should start a new thread. Please advice.

    Thanks and regards,

    Geetha

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Resizing cell in inches

    The actual aspect of the cells - what does it mean ?
    Here, the ratio of height to width.
    And how does a Function procedure do that ?
    Look at the last line of code.

  8. #8
    Registered User
    Join Date
    01-23-2009
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2000
    Posts
    31

    Re: Resizing cell in inches

    Hi shg,

    Got it.

    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1