So I have a certain word within a word in a cell in excel that I'm trying to remove. For instance in one of the cells I have P/N279-82345 and I'm trying to get rid of the P/N in the front but keep the rest. I know there is the like function but is there a way to use the like function to remove just the P/N?
Thank You
Answer is
x = Replace(ws2.Cells(i, 1), "P/N", "")
Use the replace function.
Syntax can be found here: https://support.office.com/en-us/article/REPLACE-REPLACEB-functions-8d799074-2425-4a8a-84bc-82472868878a
if you are looking for a VBA macro function you can use something like:
Cells(1, 1).Value = Replace(Cells(1, 1).Value, "P/N", "")
where the Cells value is the range you want to search.
If you are looking for an excel worksheet function, you can use substitute:
=substitute(A1,"P/N","")
where A1 is the cell that contains the value you want to substitute.
Related
I am quite new to VBA but have been working with Excel a bit. I created a formula that does exactly what I need it to do: find the Nth to last word (mostly 2nd or 3rd to last) in a cell. I think my main issue is how to apply a formula to a range of cells without overwriting the cell and how to use excel formulas in VBA. The Excel formula I use is as follows
=TRIM(LEFT(RIGHT(" "&SUBSTITUTE(TRIM(A1)," ",REPT(" ",60)),180),60))
It might not be the most eloquent way but it works pretty well in Excel. Changing the number 180 to 60 will give last word, 120 2nd to last and so on. But in VBA it gives 1st syntax error and when I get it to run without the arguments and with only TRIM(A1) it overwrites the cell. The code I use is as follows (referencing only A1 to test it):
reportsheet.Range("A1").Formula = "=TRIM(LEFT(RIGHT(" " & SUBSTITUTE(TRIM(A1)," " ,REPT(" ",60)),180),60))"
My macro searches and extracts specific data from Sheet1 to Sheet2. Now I would want to apply this (or a similar) formula to the data it extracts to the Sheet2. I have tried a lot of different things from using VBA's own trim to making a completely custom function. None of it seems to work and I think it is down to a misunderstanding on how Excel formulas and VBA play together.
In addition I am trying to find a way to find the only numbers in the cell and trim out everything else. Any help with this would also be appreciated.
EDIT: Sorry guys, I had a mistake in the code I provided, it should have been referring to A1 in both instances.
Double up quotes within a quoted string or use alternatives.
reportsheet.Range("A1").Formula = "=TRIM(LEFT(RIGHT("" "" & SUBSTITUTE(TRIM(A20),"" "" ,REPT("" "",60)),180),60))"
'alternative
reportsheet.Range("A1").Formula = "=TRIM(LEFT(RIGHT(char(32) & SUBSTITUTE(TRIM(A20), char(32), REPT(char(32),60)),180),60))"
Doubling the quotes as #Jeeped and the commenters wrote is solving your issue with the formula.
As an alternative, you could write a function ("UDF") that returns the n-th word of a string. It is rather easy by using the VBA function split that returns an array of strings. Put the following code in a Module:
Public Function getWord(s As String, ByVal n As Integer) As String
n = n - 1 ' Because Array index will start at 0
Dim arr() As String
arr = Split(s, " ")
If UBound(arr) >= n Then
getWord = arr(n)
End If
End Function
In Excel, you write for example =getWord(A20, 3) as formula
I'm trying to assign a cell a formula using VBA, but every time I run the code it assign to the cell the result not the formula itself, like when I enter the formula using the excel spreadsheet.
Does anyone know how to display the formula using a macro within a cell and not the formula result.
I'm asking this because I need to insert this line within an existing sheet among other data and then run another macro to keep it updated, and the macro depends on this formula.
The code I'm using
Cells(C, 9).Formula = Application.Index(Plan2.Range("B2:D10000"), _
Application.Match(Plan1.Range("B" & C) & Range("F" & C), Plan2.Range("A2:A10000"), 0), 3)
As you can see this formula depends on the row that its inserted.
Your Formula is ultimately being reduced to whatever is returned by Application.Index. Unless the value being returned there is an actual formula string then you will just get a number as the result and this is set to the .Formula.
If you want to actually set the formula, you need to create a string in VBA that represents the formula to use. In this case, that string would look something like:
Cells(C, 9).Formula = "=INDEX(Plan2!B2:D10000, MATCH(Plan1!B" & C & "..."
where you concatenate in the dynamic parts. The end result needs to look like a normal formula. The Application.XXX and Application.WorksheetFunction.XXX functions return actual results, not pieces that can be combined to create a formula.
Ok, I'm sure this question is really basic but all my searchers turn up complicated answers and I've been messing with VBA for about a day. I have two worksheets in an excel doc, I've created a button that I can click that invokes my macro that is just moving cells from one work sheet to another. But I need my macro to determine what row I am on. I'm using this:
r = ActiveCell.Row
to determine my row, but what would be the easiest way to use that variable in a range statement like this:
Range("A2").Select
You could use the Range method with the & operator to join the variable into a string:
Range("A" & r)
Alternately you can use the Cells method which takes arguments (Row, Column):
Cells(r, "A")
I am creating a string that is a formula. Like in here (this is a simpler example)
If:
A1 is "Sum"
A2 is "D3"
Then B1 is =Concatenate("=",A1,"(",A2,")")
I want a VBA macro that takes the result of the formula in B1 and paste is as a formula in C1.
I need C1 to be the formula =SUM(D3)
I think it involves the PasteSpecial and evaluate, but I can't figure out how.
I don't want to use the INDIRECT function because I want to be able to fill more cells using than formula and the relative references inside.
with Activesheet
.Range("C1").Formula = .Range("B1").Value
End With
I don't know if you explicitly want a command macro, but this thing seems like a great use of a VBA UDF. If you create the UDF:
Function EvalFormula(f As String) As Variant
EvalFormula = Application.Evaluate(f)
End Function
Then in C1 you can call:
=EvalFormula(B1)
Writing this as a UDF is going to eliminate those unpleasant situations where you forgot to run your macro and now your sheet is all out of whack.
I used ActiveSheet.Cells(5, 3).Formula = ActiveSheet.Cells(5, 3).Value to use the text value as a formula
ActiveSheet.Cells(2, 2).Value = ActiveSheet.Cells(1, 2).Value
You can work the rest from that I'd assume :-)
I’m not an Excel or VBA expert but I want to insert this current excel formula into cell’s using VBA.
Current Excel formula:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
VBA formula :
ActiveSheet.Range("a" & ActiveSheet.Rows.Count).End(xlUp).Offset(2, 12).Value = "=IF(OR(ISNUM(R[-3]C[-9])=FALSE;ISNUM(R[0]C[-9])=FALSE);'';IF(R[0]C[-10]='Total';R[0]C[-9]-R[-3]C[-9];''))"
It doesn’t work… Someone can help me please?
Try using .formula = instead of .value = in your VBA code.
Setting the .value of a cell simply copies in whatever value you specify. In this case, your formula is simply converted to a string value.
Using the .formula property, you are actually specifying the formula that gets used to compute the value, which is what you are looking for.
Can I first suggest a simplification of your formula, from:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
...to...
=IF(AND(C573="Total"; ISNUM(D570); ISNUM(D573)); D573-D570; "")
Then, I'd set a cell (the active cell in the example below) to use that formula using the VBA code:
ActiveCell.Formula = "=IF(...)"