【发布时间】:2018-01-04 14:37:48
【问题描述】:
我在 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
【问题讨论】:
-
工作表或其任何部分是否受保护?如果是这样,您可能无法选择或执行某些操作。另外,it's generally advisable to avoid relying on
SelectorActivate. -
所以不要使用
Cells(i, 1).Select和With Selection...,只需使用With Cells(i, 1).Borders(xlEdgeLeft)。但如果工作表受到保护,您仍然可能无法做到这一点。 -
Cells(i, 64).Select的想法是什么?它转到列BL? -
虽然使用正确分配的范围变量是个好主意,但在这种情况下似乎不太可能解决问题。 1004 通常表示保护问题,或者您定义了一个不可访问的对象(例如,
Cells(0, 2)将失败,因为它不/不能存在。 -
值得注意的是,当我在空白工作簿中运行代码时,您的代码不会引发任何错误(除了编译错误,可以通过正确声明变量来解决)。
标签: vba excel range runtime-error