I'm trying to clean up a security vulnerabilities spreadsheet. I've created a marco that cleans up 90% of it. The final part I'm struggling with is collecting user input and passing that into a formula. There is a column that specifies the code location / path of the vulnerability. I only need a portion of this column, so my macro creates a new column and pulls a piece of information from the full location column. To do that without an input box, I simply created a cell (with a named range) and I enter the piece of text to eliminate from the full location. This works fine. However, this spreadsheet will go to clients when finished and ideally someone else (besides me) will clean these up in the future. So I'm trying to make the macro and process a little more user friendly.
Therefore, I want to prompt user with an input box, have them enter the text (to eliminate from full location) and then apply that to a formula. I've spent a lot of time reading and looking at examples and I keep landing back at "Run-time error '1004': Application-defined or object-defined error".
I would appreciate any help or suggestions: Here's my code:
Sub FormulaMacro()
Dim myValue As Variant
myValue = Application.InputBox(Prompt:="Enter text to trim.", Type:=0)
ActiveCell.FormulaR1C1 = _
"=IF(ISBLANK(R[-7]C[-5]),"""",LEFT(RIGHT(R[-7]C[-5],LEN(R[-7]C[-5])-(LEN( " & myValue & "))),SEARCH(""/"",RIGHT(R[-7]C[-5],LEN(R[-7]C[-5])-(LEN( " & myValue & "))))-1))"
Range("a1").Select
End Sub
If you're hard-coding myValue
in the inserted formula then it would be cleaner to hard-code Len(myValue)
then there's no need to worry about quoting the value
Tested:
Sub FormulaMacro()
Dim myValue As Variant, ln As Long
myValue = Application.InputBox(Prompt:="Enter text to trim.", Type:=0)
ln = Len(myValue)
If ln > 0 Then
ActiveCell.FormulaR1C1 = Replace( _
"=IF(ISBLANK(R[-7]C[-5]),"""",LEFT(RIGHT(R[-7]C[-5],LEN(R[-7]C[-5])-<ln>)," & _
"SEARCH(""/"",RIGHT(R[-7]C[-5],LEN(R[-7]C[-5])-<ln>))-1))", _
"<ln>", CStr(ln))
End If
Range("a1").Select
End Sub
To be honest it would be simpler to do the whole thing in VBA and skip the formula.
I've decided to bail on this approach. I've tried about 1000 variations before I posted and I got a couple good suggestions right away, but I get the same result. Therefore, I'm going with a less elegant solution, but still better than what I had. Thanks to those of you who tried to help. And sorry to the poor soul who runs into this in the future and finds this thread. There's no pot of gold at the end of this rainbow. :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.