2016-04-23 26 views
0

Ich habe ein Problem mit meinem Code. Es erkennt die Empty Cell nicht.Wenn Zelle leer dann Bedingung

Die Bedingung ist: „Wenn die Zelle leer ist, dann wiederholen, sonst die Schriftart des Textes in der Zelle über der aktuelle Zelle“

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 

Weitere Details Check

Es soll erarbeiten mehr durch die zweite Bild der Tabelle mit Bedingungen

Antwort

1

Ich habe den Code im Debugger genommen, den du hättest machen sollen, und inspiziert was price war. Es stellt sich heraus, dass es eine Art von Zellenmarkierung mit den Zeichencodes 13, 7 enthält. Eine leere Zelle, die immer noch in Word ist, enthält etwas. Ändern Sie Ihre if-Anweisung auf die folgenden:

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

Ich habe ein zweites Bild hinzugefügt, um genauer über die Schleife zu sein –

0

Nach Edited: @ Paul Ogilvie

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

Werke für mich, ändere ich die Logik <>-=

Hier arbeitet Code

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 
Verwandte Themen