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