I am trying to organize data in two columns in a listbox on a userform. Something like the following:
This works as far as it goes, but the vbTab just grabs whatever tab stop is available next, and as a result the List2 items fail to align in a neat column.
In all my searching online I have found only one basic solution, which is based on VB6, not VBA. The solution is simply to clear the existing tab stops in the listbox and set a new one. The problem is that this doesn't seem to work in VBA.
This produces a "Compile Error: Method or data member not found," where ".hwnd" on the first SendMessage line is highlighted.
I'm a little out of my depth here, and so I am stuck. Usually this type of API call works fine in VBA -- so is the problem that VBA does not recognize that a listbox has a window handle? That some reference needs to be set? Or something else entirely?
And if this just can't be made to work within VBA, is there another way to get to the goal -- a neat, two-column display in the listbox?
Code:
List1 = Array("Height", "Weight", "Depth")
List2 = Array(10, 20, 30)
For i = 0 to UBound(List1)
MyForm.ListBox1.AddItem List1(i) & vbTab & List2(i)
Next
In all my searching online I have found only one basic solution, which is based on VB6, not VBA. The solution is simply to clear the existing tab stops in the listbox and set a new one. The problem is that this doesn't seem to work in VBA.
Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LB_SETTABSTOPS = &H192
TabLength = 80 'Tab length in pixels
SendMessage MyForm.ListBox1.hwnd, LB_SETTABSTOPS, 0&, 0&
SendMessage MyForm.ListBox1.hwnd, LB_SETTABSTOPS, 1&, TabLength
I'm a little out of my depth here, and so I am stuck. Usually this type of API call works fine in VBA -- so is the problem that VBA does not recognize that a listbox has a window handle? That some reference needs to be set? Or something else entirely?
And if this just can't be made to work within VBA, is there another way to get to the goal -- a neat, two-column display in the listbox?