2016-04-23 35 views
0

我的代码存在问题。它不承认Empty Cell如果单元格为空然后条件为

的条件是“如果单元格是空的,然后重复,否则更改文本的字体在当前单元格上方的单元格”

Sub lastprice() 
    Dim price As String 
    Do 
     Selection.Find.ClearFormatting 
     With Selection.Find.Font 
      .Size = 11 
      .Bold = True 
     End With 
     With Selection.Find 
      .Text = "last price (" 
      .Forward = True 
      .Wrap = wdFindStop 
      .Format = True 
     End With 
     Selection.Find.Execute 

     If Selection.Find.Found = False Then 
      Exit Do 
     Else 
      Selection.MoveRight Unit:=wdCell 
      price = Trim(Selection.Text) 

      If price <> "" Then ' I think there is some problem 
       Selection.MoveLeft Unit:=wdCell, Count:=2 
       Selection.Font.Name = "Cambria" 
       Selection.MoveDown Unit:=wdLine, Count:=1 
      Else 
      End If 
     End If 
    Loop 
End Sub 

有关详情,请

它应该用条件更详细阐述通过表中的第二图像

回答

1

我在调试器中执行了代码,您应该完成该代码,并检查price是什么。事实证明,它包含某种具有字符代码13,7的单元格标记。因此,仍然在Word中的空单元格包含某些内容。改变你的if语句如下:

If mid(price,1,1) <> Chr(13) Then 
+0

我已经添加第二个图像,以更具体地关于循环 –

0

编辑完成后:@保罗奥格尔维

If mid(price,1,1) = Chr(13) Then 

作品对我来说,我改变逻辑<>=

这里是工作的代码

Sub lastprice() 
Dim price As String 
Do 
Selection.Find.ClearFormatting 
With Selection.Find.Font 
    .Size = 11 
    .Bold = True 
End With 
With Selection.Find 
    .Text = "last price (" 
    .Forward = True 
    .Wrap = wdFindStop 
    .Format = True 
End With 
Selection.Find.Execute 
If Selection.Find.Found = False Then 
    Exit Do 
    Else 
    Selection.MoveRight Unit:=wdCell 
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend 
    price = Trim(Selection.Text) 
    If Mid(price, 1, 1) = Chr(13) Then 
    Selection.MoveLeft Unit:=wdCell, Count:=2 
    Selection.Font.Name = "Cambria" 
    Selection.MoveDown Unit:=wdLine, Count:=1 
    Else 
    Selection.MoveRight Unit:=wdCharacter, Count:=1 
    End If 
    End If 
Loop 
End Sub 
相关问题