Sub test1()
Dim Str As String
Dim Search As String
Dim Status As String
Str = Cells(2, 5).Value
Search = FDSA!Cells(2, 5).Value
Status = FDSA!Cells(2, 10).Value
If InStr(Search, Str) = True Then
Status = "ok"
Else
End If
End Sub
I will be building up from this with loops. I want to check if what is in Cells(2,5) is contained in FDSA!Cells(2,5). If it is true then I would like to mark FDSA!Cells(2,10) as ok. I am getting an object required message. This is what I could come up with after looking at examples and tutorials. Let me know if you have questions
Only second time working on VBA.
Thanks in advance, Alexis M.
Your syntax for referencing the worksheet is incorrect. That is probably throwing the error. You need to call to Worksheets("FDSA") and not use the FDSA! call like you have.
Also, you will have to set the cell value equal to Status for this to work. Just changing Status will not write it back into the workbook.
Also InStr returns the location of the match. If you want to know if there was a match, you need to check that the return is >0. This code should run and hopefully is closer to correct than your current code.
Sub test1()
Dim Str As String
Dim Search As String
Str = Cells(2, 5).Value
Search = Worksheets("FDSA").Cells(2, 5).Value
If InStr(Search, Str) > 0 Then
Worksheets("FDSA").Cells(2, 10).Value = "ok"
End If
End Sub
Related
I have got through every compile error post on the site and I know it has something to do with the .Value. I have tried setting it to = Now and adding (Now, term.Value) similar to what others have done. Does anyone see why I'm getting the "Compile error: Method or data member not found"
Please let me know if you see anything wrong specifically with the words() = Split(term.Value) code:
Sub FindSimilar()
Dim phrases As Range, phrase As Range
Dim terms As Range, term As Range
Dim matches As String
Dim words() As String
'ensure this has the correct sheet names for your workbook
Set phrases = ThisWorkbook.Worksheets("Export").Range("B2:B4345")
Set terms = ThisWorkbook.Worksheets("Topics").Range("D1847:D4847")
For Each term In terms
matches = ""
words() = Split(term.Value)
For i = 0 To UBound(words, 1)
If Len(words(i)) > 2 Then
Select Case words(i)
Case "business", "example", "string"
Case Else
For Each phrase In phrases
If InStr(1, phrase.Value, words(i)) Then
matches = matches & phrase & "/"
End If
Next phrase
End Select
End If
Next i
If matches <> vbNullString Then
term.Offset(0, 6).Value = Left(matches, Len(matches) - 1)
Else
term.Offset(0, 6).Value = "No match"
End If
Next term
End Sub
Also, as a note, this code was working well and it just stopped working so I'm assuming it's just an update or some small text. I'm using Excel for Mac.
I cannot find the correct type for my lookup function in vba.
My Excel formula is as following and works fine.
=IF(INDIRECT("'Enclosure4-Workflow_Structure'!C"&MATCH('Enclosure2-Accesses'!A8;
'Enclosure4-Workflow_Structure'!A:A; 0))="Create";
IF(LOOKUP(2; 1/('Enclosure5-Workflow_Steps'!A:A=INDIRECT("'Enclosure4-Workflow_Structure'!D"
&MATCH('Enclosure2-Accesses'!A8; 'Enclosure4-Workflow_Structure'!A:A; 0)));
'Enclosure5-Workflow_Steps'!D:D) = "Task"; 'Enclosure2-Accesses'!B8; FALSE); FALSE)
The first if-clause works fine for me but the second if-clause contains the lookup function. This lookup function should come up with "Task", therefore I thought I should set the DIM as String but I constantly receive the error message: "Type mismatch".
The line which throws the error should get the last occurence of a value. This value should correspond to "Task" in the D column.
lOccurence = WorksheetFunction.Lookup(2, 1 / (Enc5.Range("A:A") = Enc4.Cells(MatchCrt, "D").Value), Enc5.Range("D:D"))
I am curious why the above line causes the error. In Excel the line works without problem. Is the line incorrect or is the DIM type (String) incorrect?
My VBA code is:
Public Sub CopyUserAR2Data()
Dim Enc2 As Worksheet
Dim Enc4 As Worksheet
Dim Enc5 As Worksheet
Dim Enc9 As Worksheet
Dim MatchCrt As Double
Dim lOccurence As String
Set Enc2 = Sheets("Enclosure2-Accesses")
Set Enc4 = Sheets("Enclosure4-Workflow_Structure")
Set Enc5 = Sheets("Enclosure5-Workflow_Steps")
Set Enc9 = Sheets("Enclosure9-Dependency")
MatchCrt = WorksheetFunction.Match(Enc2.Cells(9, "A"), Enc4.Range("A:A"), 0)
lOccurence = WorksheetFunction.Lookup(2, 1 / (Enc5.Range("A:A") = Enc4.Cells(MatchCrt, "D").Value), Enc5.Range("D:D"))
If Enc4.Cells(MatchCrt, "C") = "Create" Then
Enc9.Cells(2, 1).Value = lOccurence
End If
End Sub
Your string will cause an error due to you pulling a long value from the Lookup
Best thing to do is call
Dim lOccurence As Variant
If you still the get error, there's something else going on - may a null return value
I went with the suggestion from Rory in the comment section and am looping through the array.
If sheet1.Cells(FirstCondition, "C") = "Create" Then
For d = 1 To numberOfRows
If (sheet1.Cells(FirstCondition, "D").Value = sheet2.Cells(d, "A").Value And sheet2.Cells(d, "D").Value = "Task") Then
{"Cell values are entered"}
End If
Next d
End If
This works now as intended.
#Rory, thank you very much for your patience and assistance!
I am writing a VBA code to go through a specified range or ranges, look for a keyword provided by the user at run-time, and grab the value in the cell offset from the cell with the keyword by an amount also provided by the user. For instance, if you wanted to look through A1:B10 for the word "Apple" and then grab the value in the cell to the right of every instance of "Apple", it can do that. Two weird things have been occurring for me. First and not so weird, when I run it and click the cancel button on the userform that only contains the single line "Unload Me", it throws an error saying it expected and End Sub statement, but it has one. I don't know why it is doing that. Weird thing number 2. Whenever I click and move the cursor to the end of the file after the Cancel_Click() sub, my excel crashes and closes. Every. Single. Time. And it is weird that it does that just from me clicking. It also sometimes happens when I click around the Cancel_Click() sub or hit enter around there too. Just simply from clicking. I don't get it. Any ideas? Code contained in the userform is below. Fyi, the user can input ranges like "A1:A10,E1:E10" separated by commas for multiple ranges. I don't think it is important for this question, but I thought I would add that since i don't know how to add the userform here, if you even can.
Private Sub Accept_Click()
'Searches for string input into the KeywordBox
'Grabs contents of the cell defined by the OffsetBox
'The range it searches through is defined by the RangeBox
Dim rawRange As String: rawRange = Me.RangeBox.Text
Dim rawOffset As String: rawOffset = Me.OffsetBox.Text
Dim Keyword As String: Keyword = Me.KeywordBox.Text
Dim numOfRanges As Integer: numOfRanges = 1
Dim Ranges() As Range
Dim commaLoc As Integer: commaLoc = -1
Dim tempRange As String: tempRange = rawRange
Dim offset As Integer
Dim values() As Double
Dim valCount As Integer: valCount = 0
'--------------------------------------------------------
'Set ranges
For i = 1 To Len(rawRange)
If (Mid(rawRange, i, 1) = ",") Then
numOfRanges = numOfRanges + 1
End If
Next
ReDim Ranges(numOfRanges) As Range
If (Not numOfRanges = 1) Then
For i = 1 To numOfRanges - 1
commaLoc = InStr(1, tempRange, ",")
Set Ranges(i) = Range(Left(tempRange, commaLoc - 1))
tempRange = Right(tempRange, Len(tempRange) - commaLoc)
Next
End If
Set Ranges(numOfRanges) = Range(tempRange)
'---------------------------------------------------------
'Set offset
If (IsNumeric(rawOffset)) Then
offset = CInt(rawOffset)
Else:
MsgBox ("Offset was not input as a number")
Exit Sub
End If
'----------------------------------------------------------
'Searches for keyword
For i = 1 To numOfRanges
For Each cell In Ranges(i)
If (cell.Value = Keyword) Then
valCount = valCount + 1
End If
Next
Next
ReDim values(valCount) As Double
valCount = 0
For i = 1 To numOfRanges
For Each cell In Ranges(i)
If (cell.Value = Keyword) Then
valCount = valCount + 1
values(valCount) = cell.offset(0, offset).Value
End If
Next
Next
For i = 1 To valCount
Range("I" & i).Value = values(i)
Next
Unload Me
End Sub
I've had similar, weird things happen to me. A good thing to try is to force the VBA project to reset, then save, exit, and restart Excel.
To force a project reset, add an Enum to the general section of one of your code modules. It doesn't matter what the enum is...make it something simple, like
Enum stoplight
Red
Yellow
Green
End Enum
As you do that, you'll get a message saying that it will reset your project. That's fine; let that happen. Then save your Excel workbook, exit excel completely, start it up again, reload your workbook, go into the VBA Editor, and delete the enum you added. Then recompile and see if things work better for you.
You put an "Exit Sub" in the set offset, this is probably causing your problem.
I was able to fix the issue by making a new workbook and copying everything over. It worked fine. I think the original was corrupted somehow. For those having the same issue, I think Rich Holton's answer would be worth a try in case you have more than just a few things to copy. Thanks everyone for you time and input on this!
I'm trying to write a program in VBA for excel that will search through a column of "names", and if that name has the case-sensitive string "CAN" within it, then the column 6 columns over will be added to a total (canadaTotal). This is what I have so far... The problem is within the instr/isnumeric portion. I'm sure I'm using one of them incorrectly.. and if anybody could offer an alternative solution, or a quick fix, I would appreciate it.
(hint... I'm not sure if i can use my "search" variable as the second input of the instr function...)
Private Sub CommandButton5_Click()
Dim i As Integer
Dim col As Integer
Dim canadaTotal As Integer
Dim search As String
Dim canadaCheck As Long
i = 1
col = 4
canadaTotal = 0
Worksheets("sheet1").Activate
While Not Worksheets("Sheet1").Cells(i, col).Value = ""
search = Cells(i, col).Value
If IsNumeric(InStr(0, search, "CAN")) Then
canadaTotal = canadaTotal + Cells(i, col).Offset(0, 6).Value
End If
i = i + 1
Wend
MsgBox (canadaTotal)
End Sub
The problem you are having is that the Instr function starts with position 1, not position 0.
Also, Instr returns 0 if the string is not found, not a non-numeric value, so your test will always be true.
Additionally, the default for Instr is that it will not search case sensitive. In order to search case sensitive, you need to use the last "compare" parameter and set it to vbBinaryCompare.
Change this to:
If Instr(1, search, "CAN", vbBinaryCompare) <> 0 Then
and it should work.
I stumbled across this useful VBA function: Find a row from Excel table using VBA
Function GetRow(TableName As String, ColumnNum As Long, Key As Variant) As Range
On Error Resume Next
Set GetRow = Range(TableName) _
.Rows(WorksheetFunction.Match(Key, Range(TableName).Columns(ColumnNum), 0))
If Err.Number <> 0 Then
Err.Clear
Set GetRow = Nothing
End If
End Function
This function works perfectly fine for texts. However I have serious problems with searching for numbers. I do never get a row back when searching for an integer.
I am using the function like this:
If Not Userform_Input.Text = "" Then
Set ref = GetRow("table", 3, Userform_Input.Text)
If Not ref Is Nothing Then
variable_a = Cells(ref.Row, 2).value
End If
End If
Looking at the code I can't see the problem. Does anybody else?
can you try changing your code as below:
If Not Userform_Input.Text = "" Then
Set ref = GetRow("table", 3, cint(Userform_Input.Text)) ' force numeric
If Not ref Is Nothing Then
variable_a = Cells(ref.Row, 2).value
End If
End If
maybe that will help