The command Replace() does not work for me. I finally got it working but only by skipping over error messages. Why a command works with when there are errors, dont ask, here is my working code. Can anyone please explain where im going wrong?
If Target.Address(0, 0) = "E3" Then
Range("E3").Select
On Error Resume Next
Selection.NumberFormat = "#"
Selection.Replace What:="-", Replacement:=""
Selection.Replace What:=" ", Replacement:=""
Selection = UCase(Selection.Value)
End If
Also, why wouldn't something like this work?
selection.value = replace(selection.value," ", "")
A bit of context for those who want: I'm using this to remove " - " and spaces out of product style numbers automatically. eg 05402-pt072 004 needs to equal 05402pt072004.
Thanks to anyone who responds.
Given the existence of Target I assume the code is in a Worksheet_Change event.
That said, the following should work:
If Target.Address(0, 0) = "E3" Then
Application.EnableEvents = False 'stop change event from firing again
With Target
.NumberFormat = "#"
.Replace What:="-", Replacement:=""
.Replace What:=" ", Replacement:=""
.Value = UCase$(.Value)
End With
Application.EnableEvents = True
End If
I am assuming you are running into an infinite loop, as each time you update the targetcell, the worksheet_change event kicks in.
How about
Private Sub Worksheet_Change(ByVal Target As Range)
Dim s As String
If Target.Address = "$E$3" Then
Application.EnableEvents = False
s = Target
s = Replace(s, " ", "")
s = Replace(s, "-", "")
Target = Format(s, "#")
Application.EnableEvents = True
End If
End Sub
On a new Excel worksheet write this:
Sub TestMe()
Range("A1") = "05402-pt072 004"
Stop
Range("A1").Value = Replace(Range("A1").Value, " ", "")
Stop
Range("A1").Value = Replace(Range("A1").Value, "-", "")
End Sub
Then press F5. The 05402-pt072 004 appears on A1
Press F5. Now the text on A1 is 05402-pt072004.
Press F5. Now the text on A1 is 05402pt072004.
Enjoy!
As a next step, you may remove the .Value from the code. Then try to write With Range("A1") etc.
This will also work:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$3" Then
Target.NumberFormat = "#"
Value = Target.Value
Value = Replace(Value, "-", "")
Value = Replace(Value, " ", "")
Target.Value = UCase(Value)
End If
End Sub
Try replacing your code with the following
If Not Application.Intersect(Target, Me.Range("E3")) Is Nothing Then
With Target
.NumberFormat = "#"
Replace expression:=.Value2, Find:="-", Replace:=vbNullString
Replace expression:=.Value2, Find:=" ", Replace:=vbNullString
.Value2 = UCase(.Value2)
End With
End If
Related
I have the following code that helps me to record multiple dates in one cell stacked however I couldnt figure out how the 2nd and further entries have the strikethrough to show that date has been changed.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge = 1 And Target.Column = 1 Then
If Len(Target.Value) > 0 Then
Target.Offset(, 1).Value = Target.Value & _
IIf(Len(Target.Offset(, 1).Value), Chr(10), _
"") & Target.Offset(, 1).Value &
Target.Offset(, 1)
End If
End If
End Sub
If you know the length of the string that you want to NOT be strikethrough, you can use the following, replacing the 6 with your length:
With ActiveCell
With .Characters(6, Len(.Value) - (6 - 1))
.Font.Strikethrough = True
End With
End With
Without working through Terry Field's interpretation, I would never have understood your intent but there are still a few points to make.
When writing values to the worksheet within a Worksheet_Change, always suspend event handling or the event driven sub procedure will try to run on top of itself.
Whenever possible, deal with multiple Target cells rather than exiting the Worksheet_Change whenever more than a single Target is changed.
You appear to be dealing with dates in column A so use .Text rather than .Value or .Value2 in order to capture the dates as they appear on the worksheet.
This may be minor but there is no reason to .Strikethrough the vbLF so the .Strikethrough should start at the length of Target + 1 and continue to the end of the cell's displayed value.
Revised Worksheet_Change code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
On Error GoTo meh
Application.EnableEvents = False
Dim l As Long, ol As Long, t As Range
For Each t In Intersect(Target, Range("A:A"))
If CBool(Len(t.Value2)) Then
l = Len(t.Text)
With t.Offset(0, 1)
.Value = t.Text & _
IIf(CBool(Len(.Value2)), vbLF & t.Offset(0, 1).Text, vbNullString)
.Characters(l + 1, ol).Font.Strikethrough = True
End With
t.VerticalAlignment = xlTop
End If
Next t
End If
meh:
Application.EnableEvents = True
End Sub
I'm having trouble making sure that my code uses what the end user inputs to find a set of data pertaining to that value and continues with the code there. For example, if the user were to input "V-" as the prefix to the tag number, in theory cell A7 should be selected after the code is complete. However, the code proceeds to run line "MsgBox "No blank cell was found below a tag number with prefix " & str & ".", vbExclamation" and select cell A3 due to the fact that it contains "V-" in the cell. I tried changing the Matchcase to true but it did not help. I also do not want the entered value to be case sensitive.
Code being used:
Private Sub Worksheet_Activate()
Dim msg As String
Dim Cell As Range
Dim str As String, firstcell As String
msg = "Would you like to find the next available tag number?"
result = MsgBox(msg, vbYesNo)
If result = vbYes Then
str = Application.InputBox("Enter The Tag Number Prefix ", "Prefix To Tag Number")
If str = "" Then Exit Sub
If Right(str, 1) <> "-" Then str = str & "-"
With Range("A:A")
Set Cell = .Find(str, lookat:=xlPart, MatchCase:=False)
If Not Cell Is Nothing Then
firstcell = Cell.Address
Do
If Cell.Offset(1, 0) = "" Then
Cell.Offset(1, 0).Select
Exit Sub
ElseIf InStr(LCase(Cell.Offset(1, 0)), LCase(str)) = 0 Then
Cell.Select
MsgBox "No blank cell was found below a tag number with prefix " & str & ".", vbExclamation
Exit Sub
End If
Set Cell = .FindNext(Cell)
Loop While Not Cell Is Nothing And firstcell <> Cell.Address
End If
End With
Else
Cancel = True
End If
End Sub
If you want to find cells whose content begins with (e.g.) "V-" then
Set Cell = .Find(str & "*", lookat:=xlWhole, MatchCase:=False)
For the data below:
Sub tester()
With ActiveSheet.Columns(1)
Debug.Print .Find("C-" & "*", lookat:=xlWhole, _
MatchCase:=False).Address() '>> $A$3
Debug.Print .Find("V-" & "*", lookat:=xlWhole, _
MatchCase:=False).Address() '>> $A$5
End With
End Sub
I have the following code which filters based on what is typed into the textbox. This works for strings however it does not work for integer searches. Any idea what I might be doing wrong?
Private Sub TextBox1_Change()
On Error Resume Next
metin = TextBox1.Value
Set bul = Range("a4:a10").Find(What:=metin)
Application.Goto Reference:=Range(bul.Address), Scroll:=False
Selection.AutoFilter field:=1, Criteria1:=TextBox1.Value & "*"
If metin = "" Then
Selection.AutoFilter
End If
End Sub
Add Range("a4:a10").NumberFormat = "#" at the beginning. With numbers, Excel tries to compare values, not their digit representation as string. Hence, it tries to match exactly :) With that line it will treat digit sequence as string and will apply string comparison. The final code would be:
Private Sub TextBox1_Change()
Range("a4:a10").NumberFormat = "#"
On Error Resume Next
metin = TextBox1.Value
Set bul = Range("a4:a10").Find(What:=metin)
Application.Goto Reference:=Range(bul.Address), Scroll:=False
Selection.AutoFilter field:=1, Criteria1:=TextBox1.Value & "*"
If metin = "" Then
Selection.AutoFilter
End If
End Sub
For optimization sake, you should set the range format somewhere outside this method, so you don't have to do it every time the text box has changed.
I hope this is possible. I would like to know how to make excel tell me in what cell it encountered my predefined "error". For example part of the code is this:
Sub CheckErrors()
For Each Cel In Range("A3:A400")
If Cel.Value = "CR" Then
If IsEmpty(Cel.Offset(0, 6)) = True Then
Msgbox "Add a description (name) when creating"
End If
End If
If Cel.Value = "CR" Then
If IsEmpty(Cel.Offset(0, 7)) = True Then
Msgbox "Please choose type when creating"
Exit For
End If
End If
Next
I would like the message boxes to also include what specific cell excel found empty. So if in column A there is CR then column G has to have a description and I would like the message box to say "Add a description (name) when creating, revise cell G3" if G3 is empty while A3 has CR in it.
Any help is appreciated. Im very new to VBA and coding, so even the most basic might be helpful!
Regards
Jim
Well, you could add inside the MSGBOX the property .address... this way:
Sub CheckErrors()
For Each Cel In Range("A3:A400")
If Cel.Value = "CR" Then
If IsEmpty(Cel.Offset(0, 6)) = True Then
Msgbox "Add a description (name) when creating " & Cel.Offset(0, 6).address
End If
End If
If Cel.Value = "CR" Then
If IsEmpty(Cel.Offset(0, 7)) = True Then
Msgbox "Please choose type when creating " & Cel.Offset(0, 7).address
Exit For
End If
End If
Next
End Sub
Your question is vague and unclear, please provide more info about what you really want, what you did to achieve that, what errors you have, or any other results..
Edit
In the comment of Nick Dewitt you will see what you need to replace the $ in the address Replace(Cel.Offset(0, 6).address, "$", "")
Edit #2
Sub CheckErrors()
For Each Cel In Range("A3:A400")
If Cel.Value = "CR" Then
If IsEmpty(Cel.Offset(0, 6)) = True Then
Msgbox "Add a description (name) when creating " & Replace(Cel.Offset(0, 6).address, "$", "")
End If
End If
If Cel.Value = "CR" Then
If IsEmpty(Cel.Offset(0, 7)) = True Then
Msgbox "Please choose type when creating " & Replace(Cel.Offset(0, 6).address, "$", "")
Exit For
End If
End If
Next
End Sub
I have a series of values on a sheet
E11: "Notional"
E12: "Strike"
E13: "Coupon"
Etc.
In my code, use the sheet name
Function AddInput(name As String, strInput As String, Optional suffix = "") As String
Dim inputVal As Variant
On Error GoTo ERROR_FUNCTION
With Worksheets(name)
If .Cells.Find(what:=strInput, LookAt:=xlWhole,searchorder:=xlByRows).Offset(0, 1).Value <> "" Then
inputVal = Trim(Cells.Find(what:=strInput, LookAt:=xlWhole, searchorder:=xlByRows).Offset(0, 1).Value)
If TypeName(inputVal) = "Date" Then inputVal = Format(inputVal, "YYYYMMDD")
AddInput = Replace(strInput, " ", "") & "=" & inputVal & "&"
If suffix <> "" Then AddInput = AddInput & suffix
Else
AddInput = ""
End If
End With
Exit Function
ERROR_FUNCTION:
Debug.Print strInput & ": input not found"
MsgBox strInput & ": input not found"
End Function
I am able to find whats in Cell E12, but not E11.
I have done the following:
1) I copied the cell value into the search function directly (No chance to fat-finger it).
2) I copied the values from E11 down 1 (if for some reason it couldn't find that range etc... it just returned E12).
I still cannot find that one cell, it works for every other value I put through it.
Has anyone encountered this, and how did you resolve it?
Thanks!
Make sure you start each search correctly:
Sub dural()
Dim r As Range
strSheetname = ActiveSheet.Name
MyInputString = Application.InputBox(Prompt:="Enter value", Type:=2)
With Sheets(strSheetname)
Set r = .Cells.Find(what:=MyInputString, After:=Range("A1"))
End With
MsgBox r.Address(0, 0)
End Sub