Trying to offset a value once a a string is found - excel

So I am using the "find" function in VBA and once this value is found, I am wanting to offset 2 cells to the right. I'm not sure if something is right with my logic, but does anyone mind having a look at my code?
Set StationSearchCell = StationLocationRng.Find(what:="*" & ShortenLocationName & "*", LookIn:=xlValues, lookat:=xlWhole)
'If the StationSearchCell returns a value
If Not StationSearchCell Is Nothing Then
projectinfosheet.Range("B23").Value = StationSearchCell.Offset(0, 2).Value
Else
End If

Related

VBA Excel - If condition with LEN function not working as expected

I have a tricky situation with my one record, which appears in one line in some documents like you can see below:
Therefore I can't extract my data properly, as the other values are thrown.
The full macro you can find here:
https://dotnetfiddle.net/m1tYvi
but what I tried to do. I tried to set up the IF condition for the total length of the string, which appears in the concerned cell.
https://www.excelfunctions.net/vba-len-function.html
that's why I tried something like this:
Dim L As Integer
L = Len("Is there room in the chamber to install a new closure :")#
Set rngFound = rngSearch.Find(What:=qu(q), Lookat:=xlPart, LookIn:=xlValues)
With datTarget
lrowG = .Cells(.Rows.Count, "G").End(xlUp).Row + 1
If rngFound Is Nothing Then
.Cells(lrowG, "G") = "Not found" ' blank
Else
If rngFound > L Then
rngFound.Copy
.Cells(1, "G").PasteSpecial xlPasteValuesAndNumberFormats
rngFound.Offset(1).Copy
.Cells(lrowG, "G").PasteSpecial xlPasteValuesAndNumberFormats
Else
rngFound.Copy
.Cells(lrowG, "G").PasteSpecial xlPasteValuesAndNumberFormats
End If
End If
.Cells(lrowG, ColSort) = datSource.Parent.Name
End With
but unfortunately, it seems not to work. The effect is exactly the same.
Is there any solution to this issue? How can I extract the record I exactly need (case 2) instead of a whole string after the colon or even different data as the offset has been applied?

VBA Find .address sometimes returns range and other times a cell

I'm having an issue with the VBA Range.Find method. What the code is doing is looking through all of the worksheets in a workbook, find any matches to data in an array, and change the color of the cell with the same value as that data.
The code works perfect on the first sheet. Then, on the next sheet, it gets hung in an infinite loop. After stepping through the code it seems that Find returns an address that is in Range format ("A2:A2") the first time it is running on this page but then reverts back to Cell format ("A2") after that. It doesn't do this on the first page, just the second one.
I could write some code to check the value returned and trim it down, but I want to fix the problem, not put a patch on it.
Here's the code that breaks:
For x = 1 To UBound(wksSheets)
For y = 0 To (UBound(findData) - 1)
With wkb.Worksheets(x)
Set rng = .Range(DataRange).Find(findData(y), LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not rng Is Nothing Then
StrtAdd = rng.Address
Do
.Range(rng.Address).Interior.ColorIndex = 3
Set rng = .Range(DataRange).FindNext(rng)
Loop While Not rng Is Nothing And Not rng.Address = StrtAdd
End If
End With
Next y
Next x
The first time through on the second page the rng.Address is "A2:A2" and gets stored in StrtAdd. Then, when the code hits the .FindNext(rng) rng.Address changes to "A2". Because of this, rng.Address is never equal to StrtAdd even though they are talking about the exact same cell. That's the infinite loop.
Any ideas on the best way to fix this?
wksSheets is an array that contains the worksheet names
findData contains the data that is to be found
Thanks in advance!!
Here is the code I ended up using. I still don't know why sometimes I am getting an address of A2:A2 and sometimes A1 but it does patch the issue.
I used InStr to find the : and then Left to knock the extra off.
I also incorporated the suggestions folks left about cleaning up the code.
For x = 1 To UBound(wksSheets)
For y = 0 To UBound(findData) - 1
With wkb.Worksheets(x)
Set rng = .Range(DataRange).Find(findData(y), LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not rng Is Nothing Then
z = InStr(rng.Address, ":")
If z > 1 Then
StrtAdd = Left(rng.Address, (z - 1))
Else:
StrtAdd = rng.Address
End If
Do
rng.Interior.ColorIndex = 3
Set rng = .Range(DataRange).FindNext(rng)
Loop While Not rng Is Nothing And Not rng.Address = StrtAdd
End If
End With
Next y
Next x
While it's a patch, it's a working patch.
I didn't use #VBasic2008's suggestion of Application.Union because the code currently functions properly and I've got to get a version out. If I run into speed issues I will go back and make a new version.
Thanks everyone.

Excel VBA shorten If statement code for automation

I'm automating an excel form and my code is too long. How do I make my code efficient?
I haven't tried anything yet.
If list = Range("E2").Value Then Result = "321"
If list = Range("E3").Value Then Result = "322"
If list = Range("E4").Value Then Result = "325"
If list = Range("E5").Value Then Result = "404"
I expect the output will be short and easy to understand.
If I understand correctly and list is one of the value in the column "E" and you need the corresponding value from the Column "D":
Dim a As Range
Set a = Range("E1:E100").Find(What:=list, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not a Is Nothing Then
Result = a.Offset(0, -1).Value
End If
You can use this code to get the list from the column E and then use offset -1 to reach the corresponding value from the Column D

Finding Partial Text In Full Row

I am trying to search an entire row for a string that contains "PROFILE". It will always be capitalized, but the format will be, for example "[9]PROFILE001".
Some extra information: I have used the Find command to locate the row that I am searching in for the string. It has its own variable that I am trying to incorporate into the range I am using.
I have searched multiple partial string articles on here, and have not been able to implement it into my code. I have tried using the Like command and the IntStr command to no avail. I believe my issue may be with how im referencing the search range or how i am searching.
Here is a snippet of my current code:
'finding item name row
Set FindRow3 = Range("A1:A100").Find("Item Name", LookIn:=xlValues)
itemnamerow = FindRow3.Row
'The section above is working as intended
'searching for the word profile, the section below is the one I am having issues with
Range("B8:Z100").Style = "Normal"
If ActiveSheet.Range("B" & itemnamerow & ":Z" & itemnamerow) Like "*PROFILE" Then
Range("C1").Value = "it worked"
End If
I am currently experiencing a run time error 13, type mismatch in the "If ActiveSheet..." line. I have not been able to get the correct index to make this correct.
I am trying to use that if the partial string is found, I want do something.
TIA
You need to use the Find method, with MatchCase and LookIn arguments set. And probably LookAt for to ensure it checks actual values and not formula syntax.
Dim profileFound as Range
Set profileFound = ActiveSheet.Range("B" & itemnamerow & ":Z" & itemnamerow).Find("PROFILE",lookIn:=xlValues,MatchCase:=True,lookAt:=xlPart)
If Not profileFound is nothing Then
debug.print profileFound.Value
Range("C1").Value = "it worked"
else
debug.print "no profile found"
End If
The reason your original code is failing is because Excel will not allow you to evaluate a multi-cell range against a single value. You could loop through each cell in the range and check each cell individually, but since Find is available, that is superfluous.
You don't provide enough parameters for the Range.Find operation. Switch to the worksheet's Match to locate Item name then again as a wildcard search to locate profile.
dim m as variant, n as variant
m = application.match("Item Name", range("A1:A100"), 0)
If not iserror(m) then
n = application.match("*profile*", cells(m, "B").resize(1, 25), 0)
If not iserror(n) then
Range("C1").Value = "it worked " & cells(m, n+1).value
end if
end if

Find the exact value of a different cell - Excel - VBA

I want to be able to compare 2 cells in VBA. The issue i'm having is that it's not making an exact match.
Dim WeekNum2 As Integer
WeekNum2 = Cells(1, 12).Value
Range("F2:F60").Find(WeekNum2).Activate
U = ActiveCell.Row
Range(Cells(U, 7), Cells(U, 7)).Value = GreenCountP4
Range(Cells(U, 8), Cells(U, 8)).Value = YellowCountP4
Range(Cells(U, 9), Cells(U, 9)).Value = RedCountP4
Above is the section of code that isn't working ideally. The value in Cells(1,12) is generated using the =WEEKNUM(K1) formula so depending on the date in K1 it returns a value from 1-52. I want it to take this value and then find the equivalent value in the range F2:F60.
In F2:F60 i have values going in order from "w46"-"w52" and then "w1"-"w52". The issue is that if the value in Cells(1, 12) is 5 for example, it will select the first row that has a 5 in it in the range (w50 in this case).
Is it possible to compare just the numbers in the cell (so not include the "w" with it still being present). If not, how do i make it so it picks up the exact values (So if the value in Cells(1, 12) is 5, then it goes to right 5 instead of the first 5 in the range)
use xlWhole value for lookat argument of Find() method:
Range("F2:F60").Find("W" & WeekNum2, lookat:=xlWhole, LookIn:=xlValues).Activate
U = ActiveCell.Row
furthermore you can avoid Select and directly go like follows:
U = Range("F2:F60").Find("W" & WeekNum2, lookat:=xlWhole, LookIn:=xlValues).Row
I suggest you use an optional argument to the find method, which allows you to insist that the entire cell is matched, and not just some subset of the text inside. You want to use the LookAt argument, and set it to xlWhole. Now nothing will be found for "5", because no cell will match "5" entirely. Cells containing "w5" aren't a match with LookAt:=xlWhole. I know, the naming of arguments is not at all intuitive...
Furthermore, life would be much easier if you simply used a string "w5" in your search. An alternative would be to strip the w's out of the cells in the find (search) range. This can be done with the split method.
You can "cheat" a little, make the value of 5 as "W5" and then find a match for that in your range. Just add another variable, let's call it WeekNum2String and add the "W" as a prefix. Now you can search your range with Range("F2:F60").Find(WeekNum2String).
Dim WeekNum2String As String
WeekNum2String = "W" & Cells(1, 12).Value
Note: you should use the Find method by defining a Range, then setting it to the Find result (and not with Activate) .This method will allow you to trap errors if there is no Find.
Like this:
Dim FindRng As Range
Set FindRng = Range("F2:F60").Find(WeekNum2String)
If Not FindRng Is Nothing Then
U = FindRng.Row
Else
MsgBox "Week number " & Cells(1, 12).Value & " not found in Range"
End If

Resources