The following code searches column A(sorted) for an item# and each time it finds it, the corresponding B, C & D column are entered into 3 listboxes. I would like to use a 3-column listbox. Any help?
Private Sub cmdSearch_Click()
Dim Response As Long
Dim NotFound As Integer
Dim arr As Variant
Dim i As Long
Dim str1 As String, str2 As String, str3 As String
NotFound = 0
ActiveWorkbook.Sheets("Items").Activate
Response = Val("0" & Replace(txtItemNumber.Text, "-", ""))
If Response <> False Then
With ActiveSheet
arr = .Range("A2:D" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With
For i = 1 To UBound(arr)
If arr(i, 1) = Response Then
str1 = IIf(str1 = "", arr(i, 2), str1 & "|" & arr(i, 2))
str2 = IIf(str2 = "", arr(i, 3), str2 & "|" & arr(i, 3))
str3 = IIf(str3 = "", arr(i, 4), str3 & "|" & arr(i, 4))
End If
Next
If str1 = "" Then
MsgBox "Item Number Not Found!", vbExclamation
NotFound = 1
Else
Frame1.Visible = True
ListBox1.List = Split(str1, "|")
ListBox2.List = Split(str2, "|")
ListBox3.List = Split(str3, "|")
End If
End If
End Sub
Thanks for any help...
This should do it:
Change:
If str1 = "" Then
MsgBox "Item Number Not Found!", vbExclamation
NotFound = 1
Else
Frame1.Visible = True
ListBox1.List = Split(str1, "|")
ListBox2.List = Split(str2, "|")
ListBox3.List = Split(str3, "|")
End If
to:
If str1 = "" Then
MsgBox "Item Number Not Found!", vbExclamation
NotFound = 1
Else
Frame1.Visible = True
ListBox1.Clear 'to avoid errors
ListBox1.ColumnCount = 3
For i = 0 To UBound(Split(str1, "|"))
ListBox1.AddItem Split(str1, "|")(i)
ListBox1.List(i, 1) = Split(str2, "|")(i)
ListBox1.List(i, 2) = Split(str3, "|")(i)
Next
End If
hint: you may change ColumnWidths
however... to leave you some work, i suggest to merge it with your arr-part...
just using my solution would be a waste :D
Related
the macro below takes two cell values (from first and second column)
and displays the column and there cell content in a Pop up Form
Im trying to add the condition that only the column and cell value is displayed if the cell contains value.
something like that =IF(A1<>"",result,"")
but I dont know how to implement that for all cells not only for a specific one.
Option Explicit
Const rangeForSearch = "G2"
Const rowTitles = 4
Dim arrTmp
Dim lastRow As Long, lastColumn As Long
Dim textForSearch As String, textForSearch_withoutSpaces As String
Dim strTmp As String
Dim i As Long, j As Long
Sub searchPerson()
Application.ScreenUpdating = False
With ActiveSheet
textForSearch = .Range(rangeForSearch)
If textForSearch = "" Then
MsgBox "Input text in cell """ & rangeForSearch & """ and try again!", vbCritical
Application.ScreenUpdating = True
Exit Sub
End If
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
lastColumn = .Cells(rowTitles, .Columns.Count).End(xlToLeft).Column
If lastRow <= rowTitles Or lastColumn <= 2 Then
MsgBox "Dataset is wrong! Check it and try again!", vbCritical
Application.ScreenUpdating = True
Exit Sub
End If
arrTmp = .Range(.Cells(rowTitles, "A"), .Cells(lastRow, lastColumn))
End With
'---------------------------------------
textForSearch_withoutSpaces = Replace(textForSearch, " ", "")
For i = LBound(arrTmp, 1) + 1 To UBound(arrTmp, 1)
strTmp = Replace(arrTmp(i, 1) & arrTmp(i, 2), " ", "")
If StrComp(textForSearch_withoutSpaces, strTmp, vbTextCompare) = 0 Then Exit For
Next i
If i = UBound(arrTmp, 1) + 1 Then
strTmp = textForSearch & vbCrLf & vbCrLf & "No dataset!"
Else
strTmp = textForSearch
For j = 3 To lastColumn
strTmp = strTmp & vbCrLf & vbCrLf & arrTmp(1, j) & ": " & arrTmp(i, j)
Next j
End If
Application.ScreenUpdating = True
MsgBox strTmp, , "Result"
End Sub
maybe
For j = 3 To lastColumn
If Not IsEmpty(arrTmp(i, j)) Then strTmp = strTmp & vbCrLf & vbCrLf & arrTmp(1, j) & ": " & arrTmp(i, j)
Next j
I have a complicated split I need to do in VBA excel. I want to split each piece that starts "C:", includes "OCAK" and ends ".JPG" in range("C1") into A1,A2,A3... when click the button.
FROM THIS
TO THIS
I'm still doing research and testing, but I can't find a real viable solution. Any ideas would be greatly appreciated.
Private Sub buton_Click()
If Cells(1, "c").Text Like "C:*OCAK*.jpg*" Then
Dim jpgStart As Long
jpgStart = InStr(Cells(1, "c").Text, ".jpg")
Dim result As String
result = Left(Cells(1, "c").Text, jpgStart - 1)
Cells(1, "c").Offset(0, -2).Value = result
Else
Cells(1, "c").Offset(0, -2).Value = vbNullString
End If
End Sub
The problem is in the splitting actually. In the input, the new line should be used as a delimiter as well. Thus, consider changing the input a bit to something like this:
readCell = Worksheets(1).Cells(1, "C")
readCell = Replace(readCell, Chr(13) & Chr(10), " ")
readCell = Replace(readCell, vbCrLf, " ")
readCell = Replace(readCell, vbNewLine, " ")
readCell = Replace(readCell, vbLf, " ")
Once the input is fixed an array can be built of the units - myArray = Split(readCell). Looping through the array and using Like "C:*OCAK*.jpg" works quite well:
Public Sub TestMe()
Dim readCell As String
readCell = Worksheets(1).Cells(1, "C")
readCell = Replace(readCell, Chr(13) & Chr(10), " ")
readCell = Replace(readCell, vbCrLf, " ")
readCell = Replace(readCell, vbNewLine, " ")
readCell = Replace(readCell, vbLf, " ")
Dim myArray As Variant
myArray = Split(readCell)
Dim myVar As Variant
Dim currentRow As Long: currentRow = 1
For Each myVar In myArray
If myVar Like "C:*OCAK*.jpg" Then
Worksheets(1).Cells(currentRow, "A") = myVar
currentRow = currentRow + 1
End If
Next
End Sub
In your button macro code loop the cells in column c; I have to assume you know how to set that up and do it. Then for each cell in that range:
with thisworkbook.worksheets("theNameOfYourSheet")
dim loopRange As Range
set loopRange=.Range(.Cells(1,3),.Cells(.UsedRange.Rows.Count,3))
end with
dim cell as Range
for each cell in loopRange
If cell.text Like "C:*.jpg*" Then
Dim jpgStart As Long
jpgStart = Instr(cell.text,".jpg")
Dim result As String
result= Left(cell.text,jpgStart-1)
cell.offset(0,-1).Value=result
Else
cell.offset(0,-1).Value = vbNullString
End If
Next
Split by vbLf
Split by a blank space character
Test against your result
Code:
Option Explicit
Sub GetOcak()
Dim arr As Variant
arr = Split(Cells(1, 3).Value, vbLf)
Dim i As Long
Dim j As Long
j = 1
For i = 0 To UBound(arr)
If Left(Split(arr(i), " ")(0), 7) = "C:\OCAK" And _
Right(Split(arr(i), " ")(0), 4) = ".jpg" Then
Cells(j, 1).Value = Split(arr(i), " ")(0)
j = j + 1
End If
Next i
End Sub
I have an excel function as follows-
IF(B3="","",IF(AND(G3="NA",F3="Qualified"),"New to Qualified",
IF(AND(G3="NA",F3<>"Qualified"),CONCATENATE("New to Qualified and ",F3),
IF(AND(G3<>"NA",F3="Qualified"),IF(H3<>G3,"TCV Change","Same"),
IF(AND(G3<>"NA",F3<>"Qualified"),IF(H3="NA","TCV Change",IF(H3<>G3,CONCATENATE("TCV Change and ",F3),F3)))))))
I am trying to replicate the same in VBA as-
Sub CC()
Dim str1 As String
Dim str2 As String
str1 = "New to Qualified and" & Range("F3:F100")
str2 = "TCV Change and" & Range("F3:F100")
Range("J3:J100").Value = IIf(Application.WorksheetFunction.And(Range("G3:G100") = "NA", Range("F3:F100") = "Qualified"), "New to Qualified", _
IIf(Application.WorksheetFunction.And(Range("G3:G100") = "NA", Range("F3:F100") <> "Qualified"), str1, _
IIf(Application.WorksheetFunction.And(Range("G3:G100") <> "NA", Range("F3:F100") = "Qualified"), IIf(Range("H3:H100") <> Range("G3:G100"), "TCV Change", "Same"), _
IIf(Application.WorksheetFunction.And(Range("G3:G100") <> "NA", Range("F3:F100") <> "Qualified"), IIf(Range("H3:H100") = "NA", "TCV Change", IIf(Range("H3:H100") <> Range("G3:G100"), str2, Range("F3:F100")))))))
End Sub
However I'm getting an error on the last line- "Compile Error- Argument not optional"
Please help with the same.
This code might look more complicated at first but this is a "more VBA way" to do it. Doing this in VBA you will need a loop, since you can't just copy down the cells like you can in the worksheet. I usually avoid using WorksheetFunction. The following code will put all your values into an array and produce an array arrJ filled with the results. You do not need to approach this with arrays, but I would recommend using the If - ElseIf - Else structure I provided below, since it makes the code much easier to read, understand and modify.
Sub CC()
Dim str1 As String, str2 As String
str1 = "New to Qualified and "
str2 = "TCV Change and "
Dim arrB As Variant, arrF As Variant, arrG As Variant, arrH As Variant 'rename these to describe your data instead of columns
arrB = ActiveSheet.Range("B3:B100").Value2
arrF = ActiveSheet.Range("F3:F100").Value2
arrG = ActiveSheet.Range("G3:G100").Value2
arrH = ActiveSheet.Range("H3:H100").Value2
Dim rngJ As Range
Set rngJ = ActiveSheet.Range("J3:J100")
Dim strResult As String
Dim arrJ As Variant
ReDim arrJ(LBound(arrB) To UBound(arrB), 1 To 1)
For i = LBound(arrB) To UBound(arrB)
If arrB(i, 1) = vbNullString Then
strResult = vbNullString
Else
If arrG(i, 1) = "NA" And arrF(i, 1) = "Qualified" Then
strResult = "New to Qualified"
ElseIf arrG(i, 1) = "NA" And arrF(i, 1) <> "Qualified" Then
strResult = str1 & arrF(i, 1)
ElseIf arrG(i, 1) <> "NA" And arrF(i, 1) = "Qualified" Then
If arrH(i, 1) <> arrG(i, 1) Then
strResult = "TCV Change"
Else
strResult = "Same"
End If
ElseIf arrG(i, 1) <> "NA" And arrF(i, 1) <> "Qualified" Then
If arrH(i, 1) = "NA" Then
strResult = "TCV Change"
ElseIf arrH(i, 1) <> arrG(i, 1) Then
strResult = str2 & arrF(i, 1)
Else
strResult = arrF(i, 1)
End If
End If
End If
arrJ(i, 1) = strResult
Next i
rngJ.Value2 = arrJ
End Sub
Also, I find it useful to learn to do this kind of stuff in VBA the "proper way" (although I'm sure there are more elegant ways to solve this). This is in the long run of course, for a quick solution you might use something similiar to the code you posted.
A somewhat easier, and slower, way would be the following code. Again, please note that you will need to use a loop to achieve what you're trying to do.
Sub CC()
Dim str1 As String, str2 As String
str1 = "New to Qualified and "
str2 = "TCV Change and "
Dim i As Long
Dim strB As String, strF As String, strG As String, strH As String 'rename to describe your data
Dim strResult As String
For i = 3 To 100
With ActiveSheet
strB = .Cells(i, 2).Value2
strF = .Cells(i, 6).Value2
strG = .Cells(i, 7).Value2
strH = .Cells(i, 8).Value2
End With
If strB = vbNullString Then
strResult = vbNullString
Else
If strG = "NA" And strF = "Qualified" Then
strResult = "New to Qualified"
ElseIf strG = "NA" And strF <> "Qualified" Then
strResult = str1 & strF
ElseIf strG <> "NA" And strF = "Qualified" Then
If strH <> strG Then
strResult = "TCV Change"
Else
strResult = "Same"
End If
ElseIf strG <> "NA" And strF <> "Qualified" Then
If strH = "NA" Then
strResult = "TCV Change"
ElseIf strH <> strG Then
strResult = str2 & strF
Else
strResult = strF
End If
End If
End If
ActiveSheet.Cells(i, 10).Value2 = strResult
Next i
End Sub
not sure how exactly it should work with ranges but i tried to fix your code on cell level
try to use this:
str1 = "New to Qualified and" & Range("F7")
str2 = "TCV Change and" & Range("F7")
...
With Application.WorksheetFunction
Range("J7").Value = _
IIf(.And(Range("G7") = "NA", Range("F7") = "Qualified"), "New to Qualified", _
IIf(.And(Range("G7") = "NA", Range("F7") <> "Qualified"), str1, _
IIf(.And(Range("G7") <> "NA", Range("F7") = "Qualified"), IIf(.And(Range("H7") <> Range("G7")), "TCV Change", "Same"), _
IIf(.And(Range("G7") <> "NA", Range("F7") <> "Qualified"), IIf(Range("H7") = "NA", "TCV Change", IIf(Range("H7") <> Range("G7"), str2, Range("F7"))), ""))))
End With
I keep getting error91 come up when I run my code which says that the object variable is not set.
It highlights the line:
'If OptionButton3.Value Then' when I try the debugger. I don't understand where its going wrong and I don't know what an object variable is? Has anyone else had this issue?
Any help would be much appreciated. Thank you
Sub NewEntry()
Dim cboOpName As Object
Dim TextBox1 As Object
Dim Operator As String
Dim UserForm1 As Object
Dim OptionButton1 As Object
Dim OptionButton3 As Object
Dim OptionButton11 As Object
Sheets("Sheet1").Activate
txtTitle1 = "Input Operator"
firstline1 = "Operator" & Chr(10) & Chr(10)
firstline = (firstline1)
Oper = InputBox(firstline, txtTitle1)
If Oper = "" Then
Exit Sub
Else
End If
If Oper = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Project ID Number"
firstline3 = "Project ID No" & Chr(10) & Chr(10)
firstline = (firstline3)
PROno = InputBox(firstline, txtTitle1)
If PROno = "" Then
Exit Sub
Else
End If
If PROno = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Date of Manufacture"
firstline3 = "Date of Manufacture" & Chr(10) & Chr(10)
firstline = (firstline3)
val1 = "DD/MM/YY"
DateM = InputBox(firstline, txtTitle1, val1)
If DateM = "" Then
Exit Sub
Else
End If
If DateM = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Serial Number"
firstline3 = "Serial Number" & Chr(10) & Chr(10)
firstline = (firstline3)
SerNo = InputBox(firstline, txtTitle1)
If SerNo = "" Then
Exit Sub
Else
End If
If SerNo = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Actuator ID"
firstline3 = "Actuator ID" & Chr(10) & Chr(10)
firstline = (firstline3)
ActID = InputBox(firstline, txtTitle1)
If ActID = "" Then
Exit Sub
Else
End If
If ActID = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Opening Angle"
firstline3 = "Opening Angle" & Chr(10) & Chr(10)
firstline = (firstline3)
Angle = InputBox(firstline, txtTitle1)
If Angle = "" Then
Exit Sub
Else
End If
If Angle = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Date of Test"
firstline3 = "Date of Test" & Chr(10) & Chr(10)
firstline = (firstline3)
DateT = InputBox(firstline, txtTitle1, val1)
val1 = "DD/MM/YY"
If DateT = "" Then
Exit Sub
Else
End If
If DateT = 0 Then
Exit Sub
Else
End If
UserForm2.Show
UserForm4.Show
Sheets("Sheet1").Activate
RowNow = 6
RowNum = 1
Do While RowNow = ""
If RowNow <> "" Then
RowNow = RowNow + 1
RowNum = RowNum + 1
Else
Cells(RowNow, 1).Value = RowNum
Cells(RowNow, 4).Value = PROno
Cells(RowNow, 9).Value = DateM
Cells(RowNow, 7).Value = SerNo
Cells(RowNow, 8).Value = ActID
Cells(RowNo, 2).Value = DateT
Cells(RowNow, 3).Value = Oper
Cells(RowNow, 10).Value = Angle
End If
Loop
Do While Cells(RowNow, 11) = ""
If OptionButton3.Value Then
Cells(RowNow, 11).Value = "Yes"
End If
Cells(RowNow, 11).Value = "No"
RowNow = RowNow + 1
Loop
Do While Cells(RowNow, 6) = ""
If OptionButton11.Value Then
Cells(RowNow, 6).Value = "Yes"
End If
Cells(RowNow, 6).Value = "No"
RowNow = RowNow + 1
Loop
Do While Cells(RowNow, 5) = ""
If OptionButton1.Value Then
Cells(RowNow, 5).Value = "Yes"
End If
Cells(RowNow, 5).Value = "No"
RowNow = RowNow + 1
Loop
End Sub
I have this cell that has a list of things, for example:
[dogs, cats, mice, cows, horses]
And I want to separate them in different cells:
[dogs]
[cats]
[mice]
[cows]
[horses]
Can this be done?
You can easily do this in VBA:
Sub splitString ()
Dim ran, splitS() As String
ran = Range("A1")
splitS() = Split(ran, ",")
For j = LBound(splitS) To UBound(splitS)
Range("B" & (j + 1)) = splitS(j)
Next j
End Sub
If you also want the square brackets, use this code below:
Sub splitStringWithSquareBrackets()
Dim ran, splitS() As String
ran = Range("A1")
ran = Right(ran, Len(ran) - 1)
ran = Left(ran, Len(ran) - 1)
splitS() = Split(ran, ",")
For j = LBound(splitS) To UBound(splitS)
Range("B" & (j + 1)) = "[" & splitS(j) & "]"
Next j
End Sub
With data in cell A1:
Sub dural()
Dim s As String, i As Long
s = Range("A1").Value
s = Mid(s, 2, Len(s) - 2)
ary = Split(s, ", ")
i = 2
For Each a In ary
Cells(i, "A").Value = "[" & a & "]"
i = i + 1
Next a
End Sub
This would do it - You have to select the cell with the cell before running it and it assumes that there is closed brackets ("[" and "]") on either end.
I'll have my best answer now..
Sub ImAmazing()
Dim sString As String, i As Long
If Trim(ActiveCell.Value) = "" Then Exit Sub
ActiveCell.Value = Mid(ActiveCell.Value, 2, Len(ActiveCell.Value) - 2)
Do Until InStr(ActiveCell.Value, ",") = 0
i = i + 1
Cells(ActiveCell.Row + i, ActiveCell.Column).Value = "[" & Left(ActiveCell.Value, InStr(ActiveCell.Value, ",") - 1) & "]"
ActiveCell.Value = Right(ActiveCell.Value, (Len(ActiveCell.Value) - InStr(ActiveCell.Value, ",") - 1))
Loop
ActiveCell.Value = "[" & ActiveCell.Value & "]"
End Sub