I am trying simple replace to delete the brackets and text within it but it's not working.
Please help. My code is:
Dim r As Integer
For r = 2 To 30
Cells(r, 3) = Replace(Cells(r, 3), "(*)", "")
MsgBox Cells(r, 3)
Next
The Ordinary replace function doesn't take wildcards * - but for some reason, Range.Replace does.
Dim rng1 as Range
Set rng1 = Range(Cells(2,3), Cells(30,3))
rng1.Replace What:= "(*)", Replacement:= "", LookAt:= xlPart, SearchOrder:=xlByRows, MatchCase:= False, SearchFormat:=False, ReplaceFormat:=False
(Obviously you probably won't need to use all of those named parameters...)
Use Range.Replace function and make the replacement at once:
Sub testReplace()
Dim rngR As Range
Set rngR = Range("C2:C30")
rngR.Replace "(*)", ""
End Sub
Related
I have a cell with ~230 characters (without space), and I would like to find '%' symbol in the cell then cut the rest of the cell and paste in the next cell to the bottom. Do it until all % found.
Sub test()
Dim c As Range
Range("B12").Select
i = 1
Set c = Selection.Find(What:="%", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not c Is Nothing Then
Do
c.Cut c.Offset(i, 0)
Set c = Selection.Find(What:="%", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
i = i + 1
Loop Until c Is Nothing
End If
End Sub
Using Split would be significantly easier. We can take the entire text into a String variable, then split it based on every occurrence of % and then output the array to the cells, skipping blanks and re-adding the % character.
Sub Example()
Dim InputRange As Range
Set InputRange = Range("B12")
Dim InputText As String
InputText = InputRange.Value
Dim TextArray() As String
TextArray = Split(InputText, "%")
Dim i As Long
For i = 0 To UBound(TextArray)
If TextArray(i) <> "" Then
InputRange = TextArray(i) & IIf(i <> UBound(TextArray), "%", "")
Set InputRange = InputRange.Offset(1, 0)
End If
Next
End Sub
I have some code I found on this sight which is extremely close to what I want. It essentially highlights values that do not match to a list of values found in another tab. The addition I would like to make to this code is to continue the search across multiple columns.
For this particular example, I would like the code to change each "U" and each "A" to "V" and "B" respectively, (essentially moving right to the next column for each sheet) until there is a blank column.
Sub CompareAndHighlight()
Dim rng1 As Range, rng2 As Range, i As Integer, j As Integer
For i = 1 To Sheets("workorders").Range("U" & Rows.Count).End(xlUp).Row
Set rng1 = Sheets("workorders").Range("U" & i)
For j = 1 To Sheets("craftspersondata").Range("A" & Rows.Count).End(xlUp).Row
Set rng2 = Sheets("craftspersondata").Range("A" & j)
If StrComp(Trim(rng1.Text), Trim(rng2.Text), vbTextCompare) = 0 Then
rng1.Interior.Color = RGB(255, 0, 0)
rng1.Value = "Incorrect Name"
End If
Set rng2 = Nothing
Next j
Set rng1 = Nothing
Next i
End Sub
If you're just looking to replace each occurrence of "U" to "A" & each "V" to "B", inside a given range, wouldn't a find-replace work better? Simpler, quicker, cleaner. Ex:
Range(<insert range>).Replace What:="V", Replacement:="B", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range(<insert range>).Replace What:="U", Replacement:="A", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
If you want the replace to be case sensitive just replace MatchCase:=False with MatchCase:=True, and if you only want it to replace if the entire contents of the cell are "U" or "V", then replace LookAt:=xlPart with LookAt:=xlWhole
The only reason the iteration made sense in the example code you cite is that the individual that wrote that not only wanted to replace the value, but also to alter the fill color for the cell rng1
I've got a CSV with 27,000 records with some last names having brackets and text in them such as Harvey (MD5), i've managed to work it so it will do it when targeted at a single cell to delete the text inside the brackets and the brackets themselves but when i try to loop it i get a [RUNTIME ERROR 5] -invalid procedure call or argument....
This is my loop
Sub test()
Application.ScreenUpdating = False
Dim myCell As Range
Dim myRange As Range
Dim cellvalue As String
Set myRange = Range("D1:D27168")
For Each myCell In myRange
cellvalue = myCell.Value
openingParen = InStr(cellvalue, "(")
closingParen = InStr(cellvalue, ")")
enclosedValue = Mid(cellvalue, openingParen + 1, closingParen - openingParen - 1)
Cells.Find(What:=enclosedValue, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Replace What:=enclosedValue, Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next myCell
Application.ScreenUpdating = True
End Sub
is there something I'm doing wrong, i've removed the bracket find and replace code to keep it short.
feel like something is a miss
You probably get the error because of openingParen and closingParen values. If Mid gets wrong integer inputs (such as Mid("text", 1, 1 - 3) or Mid("text", -10, 5)) it fails with an error 5.What I recommend is to test your InStr results before calling Mid and/or add Error Handling, something like (pseudo-code, not tested) :
Option Explicit
Option Base 0
(.....)
On Error Resume Next
newRes = Mid("mytext", iFrom, iTo)
If (Err.Number = 0) Then
'do what you planned here
Else
'handle error
Call MsgBox(Err.Message & CStr(iFrom) & ", " & CStr(iTo))
End If
(...)
On Error Goto 0 'turn off error sinking
I have created this macro code using the record function.
Sub Macro1()
Cells.Find(What:="Text to find", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate
Range("E5").Select
ActiveCell.FormulaR1C1 = "text to enter"
Range("D6").Select
Cells.Find(What:="Text to find", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate
Range("E9").Select
ActiveCell.FormulaR1C1 = "text to enter"
End Sub
I need this macro to continue through the same column until it cannot find any more instances of the searched for word without it going back to the top of the column.
So it starts in a column, every time it finds a specified word it tabs across 1 column and pastes in a specified word.
It continues to search for the specified word in the same column until it cannot find it without starting at the top of the column.
Hope that makes some sense.
Not sure I understand but I think what your looking for is:
For each cell in columns(4).cells
If cell.value="Text to find" Then Cell.offset(0,1) = "Text to enter"
Next cell
You can use Find and FindNext to do this quickly, ie to:
search column D for the text in StrOld
enter any matches into column E with the text in StrIn
code
Sub Recut()
Dim strAddress As String
Dim StrIn As String
Dim StrOut As String
Dim rng1 As Range
StrOld = "Old"
StrIn = "New"
Set rng1 = Range("D:D").Find(StrOld, , xlFormulas, xlWhole, , , True)
If Not rng1 Is Nothing Then
strAddress = rng1.Address
Do
rng1.Offset(0, 1) = StrIn
Set rng1 = Range("D:D").FindNext(rng1)
Loop While Not rng1 Is Nothing And rng1.Address <> strAddress
End If
End Sub
1
2
3
4
.
.
So I have a sequence of numbers running from 1-20. I have the number "1" on top selected and I would like to search the entire column and find the number "9". The code works when I don't name the range "rng"; it finds the number and selects. But the code stops working when I name the range of number. What's wrong with the range function? could it be that if I define Dim rng as Range that when I later define the "Set rng=" I cannot have the ".Select" or ".Copy" extension on the end?
Sub macro2()
Dim rng As Range
Set rng = Range(ActiveCell, ActiveCell.End(xlDown)).Select
rng.Find(10).Select
End Sub
Also, If I want to sum the entire column from 1-20, on the last cell below the number "20" should I use the following code? because the application object doesn't seem to do it. Thank you!
rng.End(xlDown).Offset(1, 0).Select
Application.WorksheetFunction.Sum (rng.Value)
To look for 10 in the active column you could try this (which ends up selecting the first 10 - although Select in vba isn't normally needed other than taken the user to location at code end)
test that the found range exists (ie you can find 10 before proceeding)
you should also use xlWhole to avoid matching 100 if the current default for [lookAt] is xlPart
using search [After] as Cells(1, ActiveCell.Column , and [Search Direction] as xlNext finds the first value looking down.
code
Sub QuickFind()
Dim rng1 As Range
Set rng1 = ActiveCell.EntireColumn.Find(10, Cells(1, ActiveCell.Column), xlFormulas, xlWhole, , xlNext)
If Not rng1 Is Nothing Then
Application.Goto rng1
Else
MsgBox "10 not found"
End If
End Sub
Part 2
Sub Other()
Dim rng1 As Range
Set rng1 = Range(Cells(1, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp))
rng1.Cells(rng1.Cells.Count).Offset(1, 0) = Application.WorksheetFunction.Sum(rng1.Value)
End Sub
Try this, I hope this will help u to find the specific row no as well as column name too. In code you can use
strRw = FindColumn(Sheet name, "Value which need to be found", True, "Cell Name",Row number)
sourceCOL = colname(FindColumn(Shee Name, "Value which need to be found", False, , 4))
Below is main function of find
Public Function FindColumn(colnocountWS As Worksheet, srcstr As String, Optional rowflag As Boolean, Optional bycol As String, Optional strw As Integer, Optional stcol As Integer) As Integer
Dim srcrng As Range 'range of search text
Dim srcAddr As String 'address of search text
Dim stcolnm As String
colnocountWS.Activate
If stcol <> 0 Then stcolnm = colname(stcol)
If stcol = 0 Then stcolnm = "A"
If strw = 0 Then strw = 1
colnocountWS.Range(stcolnm & strw).Select
If ActiveSheet.Range(stcolnm & strw) = srcstr Then
ActiveSheet.Range(stcolnm & strw).Select
FindColumn = 1
Else
If bycol = "" Then
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Else
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End If
'ByPart
If srcrng Is Nothing Then
If bycol = "" Then
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Else
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End If
End If
If srcrng Is Nothing Then
FindColumn = 0
Exit Function
Else
srcAddr = srcrng.Address
colnocountWS.Range(srcAddr).Select
FindColumn = ActiveCell.Column
If rowflag = True Then FindColumn = ActiveCell.Row
End If
End If
End Function
'this function find column name
Public Function colname(iFinalCol1 As Integer) As String
Dim colnm As String
On Error GoTo gg
If Mid(Cells(1, iFinalCol1).Address, 3, 1) = "$" Then
colnm = Mid(Cells(1, iFinalCol1).Address, 2, 1)
Else
colnm = Mid(Cells(1, iFinalCol1).Address, 2, 2)
End If
gg: colname = colnm
End Function