Excel VBA Userform Global Counter - vba

I have created a VBA UserForm whose purpose is to allow the user to update records held in that Workbook. On Initialization, the UserForm populates with certain cell values in various text boxes, the user is then asked to add certain information in other text boxes, and on clicking the 'Next' button, the new information is added to the data table and the next entry populates the UserForm.
I am currently achieving this by keeping a count of the current row (currentRow) and once the 'Next' button is clicked, currentRow = currentRow + 1. This works fine.
My problem now is that I need to show a different UserForm if the value of a specific cell is different, for example:
If Range(Cells(currentRow, 3).Address).Value = "Planning" Then
PlanningUserForm.Show
Else
... rest of code ...
This works, but I am struggling to maintain the counter once this new PlanningUserForm is initialized. I need it to know the current value of currentRow so that it can display the correct cell values in its text boxes.
I have declared the variable currentRow as Public, but it doesn't seem to carry over the value into the new userform. Is this possible? Or am I asking too much of Excel?
Many thanks
Matt

If you have declared currentRow as public, then in PlanningUserForm you can access the currentRow value as UserForm1.currentRow where UserForm1 is the userform where you have declared currentRow as Public

You can always use the registry. It can be used to many other things too in the future.
SaveSetting "Your macro", "Values", "CurrentRow", currentRow
Now the currentRow is saved in your registry.
Now in your PlanningUserForm:
currentRow = GetSetting("Your macro", "Values", "CurrentRow")
and you got the saved value from your registry.
I have bad experiences with global variables, that is why I suggest registry.

Related

Conditionally Presetting Checkboxes on VBA Userform

I need a Userform to call another Userform that fills text boxes and checkboxes that reads from approx 10 cells from workbook. Basically it is recalling properties of a product that the user can edit, some are text based and some are yes/no (true/false). How do I connect those Userform properties?
You can use Userforms like a class, and declare and instantiate them with New UserFormName. However, they're also a class that always has an instance in the workbook, that you can treat as though it were already declared with New. If you want to pull live information that is already inside it, you'll want to use that one.
For example, if the userform is called "Bacon", and contains some methods that do what you want(Solve_Everything_Click), and some internal data that has the information you need declared at the top(Public MyName As String, TheAnswer As Boolean), you can call it from another sub/function, in userform or out, as long as the code is in the same workbook, or references the book with the userform.
You can access the global instance of it, in the same workbook, by using its name, and accessing the things inside of it with ., keep an eye on the context help that comes up when you press ., which will tell you other methods and variables you might pull from it.
Sub PokeAtIt()
Bacon.TheAnswer = True
MsgBox Bacon.MyName & " Has the answer: " & Bacon.TheAnswer
Bacon.Solve_Everything
End Sub

Excel ActiveX ListBox Shrinks with each update

I have a set of linked subs which work like this:
A user types into an ActiveX TextBox
A Change Event in that TextBox calls a sub in a Module
That Module sub drives updating a named range in a sheet
The range value drives updating a table of Excel cells that uses lookup functions based on the range value
The table values are copied and pasted to a another range (to eliminate links to formulas)
That pasted range is put into a ListBox using this (props to Rory for his patience):
ActiveSheet.ListBox1.List = Sheets("Search Criteria Control").Range("G1:G21").Value
The result is that for every character the user types in the TextBox the ListBox is updated.
The problem I have is that the ListBox shrinks a bit with every keystroke in the TextBox referred to in #1 above. Is this normal behavior and I'm misusing ListBoxes, am I doing something wrong or do I need to respecify the dimensions of the ListBox every time it is updated with something like this?
ActiveSheet.OLEObjects("ListBox1").Top = 35
ActiveSheet.OLEObjects("ListBox1").Left = 650
ActiveSheet.OLEObjects("ListBox1").Width = 550
ActiveSheet.OLEObjects("ListBox1").Height = 610
Thanks in advance for any thoughts on this.
I was having trouble with the same thing. My ActiveX listbox would move around on the sheet and change size for no reason that I could see.
While I did go ahead and develop some code to reset size and coordinates, that wasn't satisfactory since there had to be a mechanism to trigger that code - something I didn't want to burden end-users with.
I found a better answer in another user forum. There's a Listbox Property called IntegralHeight whose default property is True - something to do with screen resolution and optimal display of listbox contents. Simply set that to False. I did that with some ActiveX boxes that were giving me fits, and I was able to disable the "adjustment" code and, so far, so good!

How to reset Excel internal button count?

I have an an Excel sheet that uses VBA to generate some form control buttons on the fly.
the buttons are cleared and then new buttons are created.
I have noticed that even though old buttons are deleted Excel is keeping an internal register of each button. New buttons have button name of over 11K
I don't know if there is some sort of limit excel will allow for this and I don't want to run out of buttons.
I am not sure if this growing registry of buttons past is causing the file size to grow.
I would like to be able to reset the increment back to 0
Anyone have any idea how I can go back to button_0 ? (without starting a whole new Excel sheet)
Seems internal button count is sheet specific. Solution is to copy sheet, rename old sheet, then rename new sheet to old sheet name. Then delete old sheet. Viola! button count reset.
I found the answer somewhere in a forum, but I couldn't retrace it.
You could also pragmatically create the button by a function/sub-routine as a workaround.
Example: below function adds a button and limits the count to a fixed number (in my case, it is the total buttons available).
In my sheet, the first button is named "Button 687" (before I use the macro) and the second button is named "Button 2".
But, it is quite not dynamic when you want to add Drop Down or other form control etc. Macro recording helps you figure out the syntax, methods and properties of the form control you want to add though.
I am not sure why "buttons" is not listed in Properties/Methods after you typed "Activesheet." but Activesheet.Buttons(1).Name or Activesheet.buttons.add are valid codes.
Public Sub Add_Button(ButtonLabel$, ButtonSize#, BFontSize#, BFontName$)
Dim Button__ As Object
Dim One_Unit#: Per_Unit = Application.CentimetersToPoints(1)
'Creates a button at the sheet's first cell (row 1, col 1) with the height and width being 1cm.
Set Button__ = ActiveSheet.Buttons.Add(1, 1, One_Unit, One_Unit)
'button count will be restricted to the number set by user.
With Button__
.Name = "Button " & ActiveSheet.Buttons.Count
With .Characters
.Text = UCase(BLabel)
.Font.Name = BFontName
.Font.Size = BFontSize
End With
End With
End Sub
Sub AddAButton()
Add_Button BLabel:="SAVE"
Debug.Print ActiveSheet.Buttons(ActiveSheet.Buttons.Count).Name
End Sub

VBA Excel comboBox dropdown is empty after obj.addItem

I have an Excel2010 VBA userform that has one comboBox, from which the user should be able to select a currently-open Excel Workbook. The USERFORM_Initialize subroutine contains (among other things) :
cbWorkbook.Clear
cbWorkbook.Visible = True
For Each wb In Workbooks
cbWorkbook.AddItem wb.name
Next wb
I have set a breakpoint at this code, and am able to step through it; in the present situation there are four open workbooks, and the "for each" is iterated four times, as appropriate. And I can see that wb.name contains the values that I want.
However, when the form displays and the dropbox arrow is clicked, the "list" is empty. It looks like there is room for one item, and that item is blank. (I believe this is typical of an empty dropdown box.)
Select attributes for the combobox are:
Autosize=False; AutoTab=false; Enabled=True; DropButtonStyle=1-fmDropButtonStyleArrow;
Height=18; ListRows=8; ListStyle=0; Locked=False; ShowOptionWhen=2; SpecialEffect=2; Style=0; Visible=True. At the time of execution, cbWorkbook.listCount = 4
This is in development, and it did appear to work as expected yesterday, but now seems to never work. Any ideas where I might be going wrong?
EDIT: I found the solution to this: I had inadvertantly duplicated another combo box over the top of cbWorksheet, effectively hiding it. The control I was seeing was empty, while the control I wanted was overlaid. Deletion of the rogue control box solved the issues.
My apologies; this should have been the first thing I sought.
I found the solution to this: I had inadvertantly duplicated another combo box over the top of cbWorksheet, effectively hiding it. The control I was seeing was empty, while the control I wanted was overlaid. Deletion of the rogue control box solved the issues.
My apologies; this should have been the first thing I sought.
With ComboBox1
.AddItem "This"
.AddItem "Is"
.AddItem "A"
.AddItem "Test"
End With
or if you want to fill it with Range data:
ActiveSheet.Shapes("ComboBox1").Select
Selection.ListFillRange = "k1:k10"
PS - submit your file for review; it should be easier to look at!

Modeless MsgBox, Error trapping, .Find

I have a subroutine that searches for an occurrence of a string in another workbook. I'm trying to get an error message to pop up if the string can't be found (it's most likely due to spelling mistakes), as vbModeless, and allows to user to click on the cell in the searched sheet with the correct value. Then I'd like to resume the search with the new value.
I'm at the moment stuck on making my simple MsgBox to be modeless.
Can anyone help? So far I have (simplified):
With ...
On Error GoTo UserSelect
celladdress = .Range("a1:bb100").Find("searchstring").Address
And my error label:
UserSelect:
MsgBox("Select the cell with the correct spelling") vbModeless
newstring = ActiveCell.Value
searchstring = newstring
Resume
I think it's the Modeless MsgBox giving me grief.
I don't believe that you can use vbModeless with msgbox. That is for use with the Show method of a user form.
What you probably need to do is create a user form that has a refedit control and a button on it. They can then pick a cell with the refedit control. When the user clicks on the button set a public variable on the form with the cell reference the selected.
Then you you need to use ".Show vbModal" on the user form and read off the cell they selected from the form public variable.
Edit:
Actually, you shouldn't need the public variable as the refedit control should be a public property of the form anyway.
I'm not 100% sure on the requirements here. Given a search string of dgo and a worksheet with cells containing bird, cat and dog. Do you want the user to:
(a) edit the cell containing dog and change it to say dgo instead
This would use the modal form and RefEdit control outlined by andynormancx. Like a MsgBox, the modal form pauses the macro until the form is closed
(b) allow the user to click on the cell containg dog and then re-run the search with dog as the search term
This is more complicated. I think that you would need to look at events here. This is fine if your subroutine is pretty much standalone but if it is part of a larger program then this could require substantial rewriting