Excel VBA:范围中的每个空单元格= 0

时间:2015-11-24 15:47:34

标签: excel-vba for-loop vba excel

我一直试图运行以下代码而没有运气。什么都没发生:

dim Dim OutputDataBordyRange As Range
Dim OutputCell As Range

For Each OutputCell In OutputDataBordyRange.Cells

    If OutputCell Is Nothing Then

        OutputCell.Value = 0

    End If

Next OutputCell

有什么建议吗?

编辑。我没有运气就试过以下建议。因此,根据要求,找到以下完整代码:

Sub Output_SBTB()

Dim InputDataRange As Range
Dim InputCountryRange As Range
Dim InputSiteRange As Range
Dim InputServiceLineRange As Range
Dim InputCalcHourlySalaryRateRange As Range
Dim InputRegionRange As Range
Dim OutputDataBodyRange As Range
Dim OutputHearderRowRange As Range

Dim OutputArrayColumns As Long
Dim OutputArrayRows As Long

Dim OutputArray() As Variant
Dim OutputArrayCounter As Long

Dim InputRowCounter As Long

Dim MatchRegion As Long

Dim InputCurrentSiteRowsCount As Long
Dim i As Long

Dim OutputCell As Range

Set InputDataRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").DataBodyRange
Set InputCountryRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").PivotFields("Country").DataRange
Set InputSiteRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").PivotFields("Site").DataRange
Set InputServiceLineRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").PivotFields("Serviceline").DataRange
Set InputCalcHourlySalaryRateRange = ThisWorkbook.Worksheets(Sheet3.Name).PivotTables("PivotTableData").PivotFields("CalcHourlySalaryRate").DataRange
Set InputRegionRange = ThisWorkbook.Worksheets(Sheet4.Name).PivotTables("PivotTableRegion").PivotFields("Country").DataRange
Set OutputDataBodyRange = ThisWorkbook.Worksheets(Sheet2.Name).ListObjects("TableOutput").DataBodyRange
Set OutputHearderRowRange = ThisWorkbook.Worksheets(Sheet2.Name).ListObjects("TableOutput").HeaderRowRange

OutputArrayColumns = InputDataRange.Rows.Count
OutputArrayRows = OutputHearderRowRange.Columns.Count

ReDim Preserve OutputArray(OutputArrayRows, 1)
OutputArrayCounter = 0

If Not OutputDataBodyRange Is Nothing Then

    OutputDataBodyRange.Delete

End If

For InputRowCounter = 1 To InputDataRange.Rows.Count

    If InputSiteRange(InputRowCounter) <> InputSiteRange(InputRowCounter - 1) Then

        OutputArrayCounter = OutputArrayCounter + 1
        ReDim Preserve OutputArray(OutputArrayRows, OutputArrayCounter)


        MatchRegion = Application.WorksheetFunction.Match(InputCountryRange(InputRowCounter), InputRegionRange, 0)
        OutputArray(1, OutputArrayCounter) = InputRegionRange(MatchRegion).Offset(0, -1)
        OutputArray(2, OutputArrayCounter) = InputCountryRange(InputRowCounter)
        OutputArray(3, OutputArrayCounter) = InputSiteRange(InputRowCounter)

        InputCurrentSiteRowsCount = Application.WorksheetFunction.CountIf(InputSiteRange, OutputArray(3, OutputArrayCounter)) - 1

        For i = 0 To InputCurrentSiteRowsCount

            ' *** Landscaping & Irrigation System ***
            If InputServiceLineRange(InputRowCounter + i) = "3.2.3-3.2.4 Landscaping & Irrigation System" Or InputServiceLineRange(InputRowCounter + i) = "Landscaping & Irrigation System - SBTB" Then

                If InputCalcHourlySalaryRateRange(InputRowCounter + i) = "(blank)" Then

                    OutputArray(4, OutputArrayCounter) = OutputArray(4, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1)
                    OutputArray(5, OutputArrayCounter) = OutputArray(5, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2)
                    OutputArray(6, OutputArrayCounter) = OutputArray(6, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3)

                Else

                    OutputArray(4, OutputArrayCounter) = OutputArray(4, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 4)
                    OutputArray(5, OutputArrayCounter) = OutputArray(4, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 5)
                    OutputArray(6, OutputArrayCounter) = OutputArray(4, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 6)

                End If

            End If

            ' *** Interior Plant and Tree Maintenance ***
            If InputServiceLineRange(InputRowCounter + i) = "3.2.11 Interior Plant and Tree Maintenance" Or InputServiceLineRange(InputRowCounter + i) = "Interior Plant and Tree Maintenance - SBTB" Then

                If InputCalcHourlySalaryRateRange(InputRowCounter + i) = "(blank)" Then

                    OutputArray(7, OutputArrayCounter) = OutputArray(7, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1)
                    OutputArray(8, OutputArrayCounter) = OutputArray(8, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2)
                    OutputArray(9, OutputArrayCounter) = OutputArray(9, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3)

                Else

                    OutputArray(7, OutputArrayCounter) = OutputArray(7, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 4)
                    OutputArray(8, OutputArrayCounter) = OutputArray(8, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 5)
                    OutputArray(9, OutputArrayCounter) = OutputArray(9, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 6)

                End If

            End If

            ' *** Interior Pest Control ***
            If InputServiceLineRange(InputRowCounter + i) = "3.3.10 Interior Pest Control" Or InputServiceLineRange(InputRowCounter + i) = "Pest Control - SBTB" Then

                If InputCalcHourlySalaryRateRange(InputRowCounter + i) = "(blank)" Then

                    OutputArray(10, OutputArrayCounter) = OutputArray(10, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1)
                    OutputArray(11, OutputArrayCounter) = OutputArray(11, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2)
                    OutputArray(12, OutputArrayCounter) = OutputArray(12, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3)

                Else

                    OutputArray(10, OutputArrayCounter) = OutputArray(10, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 1) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 4)
                    OutputArray(11, OutputArrayCounter) = OutputArray(11, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 2) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 5)
                    OutputArray(12, OutputArrayCounter) = OutputArray(12, OutputArrayCounter) + InputDataRange(InputRowCounter + i, 3) + InputCalcHourlySalaryRateRange(InputRowCounter + i) * InputDataRange(InputRowCounter + i, 6)

                End If

            End If

        Next i

    End If

Next InputRowCounter

ThisWorkbook.Worksheets(Sheet2.Name).Range("A3:L" & OutputArrayCounter) = Application.WorksheetFunction.Transpose(OutputArray)

For Each OutputCell In OutputDataBodyRange.Cells

    If OutputCell.Value = vbNullString Then

        OutputCell.Value = 0

    End If

Next OutputCell

End Sub

如果需要指明,请告诉我。

2 个答案:

答案 0 :(得分:1)

如果单元格实际为空,则可以跳过循环并使用:

            var binOf2000Words = new List<string>();
            var binOf4To9Words = new List<string)();

            // And at this point you have some code to populate your lists.

            // We now need to cater for the fact that some of the items in the 2000Words bin will actually be strings with more than one word...
            // We'll do away with that by generating a new list that only contains single words.

            binOf2000Words = binOf2000Words.SelectMany(s => s.Split(' ')).Distinct().ToList();

            var result = binOf2000Words.Intersect(binOf4To9Words).Distinct().ToList();

答案 1 :(得分:0)

Nothing关键字不用于查看单元格是否为空,它用于查看变量是否包含其声明的数据类型的默认值,或者为其指定默认值(see this for more information)

尝试以下方法:

For Each OutputCell In OutputDataBordyRange.Cells
    If OutputCell.Value = vbNullString Then
        OutputCell.Value = 0
    End If
Next OutputCell