Set cell width and height based on cm value

288enzo

Board Regular
Joined
Feb 8, 2009
Messages
104
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]
 

288enzo

Board Regular
Joined
Feb 8, 2009
Messages
104
Office Version
  1. 2016
Platform
  1. Windows
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
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,614
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

288enzo

Board Regular
Joined
Feb 8, 2009
Messages
104
Office Version
  1. 2016
Platform
  1. Windows
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:

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,809
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
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.
 

288enzo

Board Regular
Joined
Feb 8, 2009
Messages
104
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,678
Messages
5,637,741
Members
416,981
Latest member
PLonchar

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
Top