使用范围时Excel Excel VBA错误1004

时间:2017-07-28 13:36:17

标签: excel vba excel-vba range runtime-error

我在VBA工作,我需要制作512X512的正方形大小(cellsXcells)。 广场假设与细胞的边界。 我制作了方形动态的大小,因此用户可以插入他想要的大小(最大值为512)。

现在我尝试使用几种技术来完成上述操作,但由于错误1004运行时间,我总是失败。

Sub printGrid(gridSize)

Range(Cells(1, 1), Cells(gridSize, gridSize)).Select
With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
End With

With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThick
End With



With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThick
End With

With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
End With

With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
End With

End With

End Sub

我的第二次尝试是逐个细胞地进行...

Sub printBorders(gridSize)
For i = 1 To gridSize ' right side
    Cells(i, 1).Select
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
Next i


For i = 1 To gridSize ' bottom
    Cells(gridSize, i).Select
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
Next i

For i = gridSize To 1 Step -1 ' top
    Cells(1, i).Select
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
Next i

For i = 1 To gridSize ' center
    Cells(i, 64).Select
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
Next i



For i = 1 To gridSize ' left
    Cells(i, gridSize).Select
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
Next i

End Sub

在printBorders中,当我尝试制作左侧网格时,我失败了。 (Cells(i,gridSize).Select)。

我开始认为这在excel中是一种限制。 谢谢你的帮助!

编辑:

当我的意思是动态: 请尝试使用64或512等输入运行它。

Sub main()
Dim gridSize As Integer, numOfDots As Integer
Call setGrid
End Sub

Sub setGrid()
Dim size As Integer
Cells.Select
Selection.Delete Shift:=xlUp

gridSize = setGridSize()

Call printGrid2(1, 1, gridSize, gridSize)
'Call printGrid2(1, 1, gridSize, gridSize / 2)

End Sub

Function setGridSize()
Do While True
    gridSize = InputBox("Enter grid size:")
    If (IsNumeric(gridSize)) And (gridSize Mod 2 = 0) Then
        setGridSize = gridSize
        Exit Do
    End If
Loop
End Function

Sub printGrid2(x, y, rowSize, colSize)
Dim rng As Range
Set rng = Range(Cells(x, y), Cells(rowSize, colSize))


With rng.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
End With

With rng.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThick
End With

With rng.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThick
End With

With rng.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
End With

End Sub

2 个答案:

答案 0 :(得分:2)

这是我运行代码的方式,"动态"。删除Option Explicit后,在新的Excel上尝试不查看选择,它可以工作:

Sub DynamicTest()

    printBorders (10)
    printBorders (20)
    printBorders (30)
    printBorders (InputBox("Dynamically"))

End Sub

Sub printBorders(gridSize As Long)
    For i = 1 To gridSize    ' right side
        Cells(i, 1).Select
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThick
        End With
    Next i


    For i = 1 To gridSize    ' bottom
        Cells(gridSize, i).Select
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThick
        End With
    Next i

    For i = gridSize To 1 Step -1    ' top
        Cells(1, i).Select
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThick
        End With
    Next i

    For i = 1 To gridSize    ' center
        Cells(i, 64).Select
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThick
        End With
    Next i



    For i = 1 To gridSize    ' left
        Cells(i, gridSize).Select
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThick
        End With
    Next i

End Sub

这是我改变的:

自: Sub printBorders(gridSize)

要: Sub printBorders(gridSize As Long)

或者您可以在InputBox中要求输入数字,如下所示: InputBox("Dynamically", Type:=1)。 (感谢 David Zemens

通常,这就是出现错误的原因:

Cells在VBA中占用parameter overloading。这意味着,您可以使用以下任何一个来引用单元格:

Cells(Long, Long) - > Cells(1,1)

Cells(Long, String) - > Cells(1,"A")

gridSizeString,但在for-loop中使用它是没有问题的,因为它内部转换为数字值。

但是,当您尝试选择Cells(i, gridSize)时,VBA首先会查找函数Cells(Long, String)。它期望String是列名。但是,您没有名为111的列,因此会引发错误。 enter image description here

答案 1 :(得分:1)

如果我正确阅读此内容,您只需要绘制一个带有周围边框的方格。您不必执行单独的操作。

Option Explicit

Sub test()
    printGrid 6
End Sub

Sub printGrid(gridSize)
    With Worksheets("sheet1")
        With .Cells(1, 1).Resize(gridSize, gridSize)
            .BorderAround LineStyle:=xlContinuous, Weight:=xlThick
        End With
    End With
End Sub

录制的代码通常执行的操作远远超出必要的范围。最好把它砍成实际需要的东西。

enter image description here