关于Excel的VBA代码问题!!!在线等

2024-12-15 16:17:55
推荐回答(1个)
回答1:

如果sheet1表格都已经设置好了,建议直接用函数去完成。

如果用VBA的话,就不用先设置好sheet1的格式,而是全部由程序自动来完成。

以后者为例,代码:

Public n As Integer, rng As Range, rag As Range
Private Sub CommandButton1_Click()
With Worksheets("Sheet1")
    With .Cells
        .Font.ColorIndex = xlAutomatic
        .Interior.Pattern = xlNone
        .Interior.TintAndShade = 0
        .Interior.PatternTintAndShade = 0
    End With
.[A:C].Delete shift = xlToLeft
st = [b1]
sr = [c1]
n = 1
For i = 2 To [a2].End(xlDown).Row
        .Cells(n, 1) = Cells(i, 1)
        .Cells(n + 1, 2) = "项目"
        .Cells(n + 1, 3) = "数量"
        .Cells(n + 2, 1) = "编号"
        .Cells(n + 2, 2) = st
        .Cells(n + 2, 3) = Cells(i, 2)
        .Cells(n + 3, 2) = sr
        .Cells(n + 3, 3) = Cells(i, 3)
        .Cells(n + 5, 1) = "备注"
        .Range(.Cells(n + 5, 2), .Cells(n + 5, 3)).Merge
        .Cells(n + 7, 1) = "制单:"
        .Cells(n + 7, 2) = "审核:"
    Set rng = .Range(.Cells(n, 1), .Cells(n + 7, 3))
    Call 格式化
    .Range(.Cells(n + 2, 2), .Cells(n + 3, 3)).Font.Color = -16776961
    n = n + 9
Next
End With
End Sub

=============