将文本格式设置为粗体并插入公式以获得总和

时间:2015-11-22 01:20:58

标签: excel vba

enter image description here

以上是以下代码的作用。

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim i As Long
    Dim k As Long
    Set ws = ActiveSheet

    With ws

        For i = 1 To 200
            If Left(.Cells(i, 1).Value, 2) = "HW" Then
                On Error Resume Next
                k = .Range(.Cells(i + 1, 1), .Cells(200, 1)).Find("HW").Row
                On Error GoTo 0
                If k <= i Then k = 200
                .Cells(i, 10).Value = Len(Cells((i + 2), 1).Value) - Len(Replace(Cells((i + 2), 1).Value, ",", "")) + 1
                .Cells(i, 11).Value = "SET"
                .Cells(i, 12).Resize(k - i).Value = .Cells(i, 1).Resize(k - i).Value
                .Cells((i + 1), 12).Resize(k - i).Value = .Cells((i + 1), 1).Resize(k - i).Value
                .Cells((i + 2), 12).Resize(k - i).Value = ws.Cells((i + 2), 1).Resize(k - i).Value
                .Cells((i + 3), 12).Resize(k - i).Value = ""
                .Cells((i + 4), 12).Resize(k - i).Value = "QTY"
                .Cells((i + 4), 13).Resize(k - i).Value = "TYPE"
                .Cells((i + 4), 15).Resize(k - i).Value = "LENGTH"
                .Cells((i + 4), 16).Resize(k - i).Value = "FINISH"
                .Cells((i + 4), 19).Resize(k - i).Value = "LIST"
                .Cells((i + 4), 20).Resize(k - i).Value = "NET"
                .Cells((i + 4), 21).Resize(k - i).Value = "MFG"
                .Cells((i + 4), 22).Resize(k - i).Value = "MODEL"

                .Cells((i + 5), 12).Resize(k - i).Value = .Cells((i + 3), 1).Resize(k - i).Value
                .Cells((i + 5), 13).Resize(k - i).Value = .Cells((i + 3), 2).Resize(k - i).Value
                .Cells((i + 5), 15).Resize(k - i).Value = .Cells((i + 3), 5).Resize(k - i).Value
                .Cells((i + 5), 16).Resize(k - i).Value = .Cells((i + 3), 6).Resize(k - i).Value
                .Cells((i + 5), 19).Resize(k - i).Value = .Cells((i + 3), 7).Resize(k - i).Value
                .Cells((i + 5), 20).Resize(k - i).Value = .Cells((i + 3), 8).Resize(k - i).Value
                .Cells((i + 5), 21).Resize(k - i).Value = .Cells((i + 3), 3).Resize(k - i).Value
                .Cells((i + 5), 22).Resize(k - i).Value = .Cells((i + 3), 4).Resize(k - i).Value

                i = k + 1
            End If
        Next i
    End With
End Sub

一些问题。首先我不确定为什么但是对于第二个数据输出它缺少门,SET和所有不同的硬件。看起来它正在跳过它?

第二个问题是我不知道如何使用VBA代码使我的标题(QTY,TYPE,LENGTH,FINISH,LIST,NET,MFG,MODEL)变粗。我想我会使用text.bold,但我不认为我知道如何正确地说出来。我还想在它们下面放一条双线,包括N列,但不包括Q和R.

第三,我想在.NET列的末尾总结我的NET价格,但我不知道如何指定该单元格。我还希望它右边的单元格将网格的总和除以特定的单元格。

第四,我试过这样做,

"DOOR: " & ws.Cells((i + 2), 1).Resize(k - i).Value

这会触发错误,因为一个是字符串而另一个是整数。我以为我可以使用CStr(),但这不起作用。

当所有编码正确时,我希望它看起来像这样。

enter image description here

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

我会将所有标题行移动到数组中。然后你可以调整区域大小并分配数组。

关于你的问题:

1)数学,当添加行并引用不添加行的数据时,还有很多数学。你基本上都在覆盖数据。

2)格式化粗体的一种方法是Range.Font.Bold = True。因此,边界类似Range.Borders(XlEdgeBottom).LineStyle = xlDouble

3)再次抽签和大量数学,有时候试错是为了正确。

4)你不能用调整大小来做,它不喜欢它

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim i As Long
    Dim k As Long
    Set ws = ActiveSheet
    Dim ofst As Long
    Dim ttlArr() As String
    ttarr = Array("QTY", "TYPE", vbNullString, "LENGTH", "FINISH", vbNullString, vbNullString, "LIST", "NET", "MFG", "MODEL")
    ofst = 0
    With ws

        For i = 1 To 200
            If Left(.Cells(i, 1).Value, 2) = "HW" Then

                On Error Resume Next
                    k = .Range(.Cells(i + 1, 1), .Cells(200, 1)).Find("HW").Row
                On Error GoTo 0
                If k <= i Then k = .Cells(i, 1).End(xlDown).Row + 2
                .Cells(i + ofst, 11).Value = "SET"
                .Cells(i + ofst, 12).Resize(2).Value = .Cells(i, 1).Resize(2).Value
                If IsNumeric(.Cells((i + 2), 1).Value) Then
                    .Cells(i + ofst, 10).Value = Len("'" & Format(.Cells(i + 2, 1).Value, "#,##0")) - Len(Replace("'" & Format(.Cells(i + 2, 1).Value, "#,##0"), ",", "")) + 1
                    .Cells(i + ofst + 2, 12).Value = "Doors: " & Format(.Cells(i + 2, 1).Value, "#,##0")
                Else
                    .Cells(i + ofst, 10).Value = Len(.Cells(i + 2, 1).Value) - Len(Replace(.Cells(i + 2, 1).Value, ",", "")) + 1
                    .Cells(i + ofst + 2, 12).Value = "Doors: " & .Cells(i + 2, 1).Value
                End If
                .Cells(i + ofst + 4, 12).Resize(, UBound(ttarr) + 1).Value = ttarr
                .Cells(i + ofst + 4, 12).Resize(, UBound(ttarr) + 1).Font.Bold = True
                .Cells(i + ofst + 4, 12).Resize(, 5).Borders(xlEdgeBottom).LineStyle = xlDouble
                .Cells(i + ofst + 4, 19).Resize(, 4).Borders(xlEdgeBottom).LineStyle = xlDouble
                ofst = ofst + 2
                .Cells(i + ofst + 3, 12).Resize(k - i - 3).Value = .Cells(i + 3, 1).Resize(k - i - 3).Value
                .Cells(i + ofst + 3, 13).Resize(k - i - 3).Value = .Cells(i + 3, 2).Resize(k - i - 3).Value
                .Cells(i + ofst + 3, 15).Resize(k - i - 3).Value = .Cells(i + 3, 5).Resize(k - i - 3).Value
                .Cells(i + ofst + 3, 16).Resize(k - i - 3).Value = .Cells(i + 3, 6).Resize(k - i - 3).Value
                .Cells(i + ofst + 3, 19).Resize(k - i - 3).Value = .Cells(i + 3, 7).Resize(k - i - 3).Value
                .Cells(i + ofst + 3, 20).Resize(k - i - 3).Value = .Cells(i + 3, 8).Resize(k - i - 3).Value
                .Cells(i + ofst + 3, 21).Resize(k - i - 3).Value = .Cells(i + 3, 3).Resize(k - i - 3).Value
                .Cells(i + ofst + 3, 22).Resize(k - i - 3).Value = .Cells(i + 3, 4).Resize(k - i - 3).Value
                .Cells(i + ofst + k - i - 1, 20).Value = WorksheetFunction.Sum(.Cells(i + ofst + 3, 20).Resize(k - i - 4))
                ' Change the Range("H1") to your cell with the factor
                .Cells(i + ofst + k - i - 1, 21).Value = .Cells(i + ofst + k - i - 1, 20).Value / .Range("H1")
                .Cells(i + ofst - 2, 17).Value = .Cells(i + ofst + k - i - 1, 21).Value

                .Cells(i + ofst - 2, 18).Value = .Cells(i + ofst + k - i - 1, 21).Value * .Cells(i + ofst - 2, 10).Value
                i = k - 1
            End If
        Next i
    End With
End Sub

答案 1 :(得分:0)

我认为数据丢失的问题与查找 HW * 的最后一次出现有关,当没有终止 HW * 来查找记录结束时。没有看到一个或两个 HW 记录作为样本,这是我能想到的最好的。

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim i As Long, k As Long, hw As Long, MX As Long

    Set ws = ActiveSheet

    With ws
        MX = 200   'maybe MX = .cells(rows.count, 1).end(xlup).row
        i = .Columns(1).Find(what:="HW*", after:=.Cells(MX, 1), lookat:=xlWhole).Row
        k = .Columns(1).FindNext(after:=.Cells(i, 1)).Row
        For hw = 1 To Application.CountIf(.Columns(1), "HW*")
            If k <= i Then k = MX
            Debug.Print i & ":" & k

            .Cells(i, 10) = UBound(Split(.Cells(i + 2, 1).Value, Chr(44))) + 1
            .Cells(i, 11).Value = "SET"
            .Cells(i, 12).Resize(k - i).Value = .Cells(i, 1).Resize(k - i).Value

            With .Cells(i + 4, 12)
                .Resize(1, 11) = Array("QTY", "TYPE", vbNullString, _
                                       "LENGTH", "FINISH", vbNullString, vbNullString, _
                                       "LIST", "NET", "MFG", "MODEL")
                With Union(.Cells(1, 1).Resize(1, 5), .Cells(1, 1).Resize(1, 5))
                    .Font.Bold = True
                    With .Borders(xlEdgeBottom)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                    End With
                End With
            End With


            .Cells((i + 5), 12).Resize(k - i).Value = .Cells((i + 3), 1).Resize(k - i).Value
            .Cells((i + 5), 13).Resize(k - i).Value = .Cells((i + 3), 2).Resize(k - i).Value
            .Cells((i + 5), 15).Resize(k - i).Value = .Cells((i + 3), 5).Resize(k - i).Value
            .Cells((i + 5), 16).Resize(k - i).Value = .Cells((i + 3), 6).Resize(k - i).Value
            .Cells((i + 5), 19).Resize(k - i).Value = .Cells((i + 3), 7).Resize(k - i).Value
            .Cells((i + 5), 20).Resize(k - i).Value = .Cells((i + 3), 8).Resize(k - i).Value
            .Cells((i + 5), 21).Resize(k - i).Value = .Cells((i + 3), 3).Resize(k - i).Value
            .Cells((i + 5), 22).Resize(k - i).Value = .Cells((i + 3), 4).Resize(k - i).Value

            i = .Columns(1).FindNext(after:=.Cells(k - 1, 1)).Row
            k = .Columns(1).FindNext(after:=.Cells(i, 1)).Row
        Next hw

    End With
End Sub

答案 2 :(得分:0)

rake db:migrate

结束 结束子