CodeModle.Insertlines drops variables - excel

After several days of searching Excel vba forums I have finally given in and must now ask for help.
The problem is that CodeModle.Insertlines causes excel to drop all variables. I have a couple arrays that I need to use / check boxes and list boxes etc. all get wiped when I run the code below
With Application.Workbooks(ActiveWorkbook.Name).VBProject.VBComponents("UserSavedQueries").CodeModule
.InsertLines 2, "tmp = " & """" & "THIS IS SOME SAMPLE TEXT " & """"
End With
I fully admit that the Insertlines was a bit of a work around as a way to an end, but that way now seems blocked. Please can someone advise how to insert lines into a module without resetting all variable?
For the bigger picture (and for someone to advise how I probably should have tackled this in the first place)
I have a sheet that pulls together a bunch of parameters from list boxes, checkboxes cells etc when a user selects the ‘Go’ button.
the selections are formatted to make a JSON message that is sent to an API and the response is displayed in a table on a second sheet.
To give some context, Imagine Listbox1 called “search criteria” with selections of “Manufacturer, Colours, Countries”. Listbox2 called “Filters” changes context depending on the selection on Listbox1. So we could have for Listbox2: “Ford, BMW, Nissan” , “Red, Blue, Green, Black, White”, “ ”. Not the actual data but it shows the varying length of each selection. There are actually around 12 available selections some of which have 100+ filter options.
The user may select “Ford, Red” which would return red Fords in all countries or “Ford,BMW,Green,Scotland” which would just Green Fords and BMWs in Scotland.
All of this works nicely.
NOW: I need to add the ability to have “Saved User Selections”. I need to avoid dumping these selections in a worksheet (even hidden) as it allows the user to “fiddle” with the shape and format of the selection which will break the JSON parsing. So I decided to save the user selections in a VB module. A UserForm provides the ability to Name the saved selection, date-stamp it and runs the code above. Parsing the UserSavedQueries module returns a listbox of all saved selections, which, when double-clicked are intended to populate listbox1 & 2 ready for the user to hit ‘Go’.
Considered options: I haven’t ruled out saving to an external file, or maybe even registry, but would prefer to keep self-contained within the one file.
This article came closest to helping, but I'm not sure how it would work for my use-case

Related

Value of the cell moves to the left after editing in the userform

I have been facing a strange issue lately. So, the user has the ability to change value of the cell from the userform window, however, after apply changes somehow the values go from the right to the left (only ones with decimals) and that creates a problem when the data is read by another software (Matlab) it can not be read. I have tried to reformat it as number or general to see what happens but nothing. The only thing that changes it back to the right is (find & Replace "," with ","). Anyone knows why this happens? Any solutions ? (see picture for demonstration)
Maybe look in the code for the user form. As I understand it the edit box treats everything as a string. So when the number goes initially from the sheet to the form it is converted into a string. Hence when it is written back, the sheet thinks it’s a string. In the code underlying the form you need to intervene and turn the string back to a number (eg using CDbl() ). If you enter a value (or change) a number directly on the sheet, Excel will try and be ‘helpful’ and convert things that look like numbers into numbers (it does the same with dates). – Solution by #DS_London

How to hide text in certain cells from a script?

Windows 10, Google Sheets, LVL: Rookie.
So I have a sheet, that is kinda like a scoreboard, the main purpose of it is to do scoreboard things, but that's irrelevant. Basically, for new people that use the sheet, I have green sentences near cells to show them what goes there, kinda like tutorials.
But, for people that have used the sheet for a while, they are going to get tired of all this extra space used up by green sentences that don't actually teach them anything cause they already know it.
SO I wanted to create a button from a UI box, that disables tutorials (Just hiding/deleting these green sentences), and whenever they wanted to see the tutorials "sentences" again, they could use another UI Button to enable them, which unhides/adds the text back in the cells.
I already have the UI Box setup...
The only thing I don't know how to do or what to code, is hiding/deleting these sentences, and being able to unhide/paste the sentences.
Now, if this can be done in an easy formula, please let me know, but otherwise, please help me write out a script for this. I will credit you if you wish.
You have a complicated spreadsheet that included about 25 text "hints" to user. You want experienced users to have the ability to "hide" the hints if they wish.
You could do this with a checkbox.
Untick (the default): the comments appear.
Tick the box: the comments are hidden
Create a checkbox (say, it is in Cell G1),
Edit all your hints along these lines.
=if($G$1=TRUE,"","Team name goes here --->")
Explanation
$G$1=TRUE`: this means that the check box has been ticked
if TRUE, then display nothing: ""
if not TRUE (the checkbox is NOT ticked), then display the hint. Such as "Team name goes here --->"

Generate comboboxes depending on number inserted in first combobox

I'm doing an userform where in one of the fields (lets say its combobox1) the user is asked for the number of failures, and for each failure he has to put the type of failure, number of part and in another field the type of rework (3 fields to fill for each failure). And what I would like to know if that if it's possible to generate comboboxes depending on the number of failures made. For example: if the user puts 3 in the field of failures, 9 comboboxes should appear and so on.
I also think that maybe another way would be to have the standards 3 comboboxes and clear the fields each time the user press a button and a counter appears indicating which failure description he is filling, but I would also like to know how this could integrate into the whole userform, which already has a "submit" button.
Maybe there could be another reasoning for this, and I would greatly appreciate any help.
I don't really have much experience using dynamically created tabstrips, but they may be helpful with creating the layout you're describing. I would recommended in the base userform, include a tabstrip with a single tab and the 3 comboboxes/textboxes that are needed for a single failure. (You could keep it invisible until after "combobox1" has a value, if desired) Then use the number from the combobox to dynamically create the additional tabs on the tabstrip:
Private Sub ComboBox_Change()
'To remove previous tabs if combobox changed
For i = 1 To TabStrip1.Tabs.Count - 1
TabStrip1.Tabs.Remove(i)
Next i
'Adding additional tabs to tabstrip
For i = 1 To ComboBox.Value - 1
TabStrip1.Tabs.Add
Next i
End Sub
The tab currently selected can be identified by using the TabStrip1.Value property. I think the change event is queued by the TabStrip1.Value changing.
You will have to manually clear out any values when the tab strip changes (or fill back in values they previously filled out but want to view again).
However, since you'd be determining which tab you're on by "index" (the .value property) you could create a 2d array (redim it to correct rows/columns based off combobox1's value) and use the tab "index" to store combobox values and retrieve existing values for when the user switches back to an existing tab.
And maybe consider "locking" the combobox1 so the value can't be changed by accident. This could be accomplished by having a variable initialized to, maybe -1, which gets set to the combobox value after they've started adding infor to the incidents. Or don't remove any tabstrip tabs, only add if the number is greater than the existing count. (If you want them to put in whatever value they'd like, maybe consider using a textbox?)

To make entire row mandatory to fill if one cell of that row is filled

For ms Excel 2007.
I want the user to fill all cells in a row IF any one cell of that row is filled.
I am using ms-Excel 2007.
The only way I can think of to do this is by using a form. The form would have to be the only way that the user can enter data (you protect the worksheet and the form code "unprotects" it), then when the user fills out the form you check the fields for errors (not just omissions, but things like "that is not a valid date", etc).
To see what I mean, you can see a form at work for data entry in this tutorial. Maybe that's all you need, although there's not really solid data validation built in. But it will get you started.
Once you see how that works, you can try to design your own form that replicates the functionality of this one, but that includes error trapping. This involves opening the VBA editor (<Alt-F11>), inserting a user form, adding fields and buttons, and writing some code that is run when the "OK" button is clicked.
Not saying this is an easy thing to implement (I am guessing from the question that you may be new at this), but give it a shot. When you get stuck - post an update to your question or leave a comment.

form view in excel for a record

I have a sheet in excel that is basically like a database (yes it has to be in excel unfortunately) and I want to design a simple form to display the records (the different column values in the rows). Usually MS is good with making widgets to do this but I cant seem to figure out how to write it in VBA (easily)
Basically I have a list of Names and ID#'s then some other columns that need to be filled in (address, order # etc) but I'd like the user to do this via a form rather than typing in excel. I created a small form in VBA, but now I cant link the columns to the textboxes in the form.
Anyone know of any code I can plug and play to do this.
Specifically I am using a ComboBox (for the list of names) and then a for loop searching through it to find the selection and updating data. But its a real pain.
plus I cant figure out how to put in a 'Next' button, which is really annoying me. Does anyone know how to change the comboBox value to the next row, if it is linked via RowSource
Have you considered the Form feature? You can find under the commands you can add to your QuickAccess toolbar.
Check this helpful tutorial for additional details.

Resources