Error with controls command in excel vba - excel

I created 2 forms. form1 contains 2 textbox, form2 contains 2 textbox, and 1 commandbutton.
this code does not work. how to write the right one?
Private Sub CommandButton1_Click()
Dim nom As String
nom = UserForm2.TextBox2
UserForm1.Controls("TextBox" & nom) = UserForm2.TextBox1
End Sub
UPDATE:
form1:
Private Sub textbox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
form2.show
form2.textbox2=1
End Sub
Private Sub textbox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
form2.show
form2.textbox2=2
End Sub
form2:
Private Sub commandbutton_click()
unload me
Dim nom As String
nom = UserForm2.TextBox2
UserForm1.Controls("TextBox" & nom) = UserForm2.TextBox1
End Sub

Not sure what are you trying to achieve but try with:
form1:
Private Sub textbox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
form2.show
form2.textbox2.Value = "1"
End Sub
Private Sub textbox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
form2.show
form2.textbox2.Value = "2"
End Sub
form2:
Private Sub commandbutton_click()
Dim nom As String
nom = me.TextBox2.Value
UserForm1.Controls("TextBox" & nom).Value = UserForm2.TextBox1.Value
unload me
End Sub

solved.
form1
Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Unload Me
UserForm2.TextBox2 = 1
UserForm2.Show
End Sub
Private Sub TextBox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Unload Me
UserForm2.TextBox2 = 2
UserForm2.Show
End Sub
Private Sub UserForm_Initialize()
TextBox1 = Cells(2, 2)
TextBox2 = Cells(3, 2)
End Sub
form2
Private Sub CommandButton1_Click()
On Error Resume Next
Dim nom As String
nom = TextBox2
UserForm1.Controls("textbox" & nom) = UserForm2.TextBox1
Cells(nom + 1, 2) = UserForm2.TextBox1
Unload Me
UserForm1.Show
End Sub

Related

How to show multiple images in VBA userform

I need to write a code in VBA and use a userform to show multiple images of the same item and rank them which one would be the best. I need the number, 1 being the best one to 3 being the last pick to be stored in a cell next to the image. I will put screenshots of my data as well as the userform I made. Below is the code I wrote but somehow not showing any images at all.
Thank you.
Dim ImageName As String
Dim CopyImage As String
Private Sub CheckBox1_Click()
End Sub
Private Sub CheckBox2_Click()
End Sub
Private Sub CheckBox3_Click()
End Sub
Private Sub CheckBox4_Click()
End Sub
Private Sub CheckBox5_Click()
End Sub
Private Sub CheckBox6_Click()
End Sub
Private Sub CheckBox7_Click()
End Sub
Private Sub CheckBox8_Click()
End Sub
Private Sub cmdNext_Click()
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then
MsgBox "Last Row"
ActiveCell.Offset(-1, 0).Select
Exit Sub
Else
Call GetImage
End If
End Sub
Private Sub cmdSave_Click()
ActiveCell.Offset(6349, 11).Select
If ActiveCell.Value = "" Then
MsgBox "Saved"
ActiveCell.Offset(6349, 11).Select
Exit Sub
Else
Call GetImage
End If
End Sub
Private Sub cmdLoad_Click()
If ActiveCell.Column <> 1 Or ActiveCell.Row = 1 Or ActiveCell.Value = "" Then
Cells(6349, 10).Select
End If
Call GetImage
cmdBack.Enabled = True
cmdNext.Enabled = True
End Sub
Private Sub cmdBack_Click()
ActiveCell.Offset(-1, 0).Select
If ActiveCell.Value = "" Then
MsgBox "Last Row"
ActiveCell.Offset(1, 0).Select
Exit Sub
Else
Call GetImage
End If
End Sub
Private Sub RankButton_Click()
End Sub
Private Sub DeleteButton_Click()
End Sub
Private Sub ClearButton_Click()
End Sub
Private Sub Image1_Click()
End Sub
Private Sub Image2_Click()
End Sub
Private Sub Image3_Click()
End Sub
Private Sub Image4_Click()
End Sub
Private Sub Image5_Click()
End Sub
Private Sub Image6_Click()
End Sub
Private Sub Image7_Click()
End Sub
Private Sub Image8_Click()
End Sub
Private Sub UserForm_Initialize()
cmdBack.Enabled = False
cmdNext.Enabled = False
'cmdSave.Enabled = False
End Sub
Private Sub GetImage()
Dim PhotoNames As String, fPath As String, iFile As String
Dim i As Integer
fPath = "D:\transfer\2021-12-13_image_ranking\dayco\2021-11-15_merged_dayco\"
Dim CurrentPartNumber As String
Dim CurrentImageIndex As Integer
CurrentImageIndex = 1
CurrentPartNumber = ActiveCell.Value
Sheets("IMAGE").Select
Dim CurrentCellValue As String
Dim CurrentRow As Long
CurrentRow = 6349
CurrentCellValue = Cells(CurrentRow, 10)
While Not (CurrentCellValue = "")
If CurrentCellValue = CurrentPartNumber Then
If Not Dir(fPath & Cells(CurrentRow, 10).Value) = "" Then
Me.Controls("Image" & CurrentImageIndex).Picture = LoadPicture(fPath & Cells(CurrentRow, 10).Value)
CurrentImageIndex = CurrentImageIndex + 1
End If
End If
CurrentRow = CurrentRow + 1
CurrentCellValue = Cells(CurrentRow, 1)
Wend
End Sub

Show sheet into which the value was entered when closing form

I have three buttons in userform S1,S2,S3(sheet1,sheet2..) which assign a value specified in the textbox to the first cell on a sheet. How to do that after closing user form show sheet
into which the value was entered/last used sheet
Private Sub Zamknij_Click()
UserForm1.Hide
Worksheets("SheetName").Activate
End Sub
Private Sub Sheet1_Click()
Sheets(1).Cells(1, 1).Value = TextBox1.Value
End Sub
Private Sub Sheet2_Click()
Sheets(2).Cells(1, 1).Value = TextBox1.Value
End Sub
Private Sub Sheet3_Click()
Sheets(3).Cells(1, 1).Value = TextBox1.Value
End Sub
Putting my comments into code: Here is one possible way to implement what I think it is you're trying to do.
Private Sub Zamknij_Click()
Unload Me
End Sub
Private Sub Sheet1_Click()
ApplyToSheet Worksheets(1)
End Sub
Private Sub Sheet2_Click()
ApplyToSheet Worksheets(2)
End Sub
Private Sub Sheet3_Click()
ApplyToSheet Worksheets(3)
End Sub
Sub ApplyToSheet(wsSheet As Worksheet)
With wsSheet
.Activate
.Cells(1, 1) = TextBox1.Value
End With
End Sub
The above will show the target and then write TextBox1.
If you need to keep another or the current sheet visible until Zamknij is clicked, here is another approach:
Option Explicit
'At the top of the userform (before any subs): Declare a module-level variable
Private mwsTarget As Worksheet
Private Sub Zamknij_Click()
mwsTarget.Activate
Unload Me
End Sub
Private Sub Sheet1_Click()
ApplyToSheet Worksheets(1)
End Sub
Private Sub Sheet2_Click()
ApplyToSheet Worksheets(2)
End Sub
Private Sub Sheet3_Click()
ApplyToSheet Worksheets(3)
End Sub
Sub ApplyToSheet(wsSheet As Worksheet)
Set mwsTarget = wsSheet
wsSheet.Cells(1, 1) = TextBox1.Value
End Sub

Multiple UserForms that trigger each other; UserForm_QueryClose not working as expected

I've got 5 UserForms and some of them activate depending on what the input of the first UserForm is.
The first UserForm code is below.
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then
WsName = "CAT"
Unload Me
End If
End Sub
Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then
WsName = "DOG"
Unload Me
End If
End Sub
Private Sub OptionButton3_Click()
If OptionButton3.Value = True Then
WsName = "CATDOG"
Unload Me
End If
End Sub
Private Sub OptionButton4_Click()
If OptionButton4.Value = True Then
WsName = "DOGCAT"
Unload Me
End If
End Sub
Private Sub UserForm_Initialize()
Me.StartUpPosition = 0
Me.Top = Application.Top + 250
Me.Left = Application.Left + Application.Width - Me.Width - 600
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
End
End Sub
When I press the Red "X" to end the entire module that calls the UserForm the module is exited and I am happy. When I press one of the options on the userform like OptionButton1.Value = True then the code also exits the module and I am sad. What am I doing wrong? I would like for the user to be able to press the Red "X" at any point in any UserForm to close out the Module and break out of the code.
The answer to this question was
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If
OptionButton1.Value = False And OptionButton2.Value = False OptionButton3.Value =
False And OptionButton4.Value = False Then
End
End If
End Sub

Selecting multiple sheet names in the list box and returning the results in msgbox

i tried various methods to select multiple sheets in the list box and returning the selected sheet names into the msgbox. can any one help doing this.
currently i am able to populate the sheet names in the list box.However i am not getting the all selected sheet names in the msgbox.
Public listChoice As String
Private Sub UserForm_Activate()
For n = 1 To ActiveWorkbook.Sheets.Count
With ListBox1
.AddItem ActiveWorkbook.Sheets(n).Name
End With
Next n
End Sub
Private Sub ListBox1_AfterUpdate()
listChoice = ListBox1.Text
End Sub
Private Sub CommandButton1_Click()
MsgBox (listChoice)
End Sub
Getting the selected item in a listbox isn't as straightforward as you'd want it to be:
Private Sub CommandButton1_Click()
Dim Msg As String
Dim i As Integer
Msg = "You selected" & vbNewLine
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
Msg = Msg & ListBox1.List(i) & vbNewLine
End If
Next i
MsgBox Msg
End Sub
credit: http://www.java2s.com/Code/VBA-Excel-Access-Word/Forms/GettheselecteditemsinaListBox.htm
You don't need the ListBox1_AfterUpdate() Sub or the public listChoice variable with this code
This msgbox the selected items in listbox.
Private Sub CommandButton1_Click()
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
Msg = ListBox1.List(i) & vbNewLine
End If
Next i
MsgBox Msg
End Sub
Something like this may assist,
Private strOP As String
Private dicSelections As Scripting.Dictionary
Private Sub ListBox1_Mouseup(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim strSheet As String
strSheet = Me.ListBox1.List(Me.ListBox1.ListIndex)
If Me.ListBox1.Selected(Me.ListBox1.ListIndex) Then
If Not dicSelections.Exists(strSheet) Then
dicSelections.Add strSheet, strSheet
Else
End If
Else
If dicSelections.Exists(strSheet) Then
dicSelections.Remove strSheet
End If
End If
End Sub
Private Sub UserForm_Click()
Me.ListBox1.AddItem "one"
Me.ListBox1.AddItem "two"
Me.ListBox1.AddItem "three"
End Sub
Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Worksheets selected " & Join(dicSelections.Items(), " & ")
End Sub
Private Sub UserForm_Initialize()
Set dicSelections = New Scripting.Dictionary
End Sub

only userform should be visible and not the excel

Please advise as to how can we show only userform and not excel behind it.
I used application.visible = false but it is hiding all the other excel.
I used activatewindow.visible = false but userform is not retrieving the data from excel.
I used activatewindow.displayworkbooktabs=false but it is not hiding the workbook.
Try this code.
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show vbModeless
End Sub
Don't forget to make Application visible before close
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Visible = True
End Sub
Write this to userform:
Private Sub CommandButton1_Click()
Dim wCount As Long
Dim i As Long
wCount = Windows.Count
For i = 1 To wCount
Windows(i).Visible = True
Next i
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim wCount As Long
Dim i As Long
wCount = Windows.Count
For i = wCount To 1 Step -1
Windows(i).Visible = False
Next i
End Sub

Resources