I think there's a bug in InStr(), Could anyone confirm it?
For instance,
Scenario 1, keyword = "1392WG" and searchString = "11392WG", then if you use InStr(1, searchString, keyword, 1), the results will end up 1. But if the searchString = "31392WG", then the results will be 2.
Could anyone test it?
`Sub test()
Updated_Date = "2016-10-10" 'Date might need to change (values after equation, remember to add it between "")
LastRow_Sheet1 = Worksheets("Sheet1").UsedRange.Rows.Count
LastRow_Sheet2 = Worksheets("Sheet2").UsedRange.Rows.Count
For x = 3 To 3
DU_ID = Worksheets("Sheet1").Cells(x, 2).Value 'Cells(x, 2) might need to change
sting_DU_ID = UCase(CStr(DU_ID))
For y = 374 To 375
compare_DU_ID = Worksheets("Sheet2").Cells(y, 2).Value 'Cells(y, 2) might need to change
string_compare_DU_ID = UCase(CStr(compare_DU_ID))
If InStr(1, string_compare_DU_ID, "L4L", 1) >= 1 Then
If string_compare_DU_ID = sting_DU_ID Then 'If L4L in compare_DU_ID, then DU_ID definitely equals to compare_DU_ID
Worksheets("Sheet2").Cells(y, 97).Value = Updated_Date '1. Cells(y, 97) might need to change, depending on which contract date you want to update
End If
ElseIf InStr(1, string_compare_DU_ID, sting_DU_ID, 1) = 1 Then
Worksheets("Sheet2").Cells(y, 97).Value = Updated_Date
ElseIf InStr(1, sting_DU_ID, string_compare_DU_ID, 1) = 1 Then
Worksheets("Sheet2").Cells(y, 97).Value = Updated_Date
End If
Next
Next
End Sub
`
Related
I created an Excel macro but receiving Run Time Error 1004: Method 'Match' of object 'WorksheetFunction' failed.
This is the code that is returning the error:
Sub action()
Dim mTime As Double
mcol = Application.WorksheetFunction.Match(Sheet2.Range("C7"),Sheet1.Range("1:1"), 0)
i = 10
Do Until Sheet2.Cells(i, 1) = "Total"
If Sheet2.Cells(i, 1) Like "*-*" Then
mTime1 = Left(Sheet2.Cells(i, 1), InStr(1, Sheet2.Cells(i, 1), "-", vbTextCompare) - 1)
mTime = TimeValue(mTime1)
mrow = Application.WorksheetFunction.VLookup(mTime, Sheet1.Range("BA59:BB78"), 2, 1)
Sheet1.Cells(mrow, mcol) = Sheet2.Cells(i, "D").Value
End If
i = i + 1
Loop
i = i + 1
Do Until Sheet2.Cells(i, 1) = "Total"
If Sheet2.Cells(i, 1) Like "*-*" Then
mTime1 = Left(Sheet2.Cells(i, 1), InStr(1, Sheet2.Cells(i, 1), "-", vbTextCompare) - 1)
mTime = TimeValue(mTime1)
mrow = Application.WorksheetFunction.VLookup(mTime, Sheet1.Range("BA83:BB102"), 2, 1)
Sheet1.Cells(mrow, mcol) = Sheet2.Cells(i, "D").Value
End If
i = i + 1
Loop
i = i + 1
Do Until Sheet2.Cells(i, 1) = "Total"
If Sheet2.Cells(i, 1) Like "*-*" Then
mTime1 = Left(Sheet2.Cells(i, 1), InStr(1, Sheet2.Cells(i, 1), "-", vbTextCompare) - 1)
mTime = TimeValue(mTime1)
mrow = Application.WorksheetFunction.VLookup(mTime, Sheet1.Range("BA107:BB126"), 2, 1)
Sheet1.Cells(mrow, mcol) = Sheet2.Cells(i, "D").Value
End If
i = i + 1
Loop
End Sub
The 3rd line of code is causing Run Time error (mcol = Application.WorksheetFunction ....). Sheet2.Range("C7") is a date field. Basically the code should look for the date in Sheet2 and proceed with the VLOOKUP & Match functions for worksheets 1&2. Is there another way the date match that needs to take place in order for the procedure to run correctly?
My macro goes through all data rows on a specific sheet. Currently there are 6 rows. The first row is a negative number and the 2nd row is a positive number (debit and credit).
The macro reviews each row and displays a list box for the user to make a selection. Then it goes through the next row and does the same thing. I'm expecting the listbox to display 6 times, once for each row of data.
The problem I'm having is that the listbox is displaying 7 times. 3 times for the first pair or records and twice for the remaining pair of records. I can't figure out why the listbox is displaying the extra time.
Here is the code for the list box:
Private Sub ContinueButton_Click()
If IsNull(ListBox1.Value) Then
MsgBox " Please select the appropriate balance to continue. "
Exit Sub
Else
MyIndex = 0
MyIndex = ListBox1.ListIndex
MyIndex = MyIndex + 1
MyBal = ""
MyBal = APIARArray(MyIndex, 4)
Unload UserForm1
UserForm1.Hide
Sleep 750
End If
End Sub
Private Sub UserForm_Initialize()
UserForm1.Label1.Caption = "Please select the appropriate balance for Unit: " & vUnit
ListBox1.ColumnCount = 4
ListBox1.ColumnWidths = "50;50;75;50"
Dim i As Integer
With ListBox1
w = 1
i = 0
For w = 1 To UBound(APIARArray)
DoEvents
.AddItem
.List(i, 0) = APIARArray(w, 1)
.List(i, 1) = APIARArray(w, 2)
.List(i, 2) = APIARArray(w, 3)
.List(i, 3) = format(APIARArray(w, 4), "#,##0.00;[Red](#,##0.00)")
i = i + 1
Next
End With
UserForm1.Height = 215
UserForm1.Width = 348
ListBox1.SetFocus
End Sub
This is the code that calls the ListBox:
Sub LookForBalance()
Dim r As Integer
Dim APIUnit As String
r = 2
Do Until Len(Trim(Cells(r, 1))) + Len(Trim(Cells(r, 7))) + Len(Trim(Cells(r, 9))) + Len(Trim(Cells(r, 10))) + Len(Trim(Cells(r, 11))) = 0
DoEvents
If Trim(Cells(r, 27)) = "A199" Then
If Cells(r, 29) > 90 Then
APIUnit = ""
vUnit = ""
vUnit = Trim(Cells(r, 11))
If MyCntry = "A1" Then APIUnit = clsAPI.APIARSearch("WWW11", Trim(Cells(r, 11)))
If MyCntry = "A2" Then APIUnit = clsAPI.APIARSearch("WWW12", Trim(Cells(r, 11)))
If InStr(1, APIUnit, "ERROR") > 0 Then
Cells(r, 30) = "Unit Not Found"
Else
If UBound(APIARArray) > 1 Then
Load UserForm1
UserForm1.Show
Cells(r, 30) = MyBal
Else
Cells(r, 30) = APIARArray(1, 4)
End If
End If
End If
End If
r = r + 1
Loop
End Sub
There isn't much code here but I'm not sure what is going on. Any help or suggestions to resolve this issue would be greatly appreciated. Thanks in advance for your help.....
I'm trying to write a macro to appear Rows depend cell value as we see in the code below :
For a = 3 To 400
If Cells(a, 5).value = reda Then
Rows(a).Hidden = False
End If
Next a
and that's working well when variable = cell.value :
reda = LS700 & Cells(a, 5).value= LS700
and not working in this case :
reda = LS700 & Cells(a, 5).value = LS700/LS740.....
thank you so much advance
Reda
You can try this, similar to answer by BigBen, he is missing start position on instr function, it work:
Dim a As Long
Dim reda As String
reda = "LS700"
For a = 3 To 400
If InStr(1, Sheet1.Cells(a, 1).Value, reda, 1) > 0 Then
Rows(a).Hidden = True
End If
Next a
This is really all you should need:
reda = "LS700"
For Each rw In ActiveSheet.Range("E3:E400").EntireRow.Rows
rw.Hidden = InStr(1, rw.Cells(1, 1).Value, reda, 1) > 0
Next
I am working on an Excel form which has a text box txtSearch where user inputs some value. On pressing
search button btn_search2 if value is found on Sheet1 some fields should be populated by row where the value is found. Else system should return a message : Data not found.
My code works correctly if I enter values from column A but doesnt search other columns.
Any thoughts about this?
My code as below:
Dim iSearch As Long, i As Long
iSearch = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
For i = 1 To iSearch
If Trim(Sheet1.Cells(i, 1)) <> Trim(txtSearch.Text) And i = iSearch Then
MsgBox ("Invalid data")
txtSearch.Text = ""
txtSearch.SetFocus
End If
If Trim(Sheet1.Cells(i, 1)) = Trim(txtSearch.Text) Then
txtRef.Text = Sheet1.Cells(i, 1)
txtFirstname.Text = Sheet1.Cells(i, 2)
txtSurname.Text = Sheet1.Cells(i, 3)
txtAddress.Text = Sheet1.Cells(i, 4)
txtPostCode.Text = Sheet1.Cells(i, 5)
txtTelephone.Text = Sheet1.Cells(i, 6)
txtDateReg.Text = Sheet1.Cells(i, 7)
txtProve.Text = Sheet1.Cells(i, 8)
txtMemberType.Text = Sheet1.Cells(i, 9)
txtMemberFees.Text = Sheet1.Cells(i, 10)
Exit For
End If
Next i
If I'm understanding your question correctly, you want to search all rows and columns in the current data region for the value txtSearch but your code is only searching all rows in column A. Is that correct?
If so, this is happening because your code block that checks the values in each row against txtSearch is only checking cell (i,1) in each row. In cell indexing, the first value (i in your case) is the row number and the second (1 in your case) is the column number. Since the value of i is changing, you're checking multiple rows. Since the value of the second index (1) is not changing, you are only checking column 1 ("A").
To check all columns, you would need an inner for-loop to iterate through all columns in each row. So you need another variable to check the total number of columns and another loop to traverse them all, like so:
Dim iSearch As Long, i As Long
Dim jSearch As Long, j As Long
iSearch = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
jSearch = Worksheets("Sheet1").Range("A1").CurrentRegion.Columns.Count
For i = 1 To iSearch
For j = 1 to jSearch
If Trim(Sheet1.Cells(i, j)) <> Trim(txtSearch.Text) And i = iSearch And j = jSearch Then
MsgBox ("Invalid data")
txtSearch.Text = ""
txtSearch.SetFocus
End If
If Trim(Sheet1.Cells(i, j)) = Trim(txtSearch.Text) Then
txtRef.Text = Sheet1.Cells(i, 1)
txtFirstname.Text = Sheet1.Cells(i, 2)
txtSurname.Text = Sheet1.Cells(i, 3)
txtAddress.Text = Sheet1.Cells(i, 4)
txtPostCode.Text = Sheet1.Cells(i, 5)
txtTelephone.Text = Sheet1.Cells(i, 6)
txtDateReg.Text = Sheet1.Cells(i, 7)
txtProve.Text = Sheet1.Cells(i, 8)
txtMemberType.Text = Sheet1.Cells(i, 9)
txtMemberFees.Text = Sheet1.Cells(i, 10)
Exit For
End If
Next j
Next i
You can also clean up your code a bit by adding a With block and by only checking "Trim(...)<>Trim(...)" if you're at the last row and column, since in all other cases you'd want to keep searching anyways:
Dim iSearch As Long, i As Long
Dim jSearch As Long, j As Long
With Worksheets("Sheet1").Range("A1").CurrentRegion
iSearch = .Rows.Count
jSearch = .Columns.Count
End With
For i = 1 To iSearch
For j = 1 to jSearch
If Trim(Sheet1.Cells(i, j)) = Trim(txtSearch.Text) Then
txtRef.Text = Sheet1.Cells(i, 1)
txtFirstname.Text = Sheet1.Cells(i, 2)
txtSurname.Text = Sheet1.Cells(i, 3)
txtAddress.Text = Sheet1.Cells(i, 4)
txtPostCode.Text = Sheet1.Cells(i, 5)
txtTelephone.Text = Sheet1.Cells(i, 6)
txtDateReg.Text = Sheet1.Cells(i, 7)
txtProve.Text = Sheet1.Cells(i, 8)
txtMemberType.Text = Sheet1.Cells(i, 9)
txtMemberFees.Text = Sheet1.Cells(i, 10)
Exit For
End If
Next j
Next i
MsgBox ("Invalid data")
txtSearch.Text = ""
txtSearch.SetFocus
I need to search and to replace a specific part of a string in an Excel sheet.
Here is my code and I don't know how I can exactly search this part in each Cell.value.
my_new_string = "abc"
For each objSheet1 in objworkbook2.sheets
If objSheet1.Name = "Name1" Then
LastRow = objsheet1.UsedRange.Rows.Count + objsheet1.UsedRange.Row - 1
For i = 1 To LastRow Step 1
For j = 1 To 15 Step 1
If objExcel1.Cells(i, j).value = "xyz" Then 'Here I have to check if the Cell value contains xyz and to replace it by **my_new_string**
End if
Next
Next
End If
Next
Any help please ?
Thank you all,
this is working fine for me.
For Each objsheet1 In objworkbook2.Sheets
With objsheet1
If .Name = "BatchRun" Then
On error resume next
For i = 1 To 15 Step 1
For j = 1 To 10 Step 1
If InStr(1, .Cells(i, j).Value, my_old_string) > 0 Then
.Cells(i, j).Value = Replace(.Cells(i, j).Value, my_old_string, my_new_string)
End If
Next
Next
End If
End with
Next
I changed your method for finding the last row to one that is much more reliable.
Also you used 2 different objects to describe the same sheet, so I fixed it! ;)
Finally, you just need to use Replace method that will do the job perfectly fine, without need to test if the string is present with Instr (use it if you anything else to do if the old_string is detected)
Const my_old_string = "xyz"
Const my_new_string = "abc"
Const xlPart = 2
Const xlFormulas = -4123
Const xlByRows = 1
Const xlPrevious = 2
For Each objsheet1 In objworkbook2.Sheets
With objsheet1
If .Name = "Name1" Then
LastRow = .Cells.Find("*",.Range("A1"),xlPart,xlFormulas,xlByRows,xlPrevious,False).Row
For i = 1 To LastRow Step 1
For j = 1 To 15 Step 1
.Cells(i, j).Value = Replace(.Cells(i, j).Value, my_old_string, my_new_string)
' If InStr(1, .Cells(i, j).Value, my_old_string) Then
' .Cells(i, j).Value = Replace(.Cells(i, j).Value, my_old_string, my_new_string)
' End If
Next
Next
End If
End With
Next