简体   繁体   中英

Excel VBA error 1004 when using range

I have a work in VBA where I need to make a square size of 512X512 (cellsXcells). The square suppose to be with the borders of the cells. I made the size of the square dynamic so user can insert the size he wants (max is 512).

Now I tried with few techniques to do the above but always I fail because of error 1004 run time.

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

My second attempt was to do it cell by cell...

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

At printBorders I fail when I try to make the left grid. (Cells(i, gridSize).Select).

I am starting to think that this is sort kind of a limit in excel. Thanks for the help!

EDIT:

When I mean dynamic: Please trying running it with inputs like 64 or 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

This is how I run the code, "dynamically". After removing Option Explicit , on a new Excel and trying not to look at the selections, it works:

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

This is what I have changed:

From: Sub printBorders(gridSize)

To: Sub printBorders(gridSize As Long)

Or you may ask for a numeric input in the InputBox like this: InputBox("Dynamically", Type:=1) . (Credit to David Zemens )

In general, this is why the error appears:

The Cells takes parameter overloading in VBA. This means, that you can refer to Cells with any of these two:

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

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

gridSize is a String , but it is no problem to be used in a for-loop as such, because it is internally casted to a Numeric value.

However, when you try to select Cells(i, gridSize) , VBA looks for the function Cells(Long, String) first. It expects that the String is a column name. However, you do not have a column named 111 , thus it throws an error. 在此处输入图片说明

If I am reading this correctly, all you want to do is draw a square grid with surrounding borders. You do not have to perform the individual operations.

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

Recorded code often performs much more than is necessary. It is best to chop it down to what is actually needed.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM