Extracting data values from a textbox within a frame? - excel

In Excel I'm attempting to extract the data from the textbox that I've dynamically created within the frame. As seen in the code below, the textbox in which the names are gogo1, gogo2 and gogo3 respectively have been created. I would like the information entered into these textboxes to be pasted onto sheet1 when the okay button is pressed. However, I cannot seem to pull any data that was entered into these textboxes and sheet1 remains blank after the okay button is clicked.
Private Sub showCOL_Click()
Dim number As Integer
Dim gogo(1 To 3) As String
For number = 1 To 3
Set first = UserForm2.Frame1.Controls.Add("forms.textbox.1")
With first
.Name = "gogo" & number
.Width = 30
.Height = 20
.Left = 36
.Top = 20 * number
End With
Next number
End Sub`
Private Sub ColnProceed_Click()
If UserForm2.Frame1.Name = "gogo2" Then
MsgBox gogo2.Value
End If

It looks like you are setting up three text boxes, but then checking the name of the frame that contains them in your click handling function. I believe you want to check if the name of the TextBox is "gogo2", not the Frame that groups them.
For now, if you just want to test to see what the values are in the 2nd text box, use:
MsgBox (Me.Controls("gogo2").Value)

Related

Excel VBA UserForm filter using CheckBoxes and past the matching cells to a new sheet

I'm a complete beginner to VBA and I'm currently trying to do a UserForm for filtering lines containing checkboxes, linked to each cell, giving TRUE/FALSE values.
I want to have checkboxes on the UserForm and keyword search as well.
The data origin is a table on a separate sheet. Columns C-T are the filter criterias, which are set when putting data in the table.
Column B is the actual content, it's raw text and it's what I want to get filtered.
Column A is just for the current number of each line.
On the UserForm I'll have checkboxes with the same names as the columns C-T and when I check some of them and click the filter button, I want Excel to paste the text from B of each matching line to a second spreadsheet.
I also want to search with keywords as optional addition - but this is nothing important.
I hope it's understandable.
I already have some code for creating the captions of the checkboxes in the userform automatically based on the column names and the reset button for clearing the userform is working, too.
The code for captions:
Sub UserForm_Initialize()
CheckBox1.Caption = Sheets("Datenbank").Range("C1").Value
CheckBox2.Caption = Sheets("Datenbank").Range("D1").Value
CheckBox3.Caption = Sheets("Datenbank").Range("E1").Value
CheckBox4.Caption = Sheets("Datenbank").Range("F1").Value
CheckBox5.Caption = Sheets("Datenbank").Range("G1").Value
CheckBox6.Caption = Sheets("Datenbank").Range("H1").Value
CheckBox7.Caption = Sheets("Datenbank").Range("I1").Value
CheckBox8.Caption = Sheets("Datenbank").Range("J1").Value
CheckBox9.Caption = Sheets("Datenbank").Range("K1").Value
CheckBox10.Caption = Sheets("Datenbank").Range("L1").Value
CheckBox11.Caption = Sheets("Datenbank").Range("M1").Value
CheckBox12.Caption = Sheets("Datenbank").Range("N1").Value
CheckBox13.Caption = Sheets("Datenbank").Range("O1").Value
CheckBox14.Caption = Sheets("Datenbank").Range("P1").Value
CheckBox15.Caption = Sheets("Datenbank").Range("Q1").Value
CheckBox16.Caption = Sheets("Datenbank").Range("R1").Value
CheckBox17.Caption = Sheets("Datenbank").Range("S1").Value
CheckBox18.Caption = Sheets("Datenbank").Range("T1").Value
End Sub
The code for the reset button:
Sub Reset()
Dim c As Control
For Each c In UserForm1.Controls
If TypeName(c) = "TextBox" Then
c.Value = ""
End If
Next c
Dim d As Control
For Each d In UserForm1.Controls
If TypeName(d) = "CheckBox" Then
d.Value = "False"
End If
Next d
End Sub
Is anyone here able to help?
goto developer tab and you must see record macro whis smail red squre and behind vba node
1- click on it
2- name it
3- do as you do to normall applay filter on each column it that sheet
4- stop macro recoding
5 - press on macro you will see the macro you just recording
now select it and press edit you will see the vba code for filtring
hop this work for you
sheref adel

userform multicolumn listbox not showing records unless form is closed and re-opened

I have 4 user input forms where user inputs/edits records via fields on the form and the records display below in a multi-column listbox. (There are also Save and Delete buttons on the forms.) They have all worked fine for weeks of unit testing. Now that I have coded and debugged the calculations part of the project, I have started a final round of system testing where I am starting with blank data sheets. And the listboxes are showing a strange (new) behavior.
When I open a form with no records, enter the first record and save it, the record is not displayed. The record does get saved properly on the data sheets. The first column of the listbox displays the record number and the rest of the columns are blank. However, I am not including (displaying) the record number (column A) on the forms. I only display values from columns 2 the 7 (B thru H) of the data sheet. It will only show the record number for every record I enter thereafter, as long as I am in the same "session" of the open form when I entered the first record.
However, if I close the form and re-open it, the records displays as they should. And any subsequent records I enter after I re-open the form, the new records also display properly. Even if I delete all the records on the form and ad new ones, they display fine as long as I have not closed the form. This behavior is the same for all 4 of the forms. The forms all worked properly when I created them, and for literally a couple of months of unit testing after that. Admittedly, I have made a lot of code changes to correct errors I have found in the calculations part of my project. But I cannot conceive of any that would globally affect listboxes on all the forms.
Example code for one of the forms Initialize event:
If Not iAcctLastRow = 1 Then 'there are no records to load into listbox
Set rAcctDB = wsAcctInput.Range("A2:J" & iAcctLastRow)
With Me.lstAcct 'Populate listbox
.Font.Name = "Tahoma"
.Font.Size = 10
.ColumnCount = 10
.ColumnWidths = "0,120,80,90,60,60,80,60,0,0"
.List = rAcctDB.Cells.Value
End With
End If
And in the Save procedure I refresh the listbox as follows:
iAcctLastRow = wsAcctInput.Range("B22").End(xlUp).row
DelayThis 50 'added this delay but no change
Set rAcctDB = wsAcctInput.Range("A2:J" & iAcctLastRow)
Me.lstAcct.List = rAcctDB.Cells.Value 'Refresh listbox
The above code is the same on all 4 forms, varying in which data sheet and columns being used. I can repeat the above behaviors at will. Has anyone seen this before, or have any ideas?
You can format the listbox on initialize even if there are no records to show.
Private Sub UserForm_Initialize()
Dim wsAcctInput As Worksheet, iAcctLastRow As Long
Set wsAcctInput = Sheet1
iAcctLastRow = wsAcctInput.Range("B22").End(xlUp).Row
With Me.lstAcct 'Populate listbox
.Font.Name = "Tahoma"
.Font.Size = 10
.ColumnCount = 10
.ColumnWidths = "0,120,80,90,60,60,80,60,0,0"
If iAcctLastRow > 1 Then
.List = wsAcctInput.Range("A2:J" & iAcctLastRow).Value
End If
End With
End Sub
I spent all day on this. I cannot answer why this changed, but I can explain how I fixed it. I noticed that the listbox not only was showing just the row number, but it was only loading that single column as if there was only supposed to be 1 column. The listbox object has ColumnCount and ColumnWidths that can be set in the Object Browser. My ColumnCount has always been set to 1 and ColumnWidths being blank, because I set them in code. So I changed the Object Browser ColumnCount to 10 and pasted my widths into ColumnWidths. And it worked. I set them back to the defaults of 1 and blank, respectively. I saw the earlier failure to load the listbox columns.
The fix was to force the column count and widths again with each Save via code just as I do in the userform initialization, instead of the simple range refresh.
With Me.lstAcct 'Populate listbox
.Font.Name = "Tahoma"
.Font.Size = 10
.ColumnCount = 10
.ColumnWidths = "0,120,80,90,60,60,80,60,0,0"
.List = rAcctDB.Cells.Value
End With
I have a backup of this file from 3 months ago with the code working as it is shown in the original post, and the column counts and widths set to their default values - which is why I had not tried this fix earlier. I cannot explain what changed in the code execution. Nor can I explain why it only worked properly if there was at least one record in the listbox.

How do I prevent inserted images from clustering?

I have two sheets and one userform.
Sheet2 starts off completely empty. Sheet1 is completely empty except for one ActiveX command button that opens the userform when clicked.
The userform has one single label, that when clicked, will run some vba code that (1) inserts an image at a specified location in Sheet2 and then (2) inserts a row in Sheet2 above the image
My goal is to click on the userform's label X number of times such that I will end up with X number of images in sheet2 each separated one row apart from each other (my actual use case involves different images).
The problem I am facing is that when I first open the excel file (with sheet1 as the default sheet that appears upon opening) and start using my ActiveX command button as described, then when I navigate over to sheet2 to take a look, the images in sheet2 are all clustered together on top of each other.
Strangely enough, after I navigate over to sheet2 to take a look, then navigate back to sheet1 to continue using the ActiveX command button, then navigate back to sheet2 again to take another look, the subsequent images are now all neatly spaced out as intended.
Is there a way to make sure that right from the start, the images don't cluster when I first open my excel file and use the ActiveX command button?
Code contained in sheet1
Private Sub CommandButton1_Click()
UserForm1.Show
End Sub
Code contained in the userform (substitute your own pic filepath)
Private Sub Label1_Click()
Dim pic
Set pic = Sheet2.Shapes.AddPicture(Environ("USERPROFILE") & "\" _
& "Pictures\Account Pictures\monkey selfie.jpg", _
False, True, 1, 1, -1, -1)
pic.LockAspectRatio = msoTrue
pic.Placement = xlMoveAndSize
If pic.Height / pic.Width <= 14.3 / 47 Then
pic.Width = 40
ElseIf pic.Height / pic.Width > 14.3 / 47 Then
pic.Height = 10
End If
With pic
.Top = 20
.Left = 20
End With 'this position corresponds to cell A2
Sheet2.Range("A2") = "Filled" 'to indicate that the cell _
is supposed to contain an image
Sheet2.Rows(2).Insert Shift:=xlShiftDown 'both the image and _
the word "Filled" should be pushed down one row
End Sub
Thank you!

How do I fill Excel sheet cells with only the filled text boxes without any empty cells?

I am new to VBA and recently I have been developing a calculator to help do my work. I have created a form with multiple check boxes and text boxes, in my Excel sheet, I have a table that should be filled by the user form. However, I've run into issues, in the user form there is a check box that when checked the user must enter into the text box, I have got exactly eight similar categories (check boxes and text boxes), so all checked boxes and all filled text boxes should be displayed into the cells in the sheet, however they way I done it, is that I use the line:
Range("E5").Value = "Project Manager"
For each of the eight categories, it must fill the cells without leaving any empty cells.
For example, user checks 5 of the 8 boxes and accordingly fills out the text box for each one, if they skipped one of the boxes, it must not do the same when printing them out on the cells.
Private Sub CommandButton1_Click()
Dim PMdays As Integer
If PMcheckBx.Value Then
Range("E5").Value = "Project Manager"
PMdays = PMtextBx.Value
ActiveCell.Offset(0, 1) = PMdays
End If
End Sub
As such, I have attached an image of how I want it to look like and how mine looks like now to help you understand my problem further.
How my sheet looks like
How it should look
You can do something like this:
Private Sub CommandButton1_Click()
Dim PMdays As Integer, c As Range
Set c = ActiveSheet.Range("E5").Value '<< start list here
If PMcheckBx.Value Then
c.Resize(1,2).Value = Array("Project Manager", PMtextBx.Value)
Set c = c.Offset(1, 0) 'move down one row
End If
'next 7 checkboxes
End Sub
Note no ActiveCell/Select/Activate needed.

Is There A Fast Way To Add Multiple Text boxes With Scrollbars?

Because the cells in my project contain so much data I have had to insert textboxes that have scrollbars to see all the data (they are linked to the cell which sit behind them on the spreadsheet). Is there any fast way to do the same thing on a column of 1000 records or will I have to go through manually and link the textbox to the specific cell? Is there a faster way?
Also If an issue comes in that is a reply to the original issue I need it to use the original ID (I have used auto IDS, which can be seen in the spreadsheet). Any recommendations?
Slowly I am getting better at excel and VBA but I need a hand sometimes ^_^
I have attached the spreadsheet which contains an example of 2 records I made. The final sheet will have 1000 records. (Please download the spreadsheet and open in excel)
LINK To Spreadsheet
A few things:
You should change the cell formatting to "Top Align" the text in the cells. This will cause the cell to show the first line of the long text in the Query cells.
Instead of using the "send email" text in a cell why not add a single button to email the currently selected row. (use insert on the ribbon in the developer tab (you have to change the excel options to show the developer tab).
The code to send an email might be better if it updated a new column with the date it was sent, and in the event that it has already been sent, it could prompt the user to confirm.
if not isempty(cells(r, ColNumberWithSentdate) ) then
if vbno = msgbox ("Are you sure you want to send the email again?", VbYesno) then
Exit sub
end if
end if
All the textboxes you have added are really slowing down the spreadsheet.
why not just have one tall row at the top above the table with the filters. The tall row would show the data from the currently selected row in the table. Your table rows could then probably be less high.
Add a single text box.
Use ALT+click and drag to resize text boxes to fit cell exactly.
Change or view the name of the textbox in the named range area to "TextBoxQuery".
Add code to change the text in the summary row
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Say the tall row is in row 2
If Target.Row <= 2 Then
Exit Sub
End If
Dim i As Integer
For i = 1 To 8
Cells(2, i) = Cells(Target.Row, i)
Next i
End Sub
You could even allow the user to edit the text in the tall row and add a button to save the changes they entered:
A. Add an ACTIVEX button in the summary row labelled "SAVE"
(Then you can edit the vba in the sheets module for the button)
B. Add a cell somewhere that records which row is being displayed in the summary row.
C. When the save button is clicked, write code that copies all the values in row to back to the row recorded.
NOTE that if the user deletes a row in the table or sorts the data in the table the row stored will be wrong. So before copying the data, you might like to check to see whether the row has moved. ie check a KEY value (ie ones that never changes) is the saem in both rows.
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To 8
Cells(Cells(1, 1).Value, i) = Cells(2, i)
Next i
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Say the tall row is in row 2
If Target.Row <= 2 Then
Exit Sub
End If
' Cell A1 is used to store which row is displayed
Cells(1, 1) = Target.Row
Dim i As Integer
For i = 1 To 8
Cells(2, i) = Cells(Target.Row, i)
Next i
End Sub

Resources