我写了以下代码:
Public Sub SortMyData()
'approach: convert line to string and concatenate to that as it's a lot less picky than Excel's formats, then replace cell value with the new string.
' Excel will then define the string type as either Percentage or Scientific depending on the magnitude.
Dim i As Integer
Dim g As Integer
Dim N_Values As Integer
Dim IntermediateString As String
N_Values = Cells(1048576, 2).End(xlUp).Row 'retrieves the final filled row in column 2 (B)
For i = 6 To N_Values 'iteration loop from 6 (first row of value) to N_Values (last filled row)
If Cells(i, 2).NumberFormat <> "0.0%" Then
IntermediateString = Cells(i, 2).Value
Cells(i, 2).NumberFormat = "0.0%"
Cells(i, 2).Value = Cells(i, 2).Value / 100
Else
MsgBox ("The Range of Cells Has Already Been Formated as Percentage")
End If
Next i
For g = 6 To N_Values 'iteration loop from 6 (first row of value) to N_Values (last filled row)
If Len(Cells(g, 3) > 3) Then
Cells(g, 3).Value = Cells(g, 3).Value / 1000
Else
MsgBox ("Data is correct so no action will be taken")
End If
Next g
End Sub
代码运行正常,但它不会移动到secont if语句,它只是继续运行第一个并显示MsgBox(“单元格的范围已经被形成为百分比”)所以我不知道我在哪里犯了错误? 顺便说一下,我是一个菜鸟,所以对我很轻松!
答案 0 :(得分:1)
您声明字符串:
Dim IntermediateString As String
然后你影响它的值(顺便说一下,.Value是可选的,cells(x,y)和cells(x,y).Value是相同的):
IntermediateString = Cells(i, 2).Value
但是你没有对这个字符串做任何事情,所以我很确定这不是你打算做的(你可以评论这两行,你的程序会运行同样的方式)。
此外,当您的程序被编码时,您应该拥有最多的消息和#34;单元格的范围已经被形成为百分比&#34;作为B列中的行数,但最终应该继续达到第二个if语句。
我建议您打开执行窗口(Ctrl + G),并替换&#34; MsgBox&#34;的出现。在您的代码中由Debug.Print。您将在执行窗口中看到该消息,但它不会暂停程序的执行(更好的调试方式)