I'm trying to replace the headers in Columns B and C with a different header text with the following code:
Columns("B").Replace What:="PN-ASSIGN", _
Replacement:="SERV BAL.", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("C").Replace What:="MISC COST", _
Replacement:="EQUIP BAL.", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
But when I try to use it, the code doesn't seem to do anything. I'm not sure what I'm missing.
Any help would be appreciated!
You need to qualify your sheet as it sounds like your macro is relying on the Active Sheet which is bad practice. You can use a With statement if you are referring to you ws multiple times. Also, you do not need to indicate all the options for the .Replace method. If you do not specify, they default to false. The below code should be sufficient as is.
Also, Option Explicit for good measure
Option Explicit
Sub Headers()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Range("B:B").Replace "PN-Assign", "Serv Bal.", xlPart
ws.Range("C:C").Replace "MISC COST", "EQUIP BAL.", xlPart
End Sub
Related
I am trying to make a userform that can bring up data using an ID number.
I am trying to reference a text box and select it, and then using it as a reference to fill out the Time and comments in the sheet. I think the is I cant put "txtID.Value" into the Find function.
Here is an example of my code:
Sheet1.Select
Columns("A:A").Select
Selection.Find(What:="txtID.Value", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
ActiveCell.Offset(0, 8).Value = txtTime2
ActiveCell.Offset(0, 9).Value = txtComment2
When using the Find function, it's recommended to use a Range object, and set it to the result. This method allows you to trap a possible scenario where Find failed to find a match in the searched range Sheet1.Columns("A:A").
Also, try to avoid using Select, Selection and ActiveCell, and use fully qualified Range objects (like in the code below).
Code
Dim FndRng As Range
Set FndRng = Sheet1.Columns("A:A").Find(What:=txtID.Value, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not FndRng Is Nothing Then ' successful find
FndRng.Offset(, 8).Value = txtTime2
FndRng.Offset(, 9).Value = txtComment2
Else ' unable to fins the value in txtID
MsgBox "Unable to find " & txtID.Value & " in Sheet1"
End If
Note: if you have this code outisde the User_Form module, then you need to add the User_Form reference when trying to get the txtID.Value.
For eaxmple, let's say the name of your form is UserForm1, then change this line to:
Set FndRng = Sheet1.Columns("A:A").Find(What:=UserForm1.txtID.Value, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
To Make it work, you will have to put the code as below, which is quite self explainatory.
Selection.Find(What:=Userform1.textbox1.value, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
hope this helps
I just started using VBA for making my life easier, programming is not my background at all. When I run codes I may write too much.
So I have two questions, check the code below.
Sub Find()
'
' Find Macro
'
'
'L.NAM.O
Worksheets("LAC").Select
Cells.Select
Selection.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("NewForecast").Select
Range("K2").Select
ActiveSheet.Paste
'L.NAM.M
Worksheets("EMEA").Select
Cells.Select
Selection.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("NewForecast").Select
Range("K" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
I want to be able to find the forecast_quarter in both sheets(I have 3 in total) and paste in one Worksheet(New Forecast), one below other. The thing is, I think this is too much, might have an easier way than run all the process all over again.
My idea would be, "search the forecast_quarter quarter in the worksheets I want and paste on below the other). As I have all criterias to do that, this could me massive. Any easier, better way to run it?
Thanks!
Something like this (untested) should work.
Sub CopyAll()
CopyDataByHeader "LAC", "forecast_quarter"
CopyDataByHeader "EMEA", "forecast_quarter"
End Sub
'Look for a specific header on a sheet, and if found copy
' the data below it to "NewForecast" sheet
Sub CopyDataByHeader(shtName As String, hdrText As String)
Dim f As Range
With ActiveWorkbook.Sheets(shtName)
'search for the header
Set f = .Cells.Find(What:=hdrText, After:=.Cells(1), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then
'found the header: copy the data below it
.Range(f.Offset(1, 0), .Cells(.Rows.Count, f.Column).End(xlUp)).Copy _
ActiveWorkbook.Sheets("NewForecast").Cells( _
Rows.Count, "K").End(xlUp).Offset(1, 0)
Else
'header not found...
MsgBox "Header text '" & hdrText & "' not found on sheet '" & shtName & "' !"
End If
End With
End Sub
Am having this "�" special character in my excel work book, i want to replace all with ".", when i place this � character in the code , it turns to "?" after i run the macro it replaces everything to "."
Sub FindReplaceAll()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Replace What:="�", Replacement:=".", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next ws
End Sub
Thanks in advance
Try to copy this character in a cell, then have the ASCII number and replace the character by the ASCII num:
Dim num =123
Ex:
Cells.Replace What:=Chr(Num), Replacement:=".", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
I have wrote a piece of code to replace any characters, but it doesn't work fine with *. Can some one guide how I can handle *.
Here is the code:
nextrow = ActiveCell.Row
tocolnbr = ActiveCell.Column
Columns(tocolnbr).Select
Selection.Replace What:="'", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Use a ~ tilde as per Office Support.
Microsoft Excel uses the tilde (~) as a marker to indicate that the next character is a literal. When you use the Find and Replace dialog box to find or replace a character such as a tilde (~), an asterisk (*), or a question mark (?), you must add a tilde (~) before the character in the Find what box.
So it must be something like this:
Selection.Replace What:="~*", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Use it's Chr value
Selection.Replace What:=Chr(42), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
EDIT:
I posted without checking. Your code works to replace "*" with nothing, although what you posted is replacing "'" with nothing.
I just used this,
Sub Button1_Click()
Dim rng As Range
Set rng = Range("K2")
rng = Replace(rng, "*", "")
End Sub
And it removed the *
Is it a Character? Or just an asterix?
For an entire range of cells,
Sub Button1_Click()
Dim rng As Range, c As Range
Set rng = Range("K2:K20")
For Each c In rng.Cells
c = Replace(c, "*", "")
Next c
End Sub
I have seen on some occasions where you would have to use the tilde "`" symbol but I don't know if is required here.
I have a worksheet with a formula that returns =NA() under certain conditions. Using VBA, I'd like to find #N/A, but I haven't been able to tweak this answer.
lastrow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
I've tried What:=CVErr(xlErrNA), What:=xlErrNA and also What:="#N/A" to no avail.
As an added difficulty, I have to take Dutch into account, so What:="#N/A" probably wouldn't even work in Dutch.
Note. I'm asking this question out of curiosity as I haven't found a method online.
At this moment, I'm calculating which cells contain =NA()
You're looking in the cell formulas. Try looking in the cell values, then I can get this to work using the string "#N/A" for the What argument:
lastrow = .Cells.Find(What:="#N/A", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Please try the below code ,
You should use the loop to read the error.
Sub Test()
If Range("A2").Text = "#N/A" Then
MsgBox "hi"
End If
End Sub
Hi I have another solution,
You have paste these formulas values into another columns as text and
then use the replace code,
Please try the below code.
Sub Tst()
Columns("C:C").Select
Selection.Copy
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
The column C Contains formulas values (Error) .
The problem here is that you're looking for "#N/A" as a value of the cell and it's not the value it's a error indicator, so if you're trying to find a cell wich gives you a error you have to use something like this:
If WorksheetFunction.IfError("Put here a loop to read the cells;"Error")="Error" then
"Write what you desire for cells with error"
end if