Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

[VBA] Listbox Find Column Maximum Width

I am trying to figure out VBA code for Access which will set the widths of the columns in a listbox so that they are the width of the largest item in the column.

The principle isn't difficult:
go through the rows in a column and look a the width of that item, if the item width is greater than the largest so far store it and move on, else just move on. Repeat until you reach the end of the column.

Now I can do the code to loop through the rows in a column but the problem that I am having is that I haven't got a clue how I would go about getting the width of the item in that row. I can get the string length by not the width which I what I need.

I was wondering whether anyone knows how I would be able to do this?

Thanks for any help in advance.

View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Listbox Find Column Maximum Width
I am trying to figure out code which will set the widths of the columns in a listbox so that they are the width of the largest item in the column.

The principle isn't difficult:
go through the rows in a column and look a the width of that item, if the item width is greater than the largest so far store it and move on, else just move on. Repeat until you reach the end of the column.

Now I can do the code to loop through the rows in a column but the problem that I am having is that I haven't got a clue how I would go about getting the width of the item in that row. I can get the string length by not the width which I what I need.

I was wondering whether anyone knows how I would be able to do this?

Thanks for any help in advance.

How To Find The Maximum Height/Width Of The Screen.
How can I find the max height.width of the display screen?

Edited by - uncletr on 7/10/2004 6:04:27 PM

How To Find The Width Of A Flex Grid Column
I'm trying to figure out the width of a column in a msflexgrid.

Basically after the data is inserted, i want it to resize each coumn according to the text within it.
Then resize the flexgrid itself to fit the columns, but at the same time not allowing it to become wider than the form itself.

VB Code:
For z = 0 To frmMain.mfgPlacingdata.Cols    For x = 0 To frmMain.mfgPlacingdata.Rows - 1        frmMain.mfgPlacingdata.Col = z        frmMain.mfgPlacingdata.Row = x        intLen = Len(frmMain.mfgPlacingdata.ColWidth)        intLen2 = Len(frmMain.mfgPlacingdata.Text)        If intLen < intLen2 Then            frmMain.mfgPlacingdata.ColWidth = intLen2        End If    Next xNext z

this is what i have so far.. and it errors on

intlen = len(frmMain.mfgPlacingData.colwidth)
error is argument not optional...
any other suggestions?

About Column Width Of A Listbox
Hi there. Any anyone please tell me how to adjust the width (by excel VBA codel) of a multicolumn listbox for each column ?


Getting Column Width In Listbox
How can I get the size (width) of a column in a Listbox?

Getting Column Width In Listbox
How can I get the size (width) of a column in a Listbox?

ListBox Column Width

I can define the number of columns in an listbox element. But how can I define the width of each column and how can I write data into any a specific column ?
Thanks in advanace and kind regards....Zachi

Help.. Maximum Width Of A Form
Hey... Remember me, the rude kid that's doing a biology project..?
OK, so I was supposed to finish my project by tomorrow because I'm going to Florida, but I didn't finish on time.. So I'm going to have to bring my dad's laptop with us, and I want to work on my project there. Now, my dad's laptop is from like 1998.. it has Windows 98, and when I transferred my VB stuff there, I saw that the form is being cut off .. Now, the resolution is the same on both comptuers, 1024X768
But I noticed that the Width property of the form is 14000 in my computer (I guess VB doesn't use pixels as its units...), but on the laptop it's 10240, and I can't change it to anything greater than that. So obviously that's why my form is being cut off... which is VERY bad for me... So is that form width limit because of Windows 98? Did anyone here hear about that issue and knows if there's any way to fix it?

PictureBox Maximum Width ?
Hi guys

I am trying to accomplish a timeline filled with text boxes for subtitle editing (sort of like premiere) and I am using Hscroll and Picture box (I tried frame too) but seems to have maximum width number of 245745 which is not enough for a very long

any solution?

I am using this picture box to hold nearly 3000 average and maximum 10000 textbox with normal width of 150 pixel

Please advise me.

[HELP!!!] Datareport Maximum Width
hello guys,
i have many field to display in report, so i need the entire paper (letter) size, but i couldn't set my report's width to the value which all my fields would fit in, i had error when trying to execute it, about ,exceeding size.
I still got some inch in paper..., i want to use it. have tried some,
What I did :
1. set right and left margin to the lower value, e.g : 50
2. set orientation to landscape
3. use maximum allowed report's width.

Thank you for your help,

Edited by - ganero on 4/9/2008 10:45:26 PM

Form Maximum Width At Run Time
Hi All,

I have made a slide show which displays jpegs across multiple screens. I now need to display across up to 8 screens at different screen resolutions. It appears that 44580 (twips) is the maximum dimension that the form can be set to at run time. If I hard code a bigger number at design time it is fine, but any attempt to go bigger than 44580 at run time results in the form going back to 44580. I am trying to support up to 15 different screen resolutions and up to 8 screens, and I dont want to hard code the form width as there may be times when I want the slide show to be on screens 2 to 7. Has anyone else experienced this problem? I can't seem to find any threads from the search on max form width. As all ways any help is very much appreciated.

Setting Maximum Width To Flexgrid
I am making a small program in which i have data coming into a flexgrid from my database. My program uses a MDIform and all the other forms are MDIChild with window state as maximized. I want my flexgrid width to be equal to that of the form (maximized state). i tried doing grid.width = frm11.width but i found out that this width of the form refers to the size that i had set in design time and not to the maximized window screen. Any solutions on finding out how to maximize the grid width to match the window size???
Thanks in advance

MSFlexgrid - Set Column Width To Text Width..
How do I set the column width to be = to the amount of text

(the text length varies)

Is It Possible To The Form To Have Its Own Scrollbar And Exceed Its Maximum Width
    I am displaying a flowchart in a Form I maximized and the FlowChart picture width is greater than the VB Form width, in Access forms its having the Scrollbars to move and see the rest in the screen. Is is possible to give the Scrollbar in the Form in VB, is it possible to have a Form width greater than Maximized Property width. Kindly reply me.
Thank you,

Picture.width Maximum Of 337297 Too Small
I posted something a couple of weeks ago concerning a picture that did not have enough space, even with scrolling, for all of the controls that I had displayed in it. The maximum width of a picture seems to be 3.37297e5. Does anyone know if I can get around this? When I change this in my code by doing picture1.width = 340000, bad things happen.

Seems like a pretty small scrollable window if that's all that vb allows.


Changing The Column Width To The Longest Text In A Column With The MSFlexGrid Control
hi all how do i change the width of a column to fit the text inside that column i am using the MSFlexGrid to show my data, with an adodb conenction
thanks from

Find Maximum
If I have a txt file as follows:

DAY HR LAT LON 100 200 300 218 1 57.00 -12.00 0.00E+00 0.69E-16 0.91E-16
218 1 58.00 -12.00 0.18E-15 0.17E-14 0.61E-16
218 1 59.00 -12.00 0.52E-16 0.11E-14 0.00E+00
218 1 55.00 -11.00 0.00E+00 0.21E-14 0.00E+00
218 1 56.00 -11.00 0.00E+00 0.23E-14 0.79E-15
218 1 57.00 -11.00 0.28E-15 0.38E-14 0.15E-14
218 1 58.00 -11.00 0.30E-14 0.92E-14 0.57E-15
218 1 59.00 -11.00 0.10E-14 0.46E-14 0.19E-16
218 1 60.00 -11.00 0.43E-15 0.00E+00 0.00E+00

How can I find the maximum value of the numbers listed in the 5th column (headed by "100")?

Find The Maximum Value
I have a lot number, field1 until field6. Lot number is the primary key.I would like to find the record which field have maximum value (field1 until field 6).Let say I got the the field 5 has the maximum value in the the record.So what the lot number of that maximum value. How I can do that in SQL ? I use *.MDB database.Help me, I am a stupid student in programming

How To Find Maximum Value?
I want to find the maximum value of ID in a database using a loop for example

VB Code:
Do Until rec.EOF    If rec.fields("ID").Value = 'Max ID' Then         MsgBox "Max ID found"    Else         rec.MoveNext    End if Loop

How do I test if the ID value is maximum? Do I need to use some kind of sorting algorithm?

Find The Maximum Value In Excel
Hi all,

Could anyone provide me the code to find the first three maximum values from a spreadsheet? The values are listed in a sheet, and when the user clicks on a button, it will direct the user to another sheet whereby the top three maximum values from that column will be displayed on that sheet.


How To Find Maximum Value In A Colomn In A Spreadsheet
I have a spreadsheet in my VB program. I want to find out the maximum value in colomn A. How can I do so?

How To Find Maximum Value In A Colomn In A Spreadsheet
I have a spreadsheet in my VB program. I want to find out the maximum value in colomn A. How can I do so?

How Can I Find Maximum Values Depending On Different Data
I have data like the following
in columns A and B in Worksheet 1

5 00
6 00
1 01
3 01
2 01
4 20
1 20

The B column's data are text numbers (e.g 01, 0010, 10, 150 etc)
I have the following result in worksheet 2 unordered
(they are the unique values of previous column B)


I want a D column with the above data's max values next to each value on column C in worksheet 2.
The source data are in the column A in worksheet 1.
Here's the result

00 6
01 3
20 4

Im using this code , its fust and it is what i want....but
Private Sub CommandButton1_Click()
Dim RngB As Range
Dim RngC As Range
Dim Cll As Range

Set RngB = Range("B1", Range("B65536").End(xlUp))
Set RngC = Range("C1", Range("C65536").End(xlUp))

For Each Cll In RngC.Offset(, 1)
Cll.FormulaArray = "=MAX(IF(" & RngB.Address & "=" & Cll.Offset(, -1).Address & "," & RngB.Offset(, -1).Address & "))"
Cll.Value = Cll.Value
Next Cll
End Sub
But the problem is that i cant fix the code working with the two different worksheets because the A & B columns are in Worksheet 1, the column C is in worksheet 2 and so the new one D will be created in Worksheet 2

If anyone can help !!!!

Thanks in advance

Set Column Width HELP
Anyone know how to set a column width to a fix number. I've tried

app.range("A:A").width = 50

but I'm getting an error. I don't want to do an AutoFit. I actually want it fixed.

Thanks in advance!

Column Width
When I load an Access database into a can I get Visual Basic to pull up the format for each individual column width?
I'm sure Access uses a certain code to tell how much should each width be for each column.

If the above is not possible for Visual Basic...then maybe this is possible:
Go through each column and get the length of each cell...The one with the greatest length is set as an array(1-number of columns) Then format each column using a for/next statement.
The only problem and question for me is what is the right code to be able to do this?

I guess what would be good sample practice is a 2 column access file with two different lengths! And a visual-basic program that would load it into a flexgrid....but what is the code to format the width of each column? Any help on these two methods would be appreciated!!!

Kind regards,

Column Width

Can anybody tell me how can I define column width in MSFlexGrid table? For example, I would to set different widths each columns and, also different heights each rows.

Thank you forward,

Maximum Numbers Of Entries In ListBox
What is the maximum number of entries that I can add to a listbox? And what does VB do if I keep adding and exceed maximum number of entries?


Sorted Listbox Maximum Entries????
How many elements can a listbox (which has the sorted property = True) hold before there is a noticeable delay in sorting?

I am deciding whether I should use a sorted listbox to maintain the sort for me for 50000+ elements, but I am worried whether "adding" items into a listbox (which is already heavily populated) will cause noticeable delays due to the required sorting logic which will be performed.

** Of course, I will always set the Enabled and Visible properties to False to increase the speed.

Edited by - uncletr on 8/9/2004 7:56:08 AM

Change Last Column Width
I'm trying to get my last column in my MSHFlexGrid to fill the remaining space in the grid when the sum of colwidths are less than the grid.width.

For some reason I am getting a -1 value for the width when it falls out of the dispcol loop the first time and then 0 the next time. Both times, however, there are no changes to the grid's last column, even the the .ColWidth() value has changed...

Public Sub flxctlColPositions(ByRef myGrid As MSHFlexGrid, _
ByRef lstListBox As ListBox)
Dim dispcol As Integer
Dim flxcol As Integer
Dim lngSumWidths As Long
lngSumWidths = 0

With myGrid
'For each column to be displayed
For dispcol = 0 To lstListBox.ListCount - 1

'Look at each column of myGrid for match and reposition
For flxcol = 0 To .Cols - 1

If .TextMatrix(0, flxcol) = lstListBox.List(dispcol) Then

'Sum widths
lngSumWidths = lngSumWidths + .ColWidth(flxcol)

'Reposition column
.ColPosition(flxcol) = dispcol

End If

Next flxcol

Next dispcol

'Resize last column

MsgBox .ColWidth(lstListBox.ListCount - 1) 'Before change
.ColWidth(lstListBox.ListCount - 1) = .width - lngSumWidths
MsgBox .ColWidth(lstListBox.ListCount - 1) 'After change

End With
End Sub

Listview Column Width
Can someone show me how to set the column width in a Listview?

ListView1.View = lvwReport 'Set The Listview1 View so Columns/Headers can be viewed
ListView1.ColumnHeaders.Add , , "SCAC"
ListView1.ColumnHeaders.Add , , "Last Feed"

I can't get the above code to work when I add a width. The listview is blank when code is run.

I would appreciate any suggestions.


Values For The Column Width
I'm trying the FlyGuy function to resize row height with word wrap in a MSHFlexGrid using a label in the form. During the debugging in the row:

myLabel.Width = .ColWidth(lCol)
I get .ColWidth(lCol)= -1 so it yields a "invalid value for that property" error.

Does it happen when the field is empty?!

Column Static Width
I created a form with macros and a width automatic adjusting code to prevent the width of the column to be changed accidentely. After further testings with my form, I uncovered a few problems with the manner I use to specify the column width. First when I select a couple of cells horizontally, and write something on the first cell, all the other columns will be sized as the first one. To undo that, I must write manually something in each column. Also, when I run my principle macro that does a lot of maths, loopings, copying cells etc. It seems to be hard for the processor because it seems the calcul required is raising exponentially.

So is there a way to set off those columns width sizing during the principle macro runs and then set it on, and making it checks after that if all the columns width is ok? Or Maybe it would be est to transform that width change column into a macro attached to a button and I would simply click on the bottun to resize corectly all the cells?

To help I will upload my xls program. The form is in the sheet "Formulaire" and to run the macro, press the button "UE et Loyers" which is located at the upper left of the form and add the identification code 114555 when asked by an inputbox. I'll also quote at the end an example of the wight code I use.



Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Select Case Target.Column
Case 1, 3, 5 To 8
Target.ColumnWidth = 2
Case 2, 4
Target.ColumnWidth = 7
End Select
End Sub

Flexgrid Column Width
Hi guys ,i am from Turkey .I couldn't find enough help sites from Turkey.My vb is not good and unfortunately my English is not good either. i hope i can tell my problem . i can't set the column widths permanently .need your help..
and i want to sum all number in the flexgrid and show in a textbox or label
and every changes of values in the grid must be shown on the label at the same time.

DataGrid Column Width Value?
In this expression...

DataGrid.Columns.Item(iCol).Width = 1000

What type is '1000'?
Is it a Pixel, char...number.....etc....i cant figure it out......

the reason for this is I have a MSflex grid which has a column width of 20...i would like to know how to convert that number to the equiv in a datagrid..Please any help would be great. Thanks in advance

DataGrid Column Width Value
What is the Column Width Value Pixels, Spaces, Points? etc...

MSHFlexGrid Column Width
Is it possible to change the width of the columns in the grid? I don't see any property that allows it.

Datagrid Column Width ????
I have two datagrids that are exactly the same. The only difference is that I am using them on different forms, and one of them has a filter.

My question is this.
On the first datagrid, I could write column widths like this
AppointmentsDataGrid.Columns("time").Width = 40
AppointmentsDataGrid.Columns("patient").Width = 150
AppointmentsDataGrid.Columns("Doctor").Width = 150
AppointmentsDataGrid.Columns("status").Width = 50
and the output was as I expected.

but then, on the second grid, when I write the same thing, the column widths get rediculously small. Is there a way of specifying the units used for the column widths that I have missed?


Specifying The Column Width In A List Box
Is it possible to specify the individual column width in a list box (so that the columns have different widths to each other)?
If not, are there any tricks to getting around this?

<font color=blue> :: grin-r :: </font color=blue>

Flexgrid Column Width??
is there a way to set the column width? at runtime, i cant read the entire contents of some of my columns cause theyre too narrow


Setting Column Width
I am using the MSFlexGrid and want to set the first Col width to be smaller than Col 2's width.

Here is the code I wrote:
With MSFlexGrid1
.Row = 0
.Col = 0
.ColWidth = ?
End With

What is a valid value for the column width in twips? I used 400 and I get a "Compile error: Argument not optinal" I never used twips before so I am not sure if they are looking for a numeric value or not...

Any ideas to set the column width?


Column Width In DBGrid
Does anyone know how to set the column width in a DBGrid at design time? I've been able to set the default width with the properties box, but I need to customize it so that when I run an SQL query to fill the grid all of my columns will fit on the form without a scrollbar. For example, some columns are only 2 characters wide and yet all the column widths are too wide and wasting space on the form. The form is a status form and the user doesn't have time for resizing.

MSHFlexGrid Column Width
Hi. My problem concerns VB 6.0. I am using an MSHFlexGrid object to display data.

My problem is that the default column width does not fit the data. I have used the code shown at the bottom of this post to set the length to a multiple of the number of characters in the biggest box of each column, however, this doesn't give much luck as it's either too small or too big.

Can anyone offer me any advice/tips towards getting a best fit column width? Your help is much appreciated.

VB Code:
Dim fieldlen As IntegerDim maxlen As Integer For a = 0 To grdList.Cols - 1    grdList.Col = a    For b = 0 To grdList.Rows - 1        maxlen = 0        grdList.Row = b        fieldlen = Len(grdList.Text)        If fieldlen > maxlen Then maxlen = fieldlen    Next    grdList.ColWidth(a) = maxlen * 150Next

Excel Column Width
how to set the column width of a excel worksheet using vb

MSHFlexgrid Column Width?

How can I alter the column width in a MSHFlexgrid so that it completely fits the data in it, and remains like that permanently?

Also how can I center-allign the headers in a MSHFlexgrid?

Thank you.

Listview Column Width
How do you modify a listview's column width at run time?

Set Column Width In LISTVIEW

The Listview have serveral columns and it is in List report mode.

Try to do the following by add the column text and width, but no matter how I put in the width parameter, the column width is not change. any idea.

.ColumnHeaders.Add text:="Flag", width:=20
.ColumnHeaders.Add text:="Customer Name", width:=150
.ColumnHeaders.Add text:="Contact", width:=250


Listview Column Width
Hi all,

I want to emulate or get the functionality of when you double click between two columnheaders in a listview, how they resize the column to maximise the width according to the column data. There is no event built in as far as i've seen. Has anyone got any idea how I can do it?

Basically i'm loading data into the listview in report view when i form load and I want the columns to be maximised for the data on form load as well.

Thanks in advance.

Setting The Width Of A Column
I am exporting data from a recordset to an excel worksheet.I want the destination worksheet to have:
The width of the Column A set to 10
The width of the Column B set to 20
The width of the Column C set to 35
The width of the Column D set to 90

hOW can i do it?

Column Width In ListView!
I have a ListView with 2 columns. I want the width of the 1st column to be 3500 but even after specifying 3500 as the width of the 1st column in the ListView's Property Page (which comes up when Custom property is clicked in the Properties window) under the Column Headers tab, the width always remains the default i.e. the width refuses to change to 3500. How do I resolve this issue?

In fact, changing the width of the 1st column just doesn't make any difference; it always remains the same.

The view of the ListView is set to lvwReport & the ListView doesn't display the GridLines.

Copyright 2005-08, All rights reserved