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
Related
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
Working on having a userform in Excel that will cut a row with a value in column "B" that is input on the userform, then paste the row in sheet2 while also adding 3 more values from the user form. This is what I have but currently it's doing nothing for me:
Private Sub OkButton2_Click()
Dim i As Long: i = 1
With ActiveSheet
For n = nLastRow To nFirstRow Step -1
If .Cells(n, "B") = "ChartTextBox2.Value" Then
.Cells(n, "B").EntireRow.Cut Sheet2.Cells(i, "A")
.Cells(n, "B").EntireRow.Delete
i = i + 1
'Transfer information
Sheets("Sheet2").Cells(emptyRow, 7).Value = DTPicker4.Value
Sheets("Sheet2").Cells(emptyRow, 8).Value = DispoTextBox.Value
Sheets("Sheet2").Cells(emptyRow, 9).Value = ReasonTextBox.Value
End If
Next
End With
End Sub
Below code worked fine for me. Are you comparing with text "ChartTextBox2.Value" or with the text in the textbox ChartTextBox2? Assuming you are comparing with the text in the textbox, below code works fine for me. Also in your code nLastRow and nFirstRow were not declared. If this is not the case you can remove these statements.
Private Sub OkButton2_Click()
Dim nLastRow As Long 'If already declared neglect this statement
Dim nFirstRow As Long 'If already declared neglect this statement
Dim i As Long: i = 1
nLastRow = ActiveSheet.UsedRange.Rows.Count 'If already specified neglect this statement
nFirstRow = ActiveSheet.UsedRange.Row 'If already specified neglect this statement
With ActiveSheet
For n = nLastRow To nFirstRow Step -1
If .Cells(n, "B") = ChartTextBox2.Value Then
.Cells(n, "B").EntireRow.Cut Sheets("Sheet2").Cells(i, "A")
.Cells(n, "B").EntireRow.Delete
i = i + 1
'Transfer information
Sheets("Sheet2").Cells(emptyRow, 7).Value = DTPicker4.Value
Sheets("Sheet2").Cells(emptyRow, 8).Value = DispoTextBox.Value
Sheets("Sheet2").Cells(emptyRow, 9).Value = ReasonTextBox.Value
End If
Next
End With
End Sub
I'm trying to make a loop that will go thru this data set.
This is how the output should look.
Im still getting the hang of loops, but this is what i have so far:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long, k As Byte, iLines As Long
j = 1
For i = 1 To 25
For k = 1 To 8
If k = 1 Then
Cells(j, 10).Value = Len(Cells((j + 2), 1).Value) - Len(Replace(Cells((j + 2), 1).Value, ",", "")) + 1
Cells(i, 11).Value = "SET"
Cells(i, 12).Value = Cells(i, 1).Value
End If
Next k
Next i
End Sub
My problem is on my loop output at the moment. It only counts the commas in the first data set and not the other ones. Also where it outputs SET it copies down instead of just putting it in one cell. See Below.
I will probably have more question as i progress along. Thanks in advance for the help!
Try this:
Whenever possible get rid of the loop. I replaced it with a find to find the next cell with "HW" in it. It will automatically step from one "HW" to the Next.
When using steps anchor everything on the one row and expand selection using resize.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim i As Long
Dim k As Long
Set ws = ActiveSheet
With ws
For i = 1 To 200
If Left(.Cells(i, 1).Value, 2) = "HW" Then
On Error Resume Next
k = .Range(.Cells(i + 1, 1), .Cells(200, 1)).Find("HW").Row
On Error GoTo 0
If k <= i Then k = 200
.Cells(i, 10).Value = Len(Cells((i + 2), 1).Value) - Len(Replace(Cells((i + 2), 1).Value, ",", "")) + 1
.Cells(i, 11).Value = "SET"
.Cells(i, 12).Resize(k - i).Value = .Cells(i, 1).Resize(k - i).Value
i = k - 1
End If
Next i
End With
End Sub
I'm working on a program with Excel's VBA functionality, but ran into a very strange logic error.
For SheetIndex = 1 To 6
With ActiveSheet
For ColIndex = 2 To 6
For DateIndex = 0 To MinLimit
datethingy = .Cells(1, ColIndex).Value
If (.Cells(1, ColIndex).Value = Date_Array(DateIndex, 1)) Then
For RowIndex = 2 To 11
' Compare every time slot value here to every time slot value in the array
datethingy = Trim(CStr(.Cells(RowIndex, 1).Value)) 'ERROR OCCURS HERE
If (Trim(CStr(.Cells(RowIndex, 1).Value)) = Date_Array(DateIndex, 2)) Then
.Cells(RowIndex, ColIndex).Value = "Checked"
End If
Next
End If
Next
Next
End With
SheetIndex = SheetIndex + 1
Application.Worksheets(SheetIndex).Activate
Next
So in the code above, I go through a series of cell values and make comparisons to values I already have in my array. However, when I draw the values from the cells, rather than "8:30", it comes up as "0.354166666...7". I have no idea why it's coming up like this, I'm even making sure it's being compared as a string and not as an int or anything else.
Here is where I set the values for the sheet's cells.
.Cells(2, 1).Value = "8:30"
.Cells(3, 1).Value = "9:00"
.Cells(4, 1).Value = "10:15"
.Cells(5, 1).Value = "11:30"
.Cells(6, 1).Value = "12:30"
.Cells(7, 1).Value = "13:30"
.Cells(8, 1).Value = "14:45"
.Cells(9, 1).Value = "16:00"
.Cells(10, 1).Value = "17:15"
.Cells(11, 1).Value = "18:15"
With Range("A2", "A11")
.HorizontalAlignment = xlCenter
.Font.Bold = True
.ColumnWidth = 15
End With
ActiveSheet.Cells.Rows.AutoFit
Does anyone have any idea why this could be happening?
Excel stores times and dates as numbers. Each whole number is a day and hours, minutes and seconds are broken down as fractions of that day. 8:30 is just a time so there is no whole number and 8.5/24 = 0.3541667.
You can test this with this code and this may provide you a way to format your inputs. Type 8:30 into cell A1
Sub test()
Debug.Print sheet1.Range("A1").Value = "8:30" 'returns false
Debug.Print Format(sheet1.Range("A1").Value, "h:mm") = "8:30" 'returns true
Debug.Print Sheet1.Range("A1").Text = "8:30" 'return true
End Sub
I have a userform and I want to check if a particular giftcard is present in the database or not and if found I would like to pull the values into the relevant textboxes.
I'm using the code below. This code checks for a valid gift-card (this part works).
iRow is the Last row in database
ValueToFind is the ID of gift-card
WithType is the Type of gift-card.
Code:
For i = 1 To iRow + 1
If ws.Cells(i, 2).Value = ValueToFind And _
ws.Cells(i, 1).Value = WithType And _
ws.Cells(i, 6).Value = "" Then
...
Exit Sub
End If
Next i
But I can't find a way to update the Me.TXT_MONEY.Value and Me.TXT_DATE.Value in the userform textboxes.
Can you help me with this code?
Is this what you are trying?
For i = 1 To iRow + 1
If ws.Cells(i, 2).Value = ValueToFind And _
ws.Cells(i, 1).Value = WithType And _
ws.Cells(i, 6).Value = "" Then
TXT_MONEY.Value = ws.Cells(i, X).Value
TXT_DATE.Value = ws.Cells(i, Y).Value
Exit Sub
End If
Next i
Replace X and Y in the above code with the relevant column number from where you want to pick up values.
Also instead of looping, you may actually want to use .Find? See THIS link from my blog.