Results 1 to 3 of 3

Set Window Size based on Cell Reference

Threaded View

  1. #1
    Forum Contributor
    Join Date
    11-21-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    150

    Set Window Size based on Cell Reference

    Basically the code works something like this:

    - Click on cell H1
    - open a new window
    - resize the active window to the named range ("labor")
    - center active window to center of the screen

    The area I'm having problems with is resizing the active window to the range selected. While it is possible to manually adjust the width and height until it works, I'd like to be able to base this off cell references. It's easy enough to get the max column (c) and row (r) from the named range, but is it possible to set the window limits to not exceed c (width) and not exceed r (height) ?

    Sub NewWindow()
    
    ' open a new window
    ActiveWindow.NewWindow
    Worksheets("Sheet1").Activate
    
    'remove some options to make this look more like a userform
    Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"",False)"
    Application.DisplayStatusBar = Not Application.DisplayStatusBar
    ActiveWindow.DisplayWorkbookTabs = False
    
    'resize active window such that r is the max width and c is the max height
    Dim r As Long, c As Long, rng As Range
    Set rng = Range("Labor")
    
    For r = 1 To rng.Rows.Count - 1
    Next r
    
    For c = 1 To rng.Columns.Count - 1
    Next c
    
    Debug.Print "row = " & r, "column = " & c
    
    'center active window to center of screen
    Dim maxWidth As Integer
    Dim maxHeight As Integer
    Application.WindowState = xlMaximized
    maxWidth = Application.Width
    maxHeight = Application.Height
    
    CenterApp maxWidth, maxHeight
        
    End Sub
    
    Sub CenterApp(maxWidth As Integer, maxHeight As Integer)
     
     Dim appLeft As Integer
     Dim appTop As Integer
     Dim appWidth As Integer
     Dim appHeight As Integer
     
     Application.WindowState = xlNormal
     appLeft = maxWidth / 4
     appTop = maxHeight / 4
     appWidth = maxWidth / 2
     appHeight = maxHeight / 2
     Application.Left = appLeft
     Application.Top = appTop
     Application.Width = appWidth
     Application.Height = appHeight
     
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Is it possible to set window size on open and keep it that size
    By barman in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2019, 01:19 AM
  2. [SOLVED] Control application window size based on resolution and dispaly specified rows and columns
    By dentler in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-18-2017, 09:26 PM
  3. [SOLVED] For step loop, need step size to change based on reference cell in row
    By Telperion in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-22-2013, 03:41 PM
  4. Limiting excel window size to match cell range?
    By jonvanwyk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-23-2012, 04:05 PM
  5. Set window size with VBA.
    By c_leven3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2010, 03:15 PM
  6. How to set the size for the new window
    By lizaro in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-04-2008, 07:42 AM
  7. [SOLVED] Format cell window size
    By KHall1968 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-13-2005, 07:05 AM

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