Set cell width and height based on cm value

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
717
Office Version
  1. 2016
Platform
  1. Windows
Hi all - I have a simple script to set the width and height of cells based on the numbers in B1 and C1. What I need help with is how to set the width and height based on centimeters. Thanks for any guidance.

VBA Code:
    Columns("A:A").Select

    Selection.RowHeight = [B1]

    Selection.ColumnWidth = [C1]
 
For column width try
VBA Code:
   Dim i As Long
   With Range("A:A")
      For i = 1 To 3
         .ColumnWidth = Application.InchesToPoints([C1]) * (.ColumnWidth / .Width)
      Next i
   End With

For consistency I tried to add a line for height, but it's erroring out. Width still works, but the next line I get an error.

VBA Code:
   Dim i As Long
   With Range("A:A")
      For i = 1 To 3
         .ColumnWidth = Application.InchesToPoints([B1]) * (.ColumnWidth / .Width)
         .ColumnHeight = Application.InchesToPoints([C1]) * (.ColumnHeight / .Height)
      Next i
   End With
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
No need to put the rowheight in the loop, try
VBA Code:
   Dim i As Long
   With Range("A:A")
      For i = 1 To 3
         .ColumnWidth = Application.InchesToPoints([c1]) * (.ColumnWidth / .Width)
      Next i
      .RowHeight = Application.InchesToPoints([b1])
   End With
 
Upvote 0
Solution
The strangest thing is happening, perhaps someone can explain what is going on.

When I run the script in normal view all is good
When I run the script in page layout view all is good

When I run the script in normal view then switch to page layout view the cell goes from 2"Wx2H" (192x192 pixels) to 2.15"x2" (206x192 pixels).

Thank you,
 
Last edited:
Upvote 0
I think When you Switch Between Normal view & Page Layout View, Units converted from points to your default Unit & when You Back Convertion isn't Accurate.
After Run code, Make Zoom Sheet & see are Width and Height is Equal or Not, Without Going to Page Layout View.
 
Upvote 0
I think When you Switch Between Normal view & Page Layout View, Units converted from points to your default Unit & when You Back Convertion isn't Accurate.
After Run code, Make Zoom Sheet & see are Width and Height is Equal or Not, Without Going to Page Layout View.

As long as I don't bounce back and forth I'm good, I just thought it was odd. Thanks for the quick reply on this.
 
Upvote 0

Forum statistics

Threads
1,213,514
Messages
6,114,078
Members
448,547
Latest member
arndtea

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top