I want to replace a semi-colon with a comma in a range.
The following code only deletes the semi-colon, it doesn't put the comma in its place.
Range("AU2:AU250").Select
Selection.Replace What:=";", Replacement:=",", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Try this ...
Sub Comma()
Dim R As Range, C As Range
Set R = Range([AU2], [AU250])
For Each C In R.Cells
C = Replace(C, ";", ",")
Next C
End Sub
This is IMHO a more VBA like approach making use of range objects rather than simulating Excel stuff that you would use when interacting directly with your sheet
Can you copy paste some of the offending cells from the CSV range in the csv file? Open it in wordpad though, not in excel.
Also, what version of excel are you using? I haven't managed to get this to occur either on 2003 or 2010.
And I assume you need to do this more than once is why you are making a macro or is find/replace failing and you are posting macro code to get help with that too? :-)
Related
As the perhaps confusing title says, I'm writing a set of automated tools for a workbook and I've hit a roadblock.
I have a function that generates a new sheet and assigns it the current date as the name, in the format of "dd-mm-yyyy" (without quotation marks), and another that updates all required formulas in the new sheet.
There is a column that calculates the difference between the values in a column between the current and previous sheet. Upon running, the code executes and replaces the names but for some reason designates only the year as the sheet name, not the entire date. This results in an invalid reference error.
Example:
Previous sheet name: 23-5-2019
Current sheet name: 25-5-2019
The changed formula in the current sheet should be: =C3-'23-5-2019'!C3
But it instead becomes: =C3-23-5-'2019'!C3
The code for the replacement is below:
Sub FormulaUpdate()
range("D2:D100").Replace
What:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.count - 2).name,
Replacement:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.count - 1).name,
LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Any help is much appreciated :)
I couldn't replicate this in code, it works as expected with the below when I name Sheet( 3) as 23-5-2019 and Sheet(2) as 25-5-2019
Sub Macro3()
to_replace = Sheets(3).Name
replace_val = Sheets(2).Name
Cells.Replace What:=to_replace, Replacement:=replace_val, LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
I'd suggest debugging step by step and confirming the values for the text to find and replacement values just before it takes place.
Also confirm that your new sheet has been created prior to doing the find / replace.
Hope that helps.
I have a working VBA script which currently replaces all dots by commas (posted below). However I would the script avoid the first two rows and change everything else. What would be the best and easiest way to do this? I have only found solutions where you have to set a range and but I want it to work for all excel sheets regardless of the number of rows or columns.
Sheets("Sheet1").Select
Cells.Replace what:=".", Replacement:=",", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
You can still use range, but generate it dynamically: use the macro recorder and see what it gives you if you select all cells with the Select All button at the top left corner, then hold down CTRL and unselect by clicking the row headers.
After having a selected range, you can use Application.Selection instead of Cells.
set wsCSV = ... (your csv worksheet)
set rng = wsCSV.UsedRange
set ws = ... (your worksheet)
ws.UsedRange.Offset(1).ClearContents()
ws.Range("A2").Resize(rng.Rows.Count, rng.Columns.Count).Values = rng.Values
Good Morning,
I am working with a rather large dataset and I am attempting to Find/Replace values which have been modified in one spreadsheet back into the original. What I would like to do is have the macro look for the information in Cell A1 and Replace it with the value in Cell B1, and then continue down until Cell A3600:B3600. Generally, I'm pretty good at peicemealing the code I find on here to get it to do what I want, but I'm at a loss as to what I should be looking for.
What I have right now is:
Sub Macro1()
'
' Macro1 Macro
'
'
Range("A1").Select
Selection.Copy
Cells.Replace What:=ActiveCell.Value, Replacement:="????", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
From here I am at a loss. I understand Range("A1").Select will paste into the Find section of the Find/Replace box, but how do I tell excel I want to select another Cell, in this case B1, for the replace portion? And then how do I tell Excel I want it to keep doing this until it runs out of values in the A Column?
Thank you in advance for any assistance you can provide.
I think you are after something like the code below:
Option Explicit
Sub Macro1()
Dim Rng As Range
Dim OrigStr As String, ReplaceStr As String
Set Rng = Range("A1:B3600")
OrigStr = Range("A1").Value2
ReplaceStr = Range("B1").Value2
' option 1: replace only if the whole cell's string equals to OrigStr
Rng.Replace What:=OrigStr, Replacement:=ReplaceStr, LookAt:=xlWhole
' option 2: replace also partial match inside the cell's string
Rng.Replace What:=OrigStr, Replacement:=ReplaceStr, LookAt:=xlPart
End Sub
I am fairly new to excel and I am trying to setup a macro that adds a row after a specific point of the worksheet. Example: Row 2 contains text "Original", so it should insert a new row afterwards, and so on.
I know that it might be easier to insert before something, so I could change the setup (so for example the word "original" would be in row 2 and the new row is added above it) if that is an easier solution.
Is this possible? How?
Thanks for all the possible help.
Slightly simpler than the previous answer:
Sub NewRowInsert()
Dim SearchText As String
Dim GCell As Range
SearchText = "Original"
Set GCell = Cells.Find(SearchText).Offset(1)
GCell.EntireRow.Insert
End Sub
This will work with the current active sheet. If you want to use some other sheet, say Sheet2, you could use:
Set GCell = Worksheets("Sheet2").Cells.Find(SearchText).Offset(1)
and if you wanted to operate on a different workbook e.g. TestBook.xlsx, you could use:
Set GCell = Workbooks("TestBook.xlsx").Worksheets("Sheet2").Cells.Find(SearchText).Offset(1)
Note that I've avoided the use of select. This may not be an issue for you, but if you're searching through thousands of rows and making many replacements it could speed up your code considerably.
Try the code below = you can change the textvalue variable value to what ever you want to search for.
Sub newRow()
Dim textvalue As String
textvalue = "Original"
Cells.Find(What:=textvalue, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.offset(rowOffset:=1, columnOffset:=0).Activate
ActiveCell.EntireRow.Insert
End Sub
I'm using a excel template with labels that must be replaced with input values using VBA.
To do that I used the:
Cells.Replace What:="&/TDT/&", Replacement:="123456987654321456654444", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
True, ReplaceFormat:=False
But the displayed value in the cell after execute this replace is 1,23457E+23. I've tried to use different formats in the cell settings and I tried to change the ReplaceFormat and SeachFormat parameters without changes in the result.
Anyone has idea why excel doesn't respect the format of the cell?
The used format is Text for the replaced cell and the version of Office is 2007/2010. What I need is to see in the cell the 123456987654321456654444 instead of 1,23457E+23 after replacement.
What you can do is to combine using Value property and Find method to implement safe (or safer) replace like this:
Sub SafeReplace(ByVal What As String, ByVal Replacement As String)
Set cell = Cells.Find(What:=What, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)
Do While Not cell Is Nothing
cell.Value = Replacement
Set cell = Cells.FindNext(cell)
Loop
End Sub
And then use it
SafeReplace "&/TDT/&", "123456987654321456654444"
It should respect the format of the cell.
' can be used in front of values to treat them as text:
Cells.Replace What:="&/TDT/&", Replacement:="'123456987654321456654444"