VBA子函数调用时间检查线Down错误

时间:2018-11-01 10:58:16

标签: excel vba excel-vba

我在vba中有默认的测试功能调用。 vba BICO()函数中的数据首次加载成功完成数据加载,但行未淹没在第一张工作表中。 第二个功能是调用数据加载的数据,但第二个功能中的线路被淹 这是我的错误。 我的代码中分享了如何出售我的阔幅织机。任何解决方案或参考。

Sub test()
    Dim im, jm As Integer
    im = 1
    jm = 2
    Call BICO(i)
    Call ssp(j)
End Sub

Sub ssp(im)
    Dim i As Integer
    i = 2
    Dim a1 As String
    Dim a2 As String
    a1 = "A" & i
    a2 = "C" & i

    Range(a1, a2).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone

    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With

    Sheets(1).Cells(2, 1).Value = "Saravanan"
    Sheets(1).Cells(2, 2).Value = "Saravanan  S/No Ramsing, Pondichary -60089"
    Sheets(1).Cells(2, 3).Value = "9791709616"
End Sub

Sub BICO(jm)
    Dim i As Integer
    i = 2
    Dim a1 As String
    Dim a2 As String
    a1 = "A" & i
    a2 = "C" & i

    Range(a1, a2).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone

    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With

    Sheets(2).Cells(2, 1).Value = "Saravanan"
    Sheets(2).Cells(2, 2).Value = "Saravanan  S/No Ramsing, Pondichary -60089"
    Sheets(2).Cells(2, 3).Value = "9791709616"
End Sub

我需要两张纸使用vba淹没生产线。 enter image description here

数据未显示行

enter image description here

这是皮棉,它现在可以淹死了。

需要sheet1和sheet2划过网格线。

1 个答案:

答案 0 :(得分:3)

您遇到的问题是由于您不符合为边框选择的工作表的资格,下面更改的代码将为您指明正确的方向。

此刻,您的代码将在您使用Range(a1, a2)时简单地查看ActiveSheet,而不会告诉代码您所指的是哪个工作表,因此,实际上,代码是有效的,但是在边框上绘制了两次边框相同的工作表,下面修改的代码将按照我指定要更新的工作表的方式工作。

Sub test()
    Dim i As Long, j As Long
    i = 2
    j = 2
    Call BICO(i)
    Call ssp(j)
End Sub

Sub ssp(im)
    Dim wsSSP As Worksheet: Set wsSSP = ThisWorkbook.Sheets(1)
    Dim a1 As String
    Dim a2 As String
    a1 = "A" & im
    a2 = "C" & im

    wsSSP.Range(a1, a2).Borders(xlDiagonalDown).LineStyle = xlNone
    wsSSP.Range(a1, a2).Borders(xlDiagonalUp).LineStyle = xlNone
    With wsSSP.Range(a1, a2).Borders(xlEdgeLeft)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With wsSSP.Range(a1, a2).Borders(xlEdgeBottom)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With wsSSP.Range(a1, a2).Borders(xlEdgeRight)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With wsSSP.Range(a1, a2).Borders(xlInsideVertical)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    wsSSP.Cells(2, 1).Value = "Saravanan"
    wsSSP.Cells(2, 2).Value = "Saravanan  S/No Ramsing, Pondichary -60089"
    wsSSP.Cells(2, 3).Value = "9791709616"
End Sub

Sub BICO(jm)
    Dim wsBICO As Worksheet: Set wsBICO = ThisWorkbook.Sheets(2)
    Dim a1 As String
    Dim a2 As String
    a1 = "A" & jm
    a2 = "C" & jm

    wsBICO.Range(a1, a2).Borders(xlDiagonalDown).LineStyle = xlNone
    wsBICO.Range(a1, a2).Borders(xlDiagonalUp).LineStyle = xlNone
    With wsBICO.Range(a1, a2).Borders(xlEdgeLeft)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With wsBICO.Range(a1, a2).Borders(xlEdgeBottom)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With wsBICO.Range(a1, a2).Borders(xlEdgeRight)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With wsBICO.Range(a1, a2).Borders(xlInsideVertical)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    wsBICO.Cells(2, 1).Value = "Saravanan"
    wsBICO.Cells(2, 2).Value = "Saravanan  S/No Ramsing, Pondichary -60089"
    wsBICO.Cells(2, 3).Value = "9791709616"
End Sub

更新:

为使代码更有用,当您为不同的工作表重复代码时,可以修改Sub,以将工作表作为参数,以便您可以调用相同的sub来影响不同的工作表,请看下面的代码:

Sub test()
    Dim i As Long
    i = 2
    Call DrawBorder(i, ThisWorkbook.Sheets(1))
    Call DrawBorder(i, ThisWorkbook.Sheets(2))
End Sub

Sub DrawBorder(im As Long, ws As Worksheet)
    Dim a1 As String, a2 As String
    a1 = "A" & im
    a2 = "C" & im

    ws.Range(a1, a2).Borders(xlDiagonalDown).LineStyle = xlNone
    ws.Range(a1, a2).Borders(xlDiagonalUp).LineStyle = xlNone

    With wsSSP.Range(a1, a2).Borders(xlEdgeLeft)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With ws.Range(a1, a2).Borders(xlEdgeBottom)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With ws.Range(a1, a2).Borders(xlEdgeRight)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With ws.Range(a1, a2).Borders(xlInsideVertical)
        .LineStyle = xlSolid
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With

    ws.Cells(2, 1).Value = "Saravanan"
    ws.Cells(2, 2).Value = "Saravanan  S/No Ramsing, Pondichary -60089"
    ws.Cells(2, 3).Value = "9791709616"
End Sub