通过宏添加excel IF语句。编译时出错

时间:2018-10-12 08:38:37

标签: excel vba

我的代码与UserForm一起使用,其中所有信息将在保​​存到表中之前写入。我想在IF语句中添加两个公式,但是不断出现编译错误。

我根本不知道为什么它会检查该代码……不应该。只能将其与用户表单中的文本一起粘贴到右栏中。

有人可以告诉我我在做什么错吗?

我尝试了.Formula.Value,但没有任何效果。

始终“编译时出错”。

Private Sub SaveData()
'Copy input values to sheet.
Dim lrow As Long
Dim ws As Worksheet
Set ws = Tabelle1
Dim i As Integer
lrow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).row

For i = 1 To lrow
    If ws.Cells(i, 1).Value = Me.E1GCharge.Value Then
        MsgBox "Diese Charge ist schon vorhanden, bitte einfach Bestand 
        aktualisieren"
        Exit Sub
    End If
Next i

With ws
    .Cells(lrow, 1).Value = Me.E1GCharge.Value
    .Cells(lrow, 3).Value = Me.E1GMatName.Value
    .Cells(lrow, 4).Value = Me.E1Gtype.Value
    .Cells(lrow, 5).Value = Me.E1GMatNumber.Value
    .Cells(lrow, 6).Value = Me.E1GExpiryDate.Value
    .Cells(lrow, 6).NumberFormat = "mmm.yyyy"
    .Cells(lrow, 7).Value = Me.E1GBoxPcs.Value
    .Cells(lrow, 8).Value = Me.E1GAmmount.Value
    .Cells(lrow, 9).Value = Me.E1GUnit.Value
    .Cells(lrow, 10).Value = Me.E1Gkonz.Value
    'IF Statement compiling Error "=IF([@[Rest Tubes]]<>"N/A""
        .Cells(lrow, 11).FormulaLocal  = "=IF([@[Rest Tubes]]<>"N/A",[@Pieces]-[@[Auslagerung Total]]/[@[Number of tubes Ammount]],[@Pieces]-[@[Auslagerung pcs]])"

    'IF Statement compiling Error "=IF([@[Rest Tubes]]="N/A""
        .Cells(lrow, 12).FormulaLocal  = "=IF([@[Number of tubes Ammount]]="N/A";"N/A";[@Pieces]*[@[Number of tubes Ammount]]-[@[Auslagerung Total]])"          
End With

'Clear input controls.
Me.E1GMatName.Value = ""
Me.E1Gtype.Value = ""
Me.E1GMatNumber.Value = ""
Me.E1GExpiryDate.Value = ""
Me.E1GBoxPcs.Value = ""
Me.E1GAmmount.Value = ""
Me.E1Gkonz.Value = ""
Me.E1GUnit.Value = ""

Call GetData 
End Sub

1 个答案:

答案 0 :(得分:1)

Chr(34)代替“(双引号)

双引号中不能包含双引号。因此,您必须使用Chr函数用Chr(34)替换“内部”双引号,这样您便可以:

    'IF Statement compiling Error "=IF([@[Rest Tubes]]<>"N/A""
  .Cells(lrow, 11).FormulaLocal = "=IF([@[Rest Tubes]]<>" & Chr(34) & "N/A" & Chr(34) & ",[@Pieces]-[@[Auslagerung Total]]/" & "[@[Number of tubes Ammount]],[@Pieces]-[@[Auslagerung pcs]])"

    'IF Statement compiling Error "=IF([@[Rest Tubes]]="N/A""
  .Cells(lrow, 12).FormulaLocal = "=IF([@[Number of tubes Ammount]]=" & Chr(34) & "N/A" & Chr(34) & ";" & Chr(34) & "N/A" & Chr(34) & ";[@Pieces]*[@[Number of tubes Ammount]]-[@[Auslagerung Total]])"

出于可读性考虑,建议每行不要传递80个字符,因此不要害怕在每行末尾用'_分隔行:

    'IF Statement compiling Error "=IF([@[Rest Tubes]]<>"N/A""
  .Cells(lrow, 11).FormulaLocal = "=IF([@[Rest Tubes]]<>" & Chr(34) _
      & "N/A" & Chr(34) & ",[@Pieces]-[@[Auslagerung Total]]/" _
      & "[@[Number of tubes Ammount]],[@Pieces]-[@[Auslagerung pcs]])"

    'IF Statement compiling Error "=IF([@[Rest Tubes]]="N/A""
  .Cells(lrow, 12).FormulaLocal = "=IF([@[Number of tubes Ammount]]=" _
      & Chr(34) & "N/A" & Chr(34) & ";" & Chr(34) & "N/A" & Chr(34) _
      & ";[@Pieces]*[@[Number of tubes Ammount]]-[@[Auslagerung Total]])"