vba卡在一个循环中,而不是继续下一个命令

我写了下面的代码:

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(“单元格的范围已经形成为百分比”),所以我不知道我在哪里做了一个错误? 顺便说一句,我是一名新手

你声明string:

 Dim IntermediateString As String 

然后你影响一个值(顺便说一句,.Value是可选的,单元格(x,y)和单元格(x,y).Value是相同的):

 IntermediateString = Cells(i, 2).Value 

但是,这个string不会做任何事情,所以我敢肯定,这不是你打算做的事情(你可以评论这两行,你的程序会以同样的方式运行)。

另外,当你的程序被编码时,你应该至less有尽可能多的消息“单元格的范围已经被形成为百分比”作为B列的行数,但它应该最终将继续达到秒如果语句。

我build议你打开执行窗口(Ctrl + G),并用Debug.Print代替代码中“MsgBox”的出现。 你会在执行窗口中看到消息,但是它不会暂停程序的执行(更好的debugging方法)