Autoscale cells excel vba - excel

I am trying to autoscale my table to always fit the window size. I'm trying to get the width of the excel window. I want to get a differens in some form when you resize the excel application. And then I am trying to save it to the variable "w", and compare it to the last time the windows changed size "w_old". And the problem is that I am not sure I am looking at the right place when I am looking at Application.Width because every time I run the script it runs the if statement even if I have not change the size of my excel window.
Dim w As Long
Dim w_old As Long
w = Application.Width
If (w <> w_old) Then
Columns("A:K").Select
ActiveWindow.Zoom = True
Range("A1").Select
w_old = w
End If
Thanks in advance!

Related

Excel histogram bins adjustment with VBA doesn't seem to work

I'm trying to automatically create couple histograms for given data. The problem is that I can't change bins width with VBA. How to get around this?
(Excel Version 2210 Build 16.0.15726.20188)
I looked through the docs and tried the macro recorder and this is prototype code I came up with.
Sub generateHistograms()
Dim wsTab As Worksheet
Set wsTab = Worksheets("Tabele")
With wsTab.Shapes.AddChart2(-1, xlHistogram)
.Name = "Test"
.Chart.SeriesCollection.NewSeries
.Chart.FullSeriesCollection(1).Values = wsTab.ListObjects(1).ListColumns(3).DataBodyRange
.Chart.ChartGroups(1).BinsType = xlBinsTypeManual
.Chart.ChartGroups(1).BinWidthValue = 10
.Chart.ChartGroups(1).BinsOverflowEnabled = True
.Chart.ChartGroups(1).BinsOverflowValue = 90
End With
End Sub
Running the code line by line seems like BinsType, BinWidthValue, BinsOverflowEnabled and BinsOverflowValue doesn't do anything.

Excel VBA graph based on Series not working properly

I'm using the following VBA code in Excel in order to produce 2 graphs with a data picked up from the sheet and slightly elaborated. As I'm not using ranges directly for the graphs and need to use arrays for X and Y axes, I decided to go for Series. X has Date type and Y has Integer type. First it worked, but then the graphs started giving me wrong data, something random coming directly off the sheet. I suppose there could be something wrong in the way how the charts are declared. Here's the code, what could go wrong with it?
Dim crt1, crt2 As Chart
Dim sr1, sr2 As Series
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered, 60, 50).Select
Set crt1 = ActiveChart
Set s1 = crt1.SeriesCollection.NewSeries()
sr1.Values = dataArray1
sr1.XValues = datesArray1
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
Set crt2 = ActiveChart
Set s2 = crt2.SeriesCollection.NewSeries()
sr2.Values = dataArray2
sr2.XValues = datesArray2
This problem also persists when I comment the code for the second graph.
P.S. Excel says "Runtime error 424. Object required" underlining "sr1.Values = dataArray1" and then "sr1.XValues = datesArray1"

How to prevent Excel UserForm from shrinking/expanding autonomously?

My Excel UserForms contain a variety of objects, including text boxes, combo boxes, radio buttons, etc. The UserForm and the objects on the UserForm shrink and expand when my laptop is on a docking station and the VBA window is open on a larger monitor.
When I access the UserForm editor from the Forms tab in VBA, I can drag the UserForm resize handles and the objects in the UserForm will immediately snap back to their original state, but I want to do this programmatically so that the end user will not deal with shrunken/expanded UserForms.
I have tried resizing the UserForm upon opening (UserForm_Initialize), but it seems as if the shrinking/expanding takes place while the UserForm is not active, meaning that my UserForm resizing only acts to return the UserForm to its shrunken/expanded state and not its original state.
Sub UserForm_Initialize()
Call ResizeUserform(Me)
End Sub
Sub ResizeUserform(UserForm_Name As Object)
UserForm_Name.Width = UserForm_Name.Width + 0.001
UserForm_Name.Width = UserForm_Name.Width - 0.001
UserForm_Name.Height = UserForm_Name.Height + 0.001
UserForm_Name.Height = UserForm_Name.Height - 0.001
End Sub
Don't leave your form's dimensions ambiguous or prone to logic circularity (i.e. as a function of itself); set them up before loading/showing.
i.e.:
'where XX and YY are integer constants:
With YourFormName
.width=XX
.height=YY
.show
end with
If you absolutely need to incur in circular statements, do it indirectly by storing your calculated variable in a global/local variable, and then proceed to declare its properties (i.e. YourFormName.width=variable)
Good luck!.
I had a similar issue, ever time the program opened the Login user form would be smaller than the last time. It only did it on my lap top with extra monitors. I finally used
Private Sub UserForm_Initialize()
With Userform
.Width = Application.Width * 0.3
.Height = Application.Height * 0.6
End With
End Sub
in the userforms code and it kind of stopped meaning it no longer showed the user the change but if I open the VBA it had changed size on the Height and Width but since it was only in the VBA and the user didn't have to try and enter a password in a mini box its fine.
I had a similar problem. The only thing that worked for me was going into the Advanced Options of Excel and checking the box, "Disable hardware graphics acceleration." I wasted several hours trying to find this. I hope this helps someone else!
Don't make the form maximized. "Maximized" means you don't know at design-time what the size of the form will be, because you're making that depend on what monitor size it's being displayed in.
Larger monitor = larger maximized form, that's by design.
If you don't want the form to resize automatically when it's displayed in a different-size monitor, then don't maximize it.
Alternatively, handle the form's Resize event, and programmatically move each control where it needs to be relative to the bottom-right edge. Note, that's tricky and extremely annoying code to write, especially if the form is any kind of complicated. Much simpler to just not have a maximized form.
this operation can also be done with a loop. I used the button to increase and decrease the height of userform. I created a loop and assigned it to the button.At the same time there was a nice animation.
You set a height value, when the button is pressed, the userform becomes longer if the height is less than this value, and the userform becomes shorter if it is larger.
Codes:
Private Sub CommandButton4_Click()
Dim X, d, yuk, mak As Integer
For X = 1 To 100
DoEvents
If e = 0 Then
d = d + 10
yuk = 242
mak = 342
Else
d = d - 10
yuk = 345
mak = 245
End If
UserForm2.Height = yuk + d
If UserForm2.Height >= mak And e = 0 Then GoTo 10
If UserForm2.Height <= mak And e = 1 Then GoTo 20
Next
10 CommandButton4.Caption = "<"
e = 1
ListBox1.ListIndex = 0
ScrollBar1_Change
Exit Sub
20 CommandButton4.Caption = ">"
e = 0
ListBox1.ListIndex = 0
End Sub
It's video :https://www.youtube.com/watch?v=1LCxgQGy9tU
Source and Example file here

Excel VBA Chart Positions not constant

I have a worksheet where I create graphs in VBA and then place them according to named ranges in the worksheet.
graph3.Height = gRange.Height
graph3.Width = gRange.Width
graph3.Top = gRange.Cells(1, 1).Top
graph3.Left = gRange.Cells(1, 1).Left
It used to work fine but for some reason the graph no longer is positioned in the correct location. When I set a a stop point on any of the four lines and then continue to run the procedure, the graph returns to the correct position. Has anyone ever experienced this and how can I correct it?

VBA Excel Button resizes after clicking on it (Command Button)

How can I stop a button from resizing? Each time I click on the button, either the size of the button or the font size changes.
Note: I cannot lock my sheet as my Macro will write into the sheet.
Autosize is turned off. I run Excel 2007 on Windows 7 (64 Bit).
I use the following for ListBoxes. Same principle for buttons; adapt as appropriate.
Private Sub myButton_Click()
Dim lb As MSForms.ListBox
Set lb = Sheet1.myListBox
Dim oldSize As ListBoxSizeType
oldSize = GetListBoxSize(lb)
' Do stuff that makes listbox misbehave and change size.
' Now restore the original size:
SetListBoxSize lb, oldSize
End Sub
This uses the following type and procedures:
Type ListBoxSizeType
height As Single
width As Single
End Type
Function GetListBoxSize(lb As MSForms.ListBox) As ListBoxSizeType
GetListBoxSize.height = lb.height
GetListBoxSize.width = lb.width
End Function
Sub SetListBoxSize(lb As MSForms.ListBox, lbs As ListBoxSizeType)
lb.height = lbs.height
lb.width = lbs.width
End Sub
I added some code to the end of the onClick thus:
CommandButton1.Width = 150
CommandButton1.Height = 33
CommandButton1.Font.Size = 11
Seems to work.
I got the issue a slightly different way. By opening the workbook on my primary laptop display, then moving it to my big monitor. Same root cause I would assume.
Seen this issue in Excel 2007, 2010 and 2013
This code prevents the issue from manifesting. Code needs to run every time a active X object is activated.
Sub Shared_ObjectReset()
Dim MyShapes As OLEObjects
Dim ObjectSelected As OLEObject
Dim ObjectSelected_Height As Double
Dim ObjectSelected_Top As Double
Dim ObjectSelected_Left As Double
Dim ObjectSelected_Width As Double
Dim ObjectSelected_FontSize As Single
ActiveWindow.Zoom = 100
'OLE Programmatic Identifiers for Commandbuttons = Forms.CommandButton.1
Set MyShapes = ActiveSheet.OLEObjects
For Each ObjectSelected In MyShapes
'Remove this line if fixing active object other than buttons
If ObjectSelected.progID = "Forms.CommandButton.1" Then
ObjectSelected_Height = ObjectSelected.Height
ObjectSelected_Top = ObjectSelected.Top
ObjectSelected_Left = ObjectSelected.Left
ObjectSelected_Width = ObjectSelected.Width
ObjectSelected_FontSize = ObjectSelected.Object.FontSize
ObjectSelected.Placement = 3
ObjectSelected.Height = ObjectSelected_Height + 1
ObjectSelected.Top = ObjectSelected_Top + 1
ObjectSelected.Left = ObjectSelected_Left + 1
ObjectSelected.Width = ObjectSelected_Width + 1
ObjectSelected.Object.FontSize = ObjectSelected_FontSize + 1
ObjectSelected.Height = ObjectSelected_Height
ObjectSelected.Top = ObjectSelected_Top
ObjectSelected.Left = ObjectSelected_Left
ObjectSelected.Width = ObjectSelected_Width
ObjectSelected.Object.FontSize = ObjectSelected_FontSize
End If
Next
End Sub
(Excel 2003)
It seems to me there are two different issues:
- resizing of text of ONE button when clicking on it(though not always, don't know why), and
- changing the size of ALL buttons, when opening the workbook on a display with a different resolution (which subsist even when back on the initial display).
As for the individual resizing issue: I found that it is sufficient to modify one dimension of the button to "rejuvenate" it.
Such as :
myButton.Height = myButton.Height + 1
myButton.Height = myButton.Height - 1
You can put it in each button's clicking sub ("myButton_Click"), or implement it
a custom Classe for the "onClick" event.
I experienced the same problem with ActiveX buttons and spins in Excel resizing and moving. This was a shared spreadsheet used on several different PC's laptops and screens. As it was shared I couldn't use macros to automatically reposition and resize in code.
In the end after searching for a solution and trying every possible setting of buttons. I found that grouping the buttons solved the problem immediately. The controls, buttons, spinners all stay in place. I've tested this for a week and no problems. Just select the controls, right click and group - worked like magic.
Use a Forms button rather than an ActiveX one, ActiveX controls randomly misbehave themselves on sheets
Do you have a selection command in the buttons macro?
Shortly after I renamed some cells in a worksheet including one that the toggle button selects after its toggle function, the font size shrunk. I fixed this by making sure Range("...").Select included the new cell name, not the coordinates.
It happens when the screen resolution / settings change after Excel has been open.
For example:
Open a workbook that has a button on it
Log in with Remote Desktop from a computer with different screen size
Click on the button => the button size will change
The only solution I found is to close Excel and reopen it with the new screen settings. All instances of Excel must be closed, including any invisible instance executed by other processes without interface must be killed.
Old issue, but still seems to be an issue for those of us stuck on Excel 2007. Was having same issue on ActiveX Listbox Object and would expand its size on each re-calculate. The LinkCells property was looking to a dynamic (offset) range for its values. Restructuring so that it was looking to a normal range fixed my issue.
I had this problem using Excel 2013. Everything for working fine for a long time and all of sudden, when I clicked on the button (ActiveX), it got bigger and the font got smaller at the same time.
Without saving the file, I restarted my computer and open the same Excel file again and everything is fine again.
Mine resized after printing and changing the zoom redrew the screen and fixed it
ActiveWindow.Zoom = 100
ActiveWindow.Zoom = 75
Found the same issue with Excel 2016 - was able to correct by changing the height of the control button, changing it back, then selecting a cell on the sheet. Just resizing did not work consistently. Example below for a command button (cmdBALSCHED)
Public Sub cmdBALSCHED_Click()
Sheet3.cmdBALSCHED.Height = 21
Sheet3.cmdBALSCHED.Height = 20
Sheet3.Range("D4").Select
This will reset the height back to 20 and the button font back to as found.
After some frustrated fiddling, The following code helped me work around this Excel/VBA bug.
Two key things to note that may help:
Although others have recommended changing the size, and then immediately changing it back, notice that this code avoids changing it more than once on single toggle state change. If the value changes twice during one event state change (particularly if the second value is the same at the initial value), the alternate width and height properties may not ever be applied to the control, which will not reset the control width and height as it needs to be to prevent the width and height value from decreasing.
I used hard-coded values for the width and height. This is not ideal, but I found this was the only way to prevent the control from shrinking after being clicked several times.
Private Sub ToggleButton1_Click()
'Note: initial height is 133.8 and initial width was 41.4
If ToggleButton1.Value = True Then
' [Code that I want to run when user clicks control and toggle state is true (not related to this issue)]
'When toggle value is true, simply change the width and height values to a specific value other than their initial values.
ToggleButton1.Height = 40.4
ToggleButton1.Width = 132.8
Else
' [Code that I want to run when user clicks control and toggle state false (not related to this issue)]
'When toggle value is false adjust to an alternate width and height values.
'These can be the same as the initial values, as long as they are in a separate conditional statement.
ToggleButton1.Height = 41.4
ToggleButton1.Width = 133.8
End If
End Sub
For a control that does not toggle, you may be able to use an iterator variable or some other method to ensure that the width and height properties alternate between two similar sets of values, which would produce an effect similar the toggle state changes that I used in this case.

Resources