Stripping Hyphens from Numbers in Excel VBA - excel

I am new to Excel VBA and I need help removing hyphens from an Excel document. I usually use python, and could just use .strip, I was wondering if there is an equivalent in Excel. The cell I will be stripping the hyphens from is static, so the cell it self could be hardcoded I think.
I am trying to go from
233-79-01 to 2337901.
I tried these following lines of code
Sub striphyphen()
A5 = replace(A4,'-','')
End Sub
I am getting a compiling error, and am not sure why.
Any help would be appreciated

You can use replace as said #BigBen and #JvdV
Sub Test()
Dim myString as String
Dim newString as String
myString = "233-79-01"
newString = replace(myString, "-", "")
MsgBox newString
End Sub
Note That you can also set mystring = Cells(4,1).Value or mystring = Range("A4").Value

The problem is that you are wrongly referencing cells. The way you do it is the way to reference variables (just like in any other language).
To reference cell you can use one of the following (A4 cell for example):
Cells(4, 1) ' Cells(row, column)
Range("A4")
[A4]

Related

VBA Issue with Number formatted as text when numbers include comma

I use a vba script to open another workbook. This workbook is in format .XLS and the content from a DB was stored like:
"Name1" "0" "7,98"
"Name2" "5" "1"
"Name3" "2" "7,1"
When opening the workbook with a VBA script, every cell that includes a comma, is interpreted as text and shows this warning:
The number in this cell is formatted as text or is preceded by an apostrophe
Strangely, if I open the file by double clicking, the numbers are just formatted as text and don't show any error. I guess, Excel is doing some interpretting, which doesn't work.
My code is:
Dim WorkBookImport As Workbook
Name = Application.GetOpenFilename()
If Name <> False Then
Set WorkBookImport = Workbooks.Open(Filename:=Name)
End If
I tried everything from:
Range(mycolumn).Value = Range(mycolumn).Value
For loop with CDbl(.Value)
Range(mycolumn).TextToColumns ...
Nothing works ;( Please help!
Option Explicit
Sub edksg()
Dim c As Range
Set c = Sheet1.Range("D3")
c.Value2 = CDbl(c.Value2)
End Sub
Works fine for me when cell D3 of the worksheet is formatted as text.
Of course, my location uses the comma as decimal separator, so if your location uses a different standard, the issue might be there. In that case, doing something like CDbl(Replace(c.Value2, ",", Application.DecimalSeparator, 1, -1, vbBinaryCompare)) might solve that part of the issue.
If you want to loop through the entirety of some column, the end result might look something like this, assuming the values you want to convert to numbers are in column C.
Option Explicit
Sub edksg()
Dim c As Range
Dim r As Range
Set r = Sheet1.Range("C1:C" & Sheet1.Range("C" & Sheet1.Rows.Count).End(xlUp).Row)
For Each c In r
If Len(c) > 0 Then
c.Value2 = CDbl(Replace(c.Value2, ",", Application.DecimalSeparator, 1, -1, vbBinaryCompare))
End If
Next c
End Sub
It´s a problem with the formating of the cells. You should use NumberFormat to change it.
Sub FormatNumber()
ThisWorkbook.Worksheets("YourWorkbook").Range("YourRange") = CDbl(ThisWorkbook.Worksheets("YourWorkbook").Range("YourRange"))
ThisWorkbook.Worksheets("YourWorkbook").Range("YourRange").NumberFormat = "General"
End Sub
With NumberFormat you can also change it to text if you have the opposite problem, in this case you would use NumberFormat = "#"

Moving specific Text from once cell in a range to another cell in a different range

I would like to move specific text (In this case the term "Low risk") from one column to another on the same sheet.
I have tried several variations of ".Cut" function in the Macro Developer, but none seem to work.
I am not sure I understood correctly the question, but to take a value from a cell to another with vba use something like
Worksheets("Name").Cells(rowNo, column2No).Value = Worksheets("Name").Cells(rowNo, column1No).Value
Worksheets("Name").Cells(rowNo, column1No).clear
Hope it help you somehow
Try:
Option Explicit
Sub test()
Dim strValueToRemove As String, strString As String
strValueToRemove = "Low Stock"
With ThisWorkbook.Worksheets("Sheet1")
strString = .Range("A1").Value
.Range("A1").Value = Replace(strString, strValueToRemove, "")
.Range("B1").Value = strValueToRemove
End With
End Sub
Notes:
Replace is case sensitive

Is it possible for VBA to get ' character at the beginning of a cell?

I got a ' character at the beginning of range in excel that determines if it's a string. This is neat because excel hides it, however supposedly it's still there. Why doesn't .value or .value2 give this character. It would help so much in my program. I know I can do '', but that will also show in excel.
You can use Range.PrefixCharacter to determine this.
As a small example:
Public Sub Test()
MsgBox "The prefix character is " & Sheet1.Range("A1").PrefixCharacter
End Sub
Or perhaps as a UDF like this:
Function BeginsWithQuote(ByVal rng As Range) As Boolean
BeginsWithQuote = rng.PrefixCharacter = "'"
End Function

Excel VBA - Add one to each referenced cell when running macro

I have tried searching for the answer for the past couple hours but cannot seem to find what I am looking for - mind you, I am really new to VBA and macros.
What I am trying to do is to add one to what my formula is referencing to every time the macro is run. For example:
Currently the formula is =100*((Q$179/Q$167)-1)
When run, I would like it to be =100*((Q$180/Q$168)-1)
I have to do this for multiple formulas. Is there an easy way to do this with a macro?
Thank you so much in advance
Personally I'd use RegEx to achieve this. The following will increment every cells row by 1 in your formula. This should also work on columns other then Q as well
Option Explicit
Sub IncrementFormula()
Dim tmpFormula As String
Dim RegEx As Object
Dim Match, SubMatch
tmpFormula = "=100*((Q$179/Q$167)-1)"
Set RegEx = CreateObject("vbscript.regexp")
With RegEx
.Global = True
.Pattern = "(?:[A-Z]{1,})(?:\$|)([0-9]{1,})"
If .test(tmpFormula) Then
For Each Match In .Execute(tmpFormula)
For Each SubMatch In Match.submatches
tmpFormula = Replace(tmpFormula, Match, Replace(Match, SubMatch, vbNullString) & CLng(SubMatch) + 1)
Next SubMatch
Next Match
End If
End With
MsgBox tmpFormula
End Sub
Using your formula above it will output =100*((Q$180/Q$168)-1)
If you want it to persist when the workbook is closed, you'll need to store your number somewhere in a worksheet. Say it's in Cell(1,1) of Sheets(2), you could use
dim incVal as integer
incVal = cint(Sheets(2).Cells(1,1))+1
Sheets(2).Cells(1,1) = incVal
dim formula as string
formula = "=100*((Q$" & incVal & "/Q$" & (incVal-12) & ")-1)"
Then just set the desired Cell's formula to that formula. Hope this helps.
A different approach is to use OFFSET in your formula
Assuming the formula is in Q185
then:
=100*((Q$180/Q$168)-1)
Becomes:
=100*((OFFSET(Q185,-5,0)/OFFSET(Q185,-17,0)-1))
As you insert rows at the bottom of the table (and above the formulas), the formula and the cell it refers to will also move down

Assign formula to cell using VBA?

VBA has been giving me an error 1004 for this code:
Sub UpdateCellsFormula()
Dim FormulaRange As Range
Dim i As Integer
Set FormulaCellsRange = Range("J17", "J95")
i = 17
For Each r In FormulaCellsRange.Cells
r.Formula = Replace("=D17*L21+E17*M21+F17*O21+G17*N21+H17*P21+I17*Q21)/(1+0,85+0,6+0,4+0,37)", "17", i)
i = i + 1
Next
End Sub
Can anyone have a look at it?
Formulas assigned with .Formula must be in En-US locale. The decimal dot in numbers must therefore be ., not , (1+0.85+0.6+0.4+0.37).
Also you have a missing opening parenthesis in the beginning, right after =.
You also might want to learn about absolute references in Excel. That way you can copy the same formula into all cells in FormulaCellsRange without replacing anything:
Sub UpdateCellsFormula()
ActiveSheet.Range("J17", "J95").Formula = "=(D17*$L$21+E17*$M$21+F17*$O$21+G17*$N$21+H17*$P$21+I17*$Q$21)/(1+0.85+0.6+0.4+0.37)"
End Sub

Resources