如果单元格为空,则VBA无法正常工作

时间:2019-11-08 11:26:44

标签: excel vba

我一直在研究脚本,该脚本将首先导入CSV,然后根据“材料”列中的值将值添加到单元格中。

我可以使脚本正常工作,但前提是所有单元格都有值。

这并不总是可能的,因为导入的某些数据将缺少这些值。

我尝试在代码中添加一行,如果该行为空,则会向该单元格添加值“ 0”。我希望这一行将按顺序执行,但事实并非如此。我收到运行时错误1004消息。我知道,如果单元格中有一个数值,它就会起作用。

Sub CostCal()

'Call load_csv

    Dim Firstrow As Long
    Dim lastrow As Long
    Dim lRow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim i As Long
    Dim ws2 As Worksheet

    Application.Volatile


    'Optimize Code
      Call OptimizeCode_Begin

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With ActiveSheet

        'We select the sheet so we can change the window view
        .Select

        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        'ViewMode = ActiveWindow.View
       'ActiveWindow.View = xlNormalView

        'Turn off Page Breaks, we do this for speed
        '.DisplayPageBreaks = False

        Firstrow = .UsedRange.Cells(1).Row
        lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        For lRow = lastrow To Firstrow Step -1

            Set ws2 = ThisWorkbook.Sheets("MASTER")


            With .Cells(lRow, "G")

                If Not IsError(.Value) Then

                       If Cells(lRow, "D").Value = "" Then Cells(lRow, "D").Value = ("0")

                       'If .Value Like ("*MS*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H5").Value
                       If .Value Like ("*MS*") Then Cells(lRow, "D").Value = ("=" & Cells(lRow, "E") & "*MASTER!$H$5")

                       If .Value Like ("*ANGLE*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H6").Value

                       If .Value Like ("*BOX SECTION*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H6").Value

                       If .Value Like ("*CHANNEL*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H6").Value

                       If .Value Like ("*I-BEAM*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H6").Value

                       If .Value Like ("*PIPE*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H6").Value

                       If .Value Like ("*ROUND-BAR*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H6").Value

                       If .Value Like ("*SQUARE-BAR*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H6").Value

                       If .Value Like ("*STAINLESS-STEEL*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H7").Value

                       If .Value Like ("*THREADED-BAR*") Then Cells(lRow, "D").Value = Cells(lRow, "D").Value * ws2.Range("H8").Value

                       'If .Value Like ("*PURCHASED*") Then Cells(lRow, "D").Value = Cells(lRow, "D").Value * ws2.Range("H8").Value
                       If .Value Like ("*PURCHASED*") Then Cells(lRow, "D").Value = "=(RC[-6]) * (ws2.range(R8, C8)"

                       If .Value Like ("*POLYCARBONATE*") Then Cells(lRow, "D").Value = Cells(lRow, "D").Value * ws2.Range("H8").Value

                       If .Value Like ("*POLYURETHANE*") Then Cells(lRow, "D").Value = Cells(lRow, "D").Value * ws2.Range("H8").Value

                       If .Value Like ("*PVC*") Then Cells(lRow, "D").Value = Cells(lRow, "D").Value * ws2.Range("H8").Value

                       If .Value Like ("*RUBBER*") Then Cells(lRow, "D").Value = Cells(lRow, "D").Value * ws2.Range("H8").Value

                       If .Value Like ("*DURBAR*") Then Cells(lRow, "D").Value = Cells(lRow, "E").Value * ws2.Range("H5").Value

                       If .Value Like ("*1_INCH_MESH*") Then Cells(lRow, "D").Value = Cells(lRow, "D").Value * ws2.Range("H8").Value

                      'If .Cells.Offset(0, -6).Value Like ("*Part*") Then Cells(lRow, "J").Formula = ("=" & Cells(lRow, "D") & "*" & Cells(lRow, "C"))

                       If .Cells.Offset(0, -6).Value Like ("*Part*") Then Cells(lRow, "J").Value = "=(RC[-6]*RC[-7])"


                End If

            End With

        Next lRow

    End With

'Optimize Code
  Call OptimizeCode_End

End Sub

该例程应在D列中查找一个值,如果单元格值与代码中的值匹配,则它将乘以主表中特定单元格中的值。我在下面有两个选择,第一个将仅使用值,第二个将使用单元格位置,第二个使我可以轻松地更新特定物料的本机成本。

最终的If语句将在J列中提供总计x数量x单位成本

我仍然需要在此列的底部提供总计,但是我想让代码的第一部分开始工作。

我敢肯定,有很多更聪明,更简单的方法可以完成我需要做的事情。但是我还不知道...

非常感谢您提供的所有帮助。

1 个答案:

答案 0 :(得分:1)

我替换了该行:

If Cells(lRow, "D").Value = "" Then Cells(lRow, "D").Value = ("0")

使用:

If Trim(.Cells.Offset(0, -3)) = "" Then Cells(lRow, "D").Value = ("0")

发现单元格中充满了空间,因此需要先修剪

相关问题