替换功能和负值

时间:2015-10-02 20:00:22

标签: excel-vba replace vba excel

所以,我有一堆需要用字母代替的列,这是旧的签名过量的东西。基本上我想要做的就是用数字替换字母,然后将每个单元格乘以0.01。

所以基本上,我会有一个像这样的专栏      1. 0000012C = 00000123 x 0.01 = 2.46      2. 0002927B = 29272 x 0.01 = 585.44      3. 0000245N = -00002455 x 0.01 = -24.55

所以我无法弄清楚的问题是如何使单元格中的值为负值。正如您所看到的,字母值(JKLMNOPQR)为负数,但在替换步骤中,excel不知道如何使值为负数。

我想知道是否有一个步骤可以添加,我可以先将JKLMNOPQR的值变为负值,然后继续使用替换函数和乘法函数。我想它会搜索cel以获取JKLMNOPQR,如果它在单元格中有JKLMNOPQR,它会在单元格前面添加一个负值并继续你。

先谢谢,对不起,我是新来的。

Private Function DoReplace(Text As String) As String
Dim ReplacedValue As String
ReplacedValue = Text

ReplacedValue = Replace(ReplacedValue, "A", "1")
ReplacedValue = Replace(ReplacedValue, "B", "2")
ReplacedValue = Replace(ReplacedValue, "C", "3")
ReplacedValue = Replace(ReplacedValue, "D", "4")
ReplacedValue = Replace(ReplacedValue, "E", "5")
ReplacedValue = Replace(ReplacedValue, "F", "6")
ReplacedValue = Replace(ReplacedValue, "G", "7")
ReplacedValue = Replace(ReplacedValue, "H", "8")
ReplacedValue = Replace(ReplacedValue, "I", "9")
ReplacedValue = Replace(ReplacedValue, "{", "0")
ReplacedValue = Replace(ReplacedValue, "}", "-0")
ReplacedValue = Replace(ReplacedValue, "J", "-1")
ReplacedValue = Replace(ReplacedValue, "K", "-2")
ReplacedValue = Replace(ReplacedValue, "L", "-3")
ReplacedValue = Replace(ReplacedValue, "M", "-4")
ReplacedValue = Replace(ReplacedValue, "N", "-5")
ReplacedValue = Replace(ReplacedValue, "O", "-6")
ReplacedValue = Replace(ReplacedValue, "P", "-7")
ReplacedValue = Replace(ReplacedValue, "Q", "-8")
ReplacedValue = Replace(ReplacedValue, "R", "-9")



DoReplace = ReplacedValue
End Function




Private Sub MagicButton_Click()
Dim Text As String, CalculatedValue As Double

For Each cell In Worksheets("Sheet1").Range("A1:Z100").Cells
    If cell.Value <> "" Then
        Text = DoReplace(cell.Value)
        CalculatedValue = Val(Text) * 0.01
 Worksheets("Sheet2").Cells(cell.Row, cell.Column).Value =  CalculatedValue
    End If
Next
End Sub

2 个答案:

答案 0 :(得分:0)

您可以使用instr()首先检查字符是否存在。如果是这样,那么它将减号放在值的前面。

ReplacedValue = Replace(ReplacedValue, "A", "1")
ReplacedValue = Replace(ReplacedValue, "B", "2")
ReplacedValue = Replace(ReplacedValue, "C", "3")
ReplacedValue = Replace(ReplacedValue, "D", "4")
ReplacedValue = Replace(ReplacedValue, "E", "5")
ReplacedValue = Replace(ReplacedValue, "F", "6")
ReplacedValue = Replace(ReplacedValue, "G", "7")
ReplacedValue = Replace(ReplacedValue, "H", "8")
ReplacedValue = Replace(ReplacedValue, "I", "9")
ReplacedValue = Replace(ReplacedValue, "{", "0")

If InStr(ReplacedValue, "}") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "}", "0")
End If
If InStr(ReplacedValue, "J") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "J", "1")
End If
If InStr(ReplacedValue, "K") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "K", "2")
End If
If InStr(ReplacedValue, "L") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "L", "3")
End If
If InStr(ReplacedValue, "M") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "M", "4")
End If
If InStr(ReplacedValue, "N") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "N", "5")
End If
If InStr(ReplacedValue, "O") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "O", "6")
End If
If InStr(ReplacedValue, "P") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "P", "7")
End If
If InStr(ReplacedValue, "Q") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "Q", "8")
End If
If InStr(ReplacedValue, "R") <> 0 Then
    ReplacedValue = "-" & Replace(ReplacedValue, "R", "9")
End If 

答案 1 :(得分:0)

你可以使用这样的函数:

Function negt(celstr As String) As Boolean
Dim temp As Boolean
Dim charArr As Variant
Dim charStr As String
temp = False

charArr = Array("J", "K", "L", "M", "N", "O", "P", "Q", "R", "}") '// add as required

'// Create string representation of array
charStr = "{"""
For i = LBound(charArr) To UBound(charArr)
    charStr = charStr & charArr(i) & ""","""
Next
charStr = Left(charStr, Len(charStr) - 2) & "}"

If Evaluate("ISERROR(MATCH(" & Right(celstr, 1) & "," & charStr & ",0))") Then
    temp = True
End If

negt = temp
End Function

然后,您可以在按钮点击事件中调用它:

Private Sub MagicButton_Click()
Dim Text As String, CalculatedValue As Double

For Each cell In Worksheets("Sheet1").Range("A1:Z100").Cells
    If cell.value <> "" Then
        Text = DoReplace(cell.value)
        if negt(cell.value) then
            CalculatedValue = Val(Text) * 0.01 * -1
        else
            CalculatedValue = Val(Text) * 0.01
        End If
 Worksheets("Sheet2").Cells(cell.Row, cell.Column).value = CalculatedValue
    End If
Next
End Sub

然后,您将从所有替换行中删除所有&#39; - &#39;