I have a userform, within the userform there is a frame with 4 textbox, how would I pass the values of those 4 textbox into one cell? separated by comma or space.
I tried doing the following within my submit button.
Dim t As MSForms.Control
For Each t In Me.Frame1.Controls
If TypeOf t Is MSForms.TextBox Then
If IsEmpty(stCode1Box) Then
Exit For
End If
If stCode1Box Is Nothing Then
'Cells(emptyRow, 15).Value = stCode1Box.Value
ElseIf Not IsEmpty(stCode1Box) Then
Cells(emptyRow, 15).Value = stCode1Box.Value
ElseIf stCode2Box Is Nothing Then
'Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value
ElseIf Not IsEmpty(stCode2Box) Then
Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value
ElseIf stCode3Box Is Nothing Then
'Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value & ", " & stCode3Box.Value
ElseIf Not IsEmpty(stCode3Box) Then
Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value & ", " & stCode3Box.Value
ElseIf stCode4Box Is Nothing Then
'Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value & ", " & stCode3Box.Value & ", " & stCode4Box.Value
ElseIf Not IsEmpty(stCode4Box) Then
Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value & ", " & stCode3Box.Value & ", " & stCode4Box.Value
End If
End If
Next t
The result would pop up on that cell, and if more than one textbox had value, this would be separated by a ", " Comma.
Untested:
Dim t As MSForms.Control, v
v = ""
For Each t In Me.Frame1.Controls
If TypeOf t Is MSForms.TextBox Then
v = v & iif(v <> "", "," , "") & Trim(t.Value)
End If
Next t
Cells(emptyRow, 15).Value = v
Try simply
Cells(emptyRow, 15).Value = Cells(emptyRow, 15).Value & "," & stCode1Box.Value
You could loop your controls like you already have but rather than having a series of if...elseif statements you could check if the texbox.value is not "", add the value to an array, then join the array separated by whatever you like.
See my example below which writes your values into cell C5 on Sheet1, assuming your userform has a commandbutton named cmdSubmit (this will work for any number of textboxes):
Example
Private Sub cmdSubmit_Click()
Dim temp As Variant
Dim c As Control
Dim myCount As Long
'0 based array starting with a single element
ReDim temp(0 To 0)
myCount = 0
'Check each control for a textbox
For Each c In Me.Frame1.Controls
If TypeOf c Is MSForms.TextBox Then
'if there is a value in the texbox then assign it to an array
If c.Value <> "" Then
temp(myCount) = c.Value
myCount = myCount + 1
'set upperbound of the array +1 when a new value is found
ReDim Preserve temp(0 To UBound(temp) + 1)
End If
End If
Next c
myCount = 0
'Remove the last array element as it must be blank
ReDim Preserve temp(0 To UBound(temp) - 1)
'Create a string of each value joined with a comma and space
Dim myString As String
myString = Join(temp, ", ")
ThisWorkbook.Sheets(1).Range("C5").Value = myString
End Sub
References
Array Function
Join Function
Related
I would like to somehow get the value from the different controls on the user form and then write them on the sheet after that if the user form is closed down and re opened if a name is selected in the combobox then load all data in the form back ready to change values. I have 13 rows that a user can use on the user form.
In my code the writing the data to the sheet will write all item selected i want but it takes too long because all of the loops and ifs. Is there a better way to achieve what i want?
Private Sub FillingInForm()
Dim i As Long
Dim WS As Worksheet
Dim ctl As MSForms.Control
Dim lbl As MSForms.Label
Dim cmb As MSForms.ComboBox
Dim txtbox As MSForms.TextBox
Dim optbtn As MSForms.OptionButton
Set WS = ActiveSheet
With WS
For i = 1 To ItemsListFrame.Controls.Count
For Each ctl In ItemsListFrame.Controls
If TypeName(ctl) = "Label" Then
If ctl.Tag = "GroupItem" & i Then
Set lbl = ctl
If Controls("Item" & i).Value <> vbNullString Then
.Range("A" & i + 6).Offset(0, 0).Value = Me.OrderNo.Value
.Range("A" & i + 6).Offset(0, 1).Value = Me.NextCollectionDate.Text
.Range("A" & i + 6).Offset(0, 1).Value = Format(.Range("A" & i + 6).Offset(0, 1).Value, "dd/mm/yyyy")
.Range("A" & i + 6).Offset(0, 8).Value = Me.DateReturnBy.Value
.Range("A" & i + 6).Offset(0, 8).Value = Format(.Range("A" & i + 6).Offset(0, 8).Value, "dd/mm/yyyy")
Controls("OrderLbl" & i).Enabled = True
End If
End If
ElseIf TypeName(ctl) = "ComboBox" Then
If ctl.Tag = "GroupItem" & i Then
Set cmb = ctl
If Controls("Item" & i).Value <> vbNullString Then
Controls("Item" & i).Enabled = True
End If
If Controls("Item" & i).Value <> vbNullString Then
.Range("A" & i + 6).Offset(0, 2).Value = Controls("Item" & i).Text
End If
End If
ElseIf TypeName(ctl) = "TextBox" Then
If ctl.Tag = "GroupItem" & i Then
Set txtbox = ctl
If Controls("Item" & i).Value <> vbNullString Then
.Range("A" & i + 6).Offset(0, 3).Value = Controls("Qty" & i).Value
.Range("A" & i + 6).Offset(0, 4).Value = Controls("UnitPrice" & i).Value
.Range("A" & i + 6).Offset(0, 5).Value = Controls("SubTotal" & i).Value
.Range("A" & i + 6).Offset(0, 7).Value = Controls("Comments" & i).Value
Controls("Qty" & i).Enabled = True
Controls("UnitPrice" & i).Enabled = True
Controls("SubTotal" & i).Enabled = True
Controls("Comments" & i).Enabled = True
End If
End If
ElseIf TypeName(ctl) = "OptionButton" Then
If ctl.Tag = "GroupItem" & i Or ctl.Tag = "InOut" & i Then
Set optbtn = ctl
If Controls("Item" & i).Value <> vbNullString Then
.Range("A" & i + 6).Offset(0, 6).Value = Controls("OptionOut" & i).Value
Controls("OptionIn" & i).Enabled = True
Controls("OptionOut" & i).Enabled = True
End If
End If
End If
Next ctl
Next i
End With
End Sub
I'm trying to compare 2 cells and if its true that row will be deleted, i tried using msgbox to return the value and it shows its true, but row is not deleting.
The first cell is derived using formula in 1 sheet and the other is just numbers, does that make a difference?
Dim r, s, i, t As Long
Dim com, cc, bl, acc As Long
Dim rDB, rInput As Range
Dim shDB, shInput As Worksheet
Set shDB = ActiveWorkbook.Sheets("Database")
Set rDB = shDB.Range("A1", "T1000")
Set shInput = ActiveWorkbook.Sheets("Data Input")
Set rInput = shInput.Range("A1", "R1000")
r = 2
Do While Len(shDB.Cells(r, 1).Formula) > 0
com = shInput.Cells(7, 5).Value
cc = shInput.Cells(5, 5).Value
bl = shInput.Cells(9, 5).Value
acc = shInput.Cells(5, 10).Value
MsgBox (com & " " & shDB.Cells(r, 1).Value & " " & cc & " " & rDB.Cells(r, 2).Value & " " & rDB.Cells(r, 3).Value & " " & bl & " " & rDB.Cells(r, 4).Value & " " & acc)
If shDB.Cells(r, 1).Value = com And rDB.Cells(r, 2).Value = cc And rDB.Cells(r, 3).Value = bl And rDB.Cells(r, 4).Value = acc Then
shDB.Rows(r).EntireRow.Delete
MsgBox ("deleting rows")
Else
r = r + 1
End If
Loop
When deleting alway go from the last index to the first. This applies to listboxes, comboboxes, ranges, ...etc.
If you delete from first to last then you will skip every other row
I have 2 Excel sheets, I need to take 1 value in Sheet 1, look for it in Sheet 2. If I find it, then I need to make sure that some other values are matching. If yes, I copy the sheet 1 row in a "match" tab.
If not, I copy the row in "mismatch" tab and I need to insert a message that says which value didn't match.
I cannot make it work right now. I think I'm not exiting the loop in the right place. Here is my code. If anybody could help, I would appreciate.
Sub compareAndCopy()
Dim LastRowISINGB As Integer
Dim LastRowISINNR As Integer
Dim lastRowM As Integer
Dim lastRowN As Integer
Dim foundTrue As Boolean
Dim ErrorMsg As String
' stop screen from updating to speed things up
Application.ScreenUpdating = False
'Find the last row for column F and Column B from Sheet 1 and Sheet 2
LastRowISINGB = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, "f").End(xlUp).row
LastRowISINNR = Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, "b").End(xlUp).row
'fIND THE LAST ROW OF MATCH AND MISMATCH TAB
lastRowM = Sheets("mismatch").Cells(Sheets("mismatch").Rows.Count, "f").End(xlUp).row + 1
lastRowN = Sheets("match").Cells(Sheets("match").Rows.Count, "f").End(xlUp).row + 1
'ISIN MATCH FIRST
For I = 2 To LastRowISINGB
For J = LastRowISINNR To 2 Step -1
If Sheets("Sheet1").Cells(I, 6).Value = Sheets("Sheet2").Cells(J, 2).Value And _
Worksheets("Sheet1").Range("B" & I).Value = "Y" And _
Worksheets("Sheet2").Range("Z" & J).Value = "" And _
(Worksheets("Sheet1").Range("c" & I).Value = Worksheets("Sheet2").Range("AF" & J).Value Or _
Worksheets("Sheet1").Range("K" & I).Value = Worksheets("Sheet2").Range("K" & J).Value Or _
Worksheets("Sheet1").Range("N" & I).Value = Worksheets("Sheet2").Range("L" & J).Value) Then
Sheets("Sheet1").Rows(I).Copy Destination:=Sheets("match").Rows(lastRowN)
lastRowN = lastRowN + 1
Exit For
ElseIf Sheets("Sheet1").Cells(I, 6).Value = Sheets("Sheet2").Cells(J, 2).Value And _
Worksheets("Sheet1").Range("B" & I).Value = "Y" And _
Worksheets("Sheet2").Range("Z" & J).Value = "" And _
Worksheets("Sheet1").Range("c" & I).Value <> Worksheets("Sheet2").Range("AF" & J).Value And _
Worksheets("Sheet1").Range("K" & I).Value <> Worksheets("Sheet2").Range("K" & J).Value And _
Worksheets("Sheet1").Range("N" & I).Value <> Worksheets("Sheet2").Range("L" & J).Value Then
ErrorMsg = "dates don't match"
ElseIf Sheets("Sheet1").Cells(I, 6).Value = Sheets("Sheet2").Cells(J, 2).Value And _
Worksheets("Sheet1").Range("B" & I).Value <> "Y" Then
ErrorMsg = "B column don't match"
ElseIf Sheets("Sheet1").Cells(I, 6).Value = Sheets("Sheet2").Cells(J, 2).Value And _
Worksheets("Sheet1").Range("B" & I).Value = "Y" And _
Worksheets("Sheet2").Range("Z" & J).Value <> "" Then
ErrorMsg = "Z column don't match"
Else: ErrorMsg = "ISIN don't match"
End If
Next J
Sheets("Sheet1").Rows(I).Copy Destination:=Sheets("mismatch").Rows(lastRowM)
Worksheets("mismatch").Range("S" & lastRowM).Value = ErrorMsg
lastRowM = lastRowM + 1
Next I
' stop screen from updating to speed things up
Application.ScreenUpdating = True
End Sub
First, I think you should add "Exit For" for each clause in If..else method. Otherwise it will lead to the fact that almost of your "miss match" result will be "ISIN don't match".
Second, I think you should set ErrorMsg = "" before For J = LastRowISINNR To 2 Step -1, and have condition ErrorMsg <> "" when you input result in sheet miss match.
Sheets("Sheet1").Rows(I).Copy Destination:=Sheets("mismatch").Rows(lastRowM)
Worksheets("mismatch").Range("S" & lastRowM).Value = ErrorMsg
lastRowM = lastRowM + 1
Otherwise, all your row even match or missmatch will input into miss match sheet.
I am exporting data from one workbook to another workbook to T13:Tlastrow
This data, from column F in my workbook where I run this macro, I want to be put into {nyckel="TEXT HERE";} in column T in the "new" workbook, starting from row 13 (T13).
I am stuck here. So would really appreciate some help/solution. Thanks!
Sub CopyData()
Dim wkbCurrent As Workbook, wkbNew As Workbook
Set wkbCurrent = ActiveWorkbook
Dim valg, c, LastCell As Range
Set valg = Selection
Dim wkbPath, wkbFileName, lastrow As String
Dim LastRowInput As Long
Dim lrow, rwCount, lastrow2, LastRowInput2 As Long
Application.ScreenUpdating = False
' If nothing is selected in column A
If Selection.Columns(1).Column = 1 Then
wkbPath = ActiveWorkbook.Path & "\"
wkbFileName = Dir(wkbPath & "CIF LISTEN.xlsm")
Set wkbNew = Workbooks.Open(wkbPath & "CIF LISTEN.xlsm")
'Application.Run ("'C:\Users\niclas.madsen\Desktop\TEST\CIF LISTEN.xlsm'!DelLastRowData")
LastRowInput = Cells(Rows.count, "A").End(xlDown).Row
For Each c In valg.Cells
lrow = wkbNew.Worksheets(1).Range("B1").Offset(wkbNew.Worksheets(1).Rows.count - 1, 0).End(xlUp).Row + 1
lastrow2 = Range("A" & Rows.count).End(xlUp).Row
lastrow3 = Range("T" & Rows.count).End(xlUp).Row
wkbCurrent.ActiveSheet.Range("E" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("A" & lrow)
wkbCurrent.ActiveSheet.Range("A" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("B" & lrow)
wkbCurrent.ActiveSheet.Range("F" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("T" & lrow)
' Standard inputs
wkbNew.Worksheets(1).Range("D13:D" & lastrow2).Value = "Ange referens och period"
wkbNew.Worksheets(1).Range("E13:E" & lastrow2).Value = "99999002"
wkbNew.Worksheets(1).Range("G13:G" & lastrow2).Value = "EA"
wkbNew.Worksheets(1).Range("H13:H" & lastrow2).Value = "2"
wkbNew.Worksheets(1).Range("M13:M" & lastrow2).Value = "SEK"
wkbNew.Worksheets(1).Range("N13:N" & lastrow2).Value = "sv_SE"
wkbNew.Worksheets(1).Range("P13:P" & lastrow2).Value = "TRUE"
wkbNew.Worksheets(1).Range("Q13:Q" & lastrow2).Value = "TRUE"
wkbNew.Worksheets(1).Range("S13:S" & lastrow2).Value = "Catalog_extensions"
'wkbNew.Worksheets(1).Range("T" & lastrow3).Value = "{Nyckelord=" & wkbNew.Worksheets(1).Range("T" & lastrow3).Value & ";}"
Next
' Trying to get this to work
LastRowInput2 = wkbNew.Worksheets(1).Range("T" & wkbNew.Sheets("Sheet1").UsedRange.Rows.count + 1).End(xlUp).Row
For i = 0 To LastRowInput2 - 13
wkbNew.Worksheets(1).Range("T" & 13 + i).Value = "{Nyckelord=" & wkbNew.Worksheets(1).Range("T" & 13 + i).Value & ";}"
Next i
' END HERE
' wkbNew.Close False
' Find the number of rows that is copied over
wkbCurrent.ActiveSheet.Activate
areaCount = Selection.Areas.count
If areaCount <= 1 Then
MsgBox "The selection contains " & Selection.Rows.count & " suppliers."
' Write it in A10 in CIF LISTEN
wkbNew.Worksheets(1).Range("A10").Value = "COMMENTS: " & Selection.Rows.count & " Suppliers Added"
Else
i = 1
For Each A In Selection.Areas
'MsgBox "Area " & I & " of the selection contains " & _
a.Rows.count & " rows."
i = i + 1
rwCount = rwCount + A.Rows.count
Next A
MsgBox "The selection contains " & rwCount & " suppliers."
' Write it in A10 in CIF LISTEN
wkbNew.Worksheets(1).Range("A10").Value = "COMMENTS: " & rwCount & " Suppliers Added"
End If
wkbNew.Worksheets(1).Activate
Application.ScreenUpdating = True
Else
MsgBox "Please select cell(s) in column A", vbCritical, "Error"
Exit Sub
End If
End Sub
OK Try
wkbNew.Worksheets(1).Range("T" & lrow).Value = "{Nyckelord=" & wkbCurrent.ActiveSheet.Range("F" & c.Row).Value & "}"
Instead of your line:
wkbCurrent.ActiveSheet.Range("F" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("T" & lrow)
And remove the whole block marked 'Trying to get this to work
If the code is doing the right action but on the wrong cells, then the problem is in the start and end of the For loop. Your For Loop is going from row '13 + i' where i = 0 (so row 13), to row 13 + LastRowInput2 - 13 (so LastRowInput2). This seems right to me, so the problem must be with the value in LastRowInput2.
You need to correct this line:
LastRowInput2 = wkbNew.Worksheets(1).Range("T" & wkbNew.Sheets("Sheet1").UsedRange.Rows.count + 1).End(xlUp).Row
So that is gives you the correct last row input in your data. There are several approaches to finding the end of data depending on whether there may be blank cells in the middle and other factors. This may be one option:
LastRowInput2 = wkbNew.Worksheets(1).Range("T65000").End(xlUp).Row
Be sure to step through the code and verify that LastRowInput2 is set to the value you expect and then this should work.
I'm trying to type a search term and look into a entire specific range and every time this term has a match the information will be stored in another column.
When I use "Do", "With" or "While" it just stores one result.
Sub MethodFindAllSamples()
Dim rng1 As Range
Dim strSearch As String
index = 11
strSearch = InputBox("Type the model you are looking for, please: ")
Set rng1 = Range("G:G").Find(strSearch, , xlValues, xlPart, xlByRows, False)
If Not rng1 Is Nothing Then
Application.Goto rng1
Model = ActiveCell(1.1)
Content = ActiveCell(1, 4)
FIssues = Range("ER" & ActiveCell.Row + 1).Value
TIssues = Range("ER" & ActiveCell.Row + 1).Value
MsgBox "Model selected: " & Model & vbNewLine & "CS: " & Content & vbNewLine & " Issues found: " & FIssues
Errors = Left(FIssues, 1)
Errors2 = Mid(TIssues, 22, 1)
Cells(index, 1).Value = Mid(Model, 4, 6)
Cells(index, 3).Value = Errors
Cells(index, 4).Value = Errors2
Cells(index, 2).Value = strSearch + Left(Content, 8)
Else
MsgBox strSearch & " This device can't be found, please try again"
End If
End Sub
this an example how you can achieve this
Sub MethodFindAllSamples()
Dim oCell As Range, i&, z&, strSearch$
strSearch = InputBox("Type the model you are looking for, please: ")
i = Cells(Rows.Count, "G").End(xlUp).Row
z = 0
If strSearch <> "" Then
For Each oCell In Range("G1:G" & i)
If Replace(Trim(UCase(oCell.Value)), " ", "") Like "*" & Replace(Trim(UCase(strSearch)), " ", "") & "*" Then
z = z + 1
End If
Next
If z > 0 Then
MsgBox "Range [D] contain: " & z & " iteration of the selected model : " & strSearch
Else
MsgBox "Range [D] does not contain: " & strSearch
End If
Else
MsgBox "Search model not specified!"
End If
End Sub