【问题标题】:Is there a way to change the value of the number to text in VBA有没有办法将数字的值更改为VBA中的文本
【发布时间】:2019-05-17 10:45:21
【问题描述】:

我正在尝试根据分数将数字更改为“p”或“f”。

不知道如何添加图片,所以这里是一个链接

https://drive.google.com/file/d/1dKn9mkJa9brZXq2tHz8_ULkgreWsBkTI/view?usp=sharing

我已经从字符串中删除了 % 符号,但我现在需要根据分数更改数字。 >=70 到 'p',69 和更低到 'f'

Sub PassOrFail()


Dim myValue As String

myValue = ThisWorkbook.ActiveSheet.Range("testScore").Value

myValue = Replace(myValue, "%", "")



MsgBox myValue


End Sub

期望它显示 Skim: p, MP: f

【问题讨论】:

  • 我认为您应该添加一些数据示例,说明这些 % 如何存储在单元格中。这会很有帮助。
  • 如果您的myValue 始终采用相同的格式,则需要在; 处拆分,并对值进行案例分析。我会试着举个例子,除非别人更快。
  • 假设保存值的范围格式为百分比格式,您可以使用类似:If myValue >= 0.7 Then。如果更大,则为通过,否则为失败

标签: excel vba string integer


【解决方案1】:

这是你需要的吗?

if cInt(myValue) >=70 then
  myValue="p"
elseif cInt(myValue><69 then
   myValue="f"
end if

谢谢

【讨论】:

  • 那行不通,因为myValue 中有两个不同的值。
  • 所以 myValue 会像 ";Skim:100%;MP:17%" ?
  • 如果您认为这样可以解决问题,您应该在代码中添加一些解释,以便其他人可以从中学习
【解决方案2】:

虽然不是最优雅的解决方案,但它可以根据给定的细节工作:

Sub PassOrFail()

Dim myValue As String, strResult As String
Dim arrResults

myValue = ThisWorkbook.ActiveSheet.Range("testScore").Value

'Split the value
arrResults = Split(Replace(myValue, "%", ""), ";") 'This would create a 3 column array, due to the initial ";"

    'Skim
    If Replace(arrResults(1), "Skim: ", "") >= 70 Then
        strResult = "Skim: p"
    Else
        strResult = "Skim: f"
    End If

    'MP
    If Replace(arrResults(2), "MP: ", "") >= 70 Then
        strResult = strResult + ", MP: p"
    Else
        strResult = strResult + ", MP: f"
    End If

    'Result
    MsgBox strResult

End Sub

【讨论】:

    【解决方案3】:

    试试下面的代码:

    Sub PassOrFail()
    
    Dim myValue As String
    myValue = ThisWorkbook.ActiveSheet.Range("testScore").Value
    
    'String manipulation starts here
    '1. Get the pattern position
    Dim skim_pos As Integer, mp_pos As Integer
    skim_pos = InStr(myValue, "Skim: ")
    mp_pos = InStr(myValue, ";MP: ")
    
    '2. Get the skim score
    Dim skim As String
    skim = Replace(Mid(myValue, skim_pos + 6, mp_pos - (skim_pos + 6)), "%", "")
    
    '3. Get the MP
    Dim mp As String
    mp = Replace(Mid(myValue, mp_pos + 5, Len(myValue) - (my_pos + 5)), "%", "")
    
    If CInt(skim) >= 70 Then
        skim = "p"
    Else
        skim = "f"
    End If
    
    If CInt(mp) >= 70 Then
        mp = "p"
    Else
        mp = "f"
    End If
    
    MsgBox "Skim: " & skim & ", MP: " & mp
    
    
    End Sub
    

    【讨论】:

    • 有趣的方法,但您和我的方法都严重依赖根本不改变的字符串。
    【解决方案4】:

    不是基于“Skim”或“MP”而是基于 %-sign 的解决方案:

    Sub tst()
    Dim mystring As String, y As Variant, i As Long
        mystring = ThisWorkbook.ActiveSheet.Range("testScore")
        y = Split(mystring, " ")
            For i = 1 To UBound(y)
                If InStr(y(i), "%") > 0 Then
                    If Val(Split(Split(y(i), " ")(0), "%")(0)) < 70 Then
                        mystring = Replace(mystring, Split(Split(y(i), " ")(0), "%")(0) & "%", "f")
                    Else
                        mystring = Replace(mystring, Split(Split(y(i), " ")(0), "%")(0) & "%", "p")
                    End If
                End If
            Next i
    MsgBox mystring
    End Sub
    

    【讨论】:

    • 值得一提的是,它可以工作,而且非常紧凑……虽然没有完全按照要求的格式返回结果……;Skim: p;MP: f。但是,为了便于阅读……我更喜欢别的东西。
    • 我错过了底部的预期结果,“Skim:”和“MP:”的拆分是 IMO 的最佳选择
    • 哦,顺便说一句,应该只有)(0)) &lt; 70 Then而不是)(0)) &lt;= 70 Then,否则70也被认为是失败。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 2021-10-17
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多