horizontal alignment in VBA crashes Excel

asrich

New Member
Joined
Nov 5, 2009
Messages
6
I Need help here. When I try to set the horizontal alignment using HorizontalAlignment=xlCenter the procedure halts right at that command. There are no error messages. Excel does not enter break mode. I can execute the command successfully from the immediate window. I will post the code leading up to the problem below. There is code that follows what I am posting, but the problem is with the horizontal alignment statement. I have been struggling with this for weeks now!

FYI the variable Output is a global that holds the starting cell (Range) of where the user wants the results of my procedure to appear on the sheet.

Private Sub cmdOK_Click()
'


'This procedure is used to display a histogram from a set of data


'Declare Variables
Dim kount As Double 'Holds the number of cells selected
Dim Min As Double 'Holds the min of the data
Dim Max As Double 'Holds the max of the data
Dim NumBins As Integer 'Holds the number of bins needed
Dim BinW As Double 'Holds the bin width
Dim Frequency As Long 'Holds the histogram frequencies
Dim i As Long 'Loop counter
Dim j As Long 'Loop Counter
Dim CurBin As Double 'Used to calculate the bin values
Dim OldBin As Double 'Used to calculate the bin values
Dim Data As Variant 'Holds the range of the input data
Dim ErrorText As String 'Holds Message Box Text
Dim FrequencyValues As Variant 'Holds the range to make the chart
Dim BinValues As Variant 'Holds the range to make the chart
Dim RelFrequencyValues As Variant 'Holds the range for the cumulative probability chart
Dim Mean As Double 'Holds the Mean of the results
Dim Median As Double 'Holds the Median of the results
Dim SD As Double 'Holds the standrad deviation of the results
Dim NormalValues As Variant 'Holds the range of the normal probabilities
Dim k As Long 'Holds the value for the kth data point
Dim ci As Single 'Holds the confidence interval alpha
Dim SmallLimit As Double 'Holds the lower limit of the confidence interval
Dim LargeLimit As Double 'Holds the upper limit of the confidence interval
Dim ChartArea As Variant 'Where should we put the charts?
Dim chtObj As Variant 'Holds the parent chart object (for moving the charts)
Dim ChtOb As ChartObject 'Holds the chart object for moving the chart
Dim RngToCover As Range 'Where should we move the chart?
Dim r As Long, c As Long
Dim OutputKey As Variant 'Registry key name
Dim ChosenSheetName As String 'The sheet chosen by the user for output
Dim xAxisTitle As String 'Holds the x-axis title
Dim ChartName As Variant 'Holds the chart name


Application.ScreenUpdating = False


If refOutPutRange = "" Then
MsgBox "You must select an output range!", vbCritical + vbOKOnly, "Histogram Error"
Exit Sub
End If


'Where do we put the output?
Output = refOutPutRange




'SaveSetting "RCMonteCarlo", "Variables", "Output", Output
ChosenSheetName = Left(Output, InStr(Output, "!") - 1)


'Store text just above the first cell of data for our chart x-axis titles
xAxisTitle = Range(DataBegin).Offset(-1, 0).Value






'First select the entire column of data
Range(Selection, Selection.End(xlDown)).Select
Data = Selection.Address






'Determine how much data we have
kount = Selection.Count


'Find the min and max of the data so we can calculate the bin range
Min = WorksheetFunction.Min(Selection)
Max = WorksheetFunction.Max(Selection)




'Find the mean, median and standard deviation
Mean = WorksheetFunction.Average(Selection)
Median = WorksheetFunction.Median(Selection)
SD = WorksheetFunction.StDev_S(Selection)


'Find the 51, 95 and 99 percentiles
ci = 0.49 / 2
k = ci * kount
SmallLimit = WorksheetFunction.small(Selection, k)
LargeLimit = WorksheetFunction.large(Selection, k)
Range(Output).Offset(4, 7).Value = SmallLimit
Range(Output).Offset(4, 9).Value = LargeLimit


ci = 0.05 / 2
k = ci * kount
SmallLimit = WorksheetFunction.small(Selection, k)
LargeLimit = WorksheetFunction.large(Selection, k)
Range(Output).Offset(5, 7).Value = SmallLimit
Range(Output).Offset(5, 9).Value = LargeLimit


ci = 0.01 / 2
k = ci * kount
SmallLimit = WorksheetFunction.small(Selection, k)
LargeLimit = WorksheetFunction.large(Selection, k)
Range(Output).Offset(6, 7).Value = SmallLimit
Range(Output).Offset(6, 9).Value = LargeLimit




'Calculate the number of bins needed
NumBins = WorksheetFunction.Ceiling(Sqr(kount), 1)


'Calculate the bin width
BinW = (Max - Min) / NumBins


'Activate the first output cell
'Range(Output).Select


'Write the headings
Range(Output) = "Bin"
Range(Output).Offset(0, 1).Value = "Frequency"
Range(Output).Offset(0, 2).Value = "Cum. Probability"
Range(Output).Offset(0, 4).Value = "Normal Dist."
Range(Output).Offset(0, 6).Value = "Mean"
Range(Output).Offset(0, 7).Value = "Median"
Range(Output).Offset(0, 8).Value = "S. Dev"
Range(Output).Offset(0, 9).Value = "Min"
Range(Output).Offset(0, 10).Value = "Max"
Range(Output).Offset(0, 11).Value = "n"
Range(Output).Offset(0, 11).HorizontalAlignment = xlCenter
Range(Output).Offset(4, 6).Value = "51% CI"
Range(Output).Offset(5, 6).Value = "95% CI"
Range(Output).Offset(6, 6).Value = "99% CI"
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Forgot to mention that Excel does not immediately crash. But the next thing I click on, I get an "out of memory error" and then Excel crashes.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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