使用范围时Excel VBA错误1004
我在VBA工作,我需要做一个512X512(cellsXcells)的正方形大小。 广场假设与细胞的边界。 我做了dynamic的大小,所以用户可以插入他想要的大小(最大为512)。
现在我尝试了很less的技术来做到上面的事情,但总是因为错误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的一种限制。 谢谢您的帮助!
编辑:
当我的意思是dynamic的:请尝试运行它像64或512input。
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
这是我如何运行代码,“dynamic”。 删除Option Explicit
,在新的Excel上,并试图不看看select,它的工作原理:
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)
To: Sub printBorders(gridSize As Long)
或者您可以在InputBox中input一个数字input: InputBox("Dynamically", Type:=1)
。 ( 感谢David Zemens )
一般来说,这就是错误出现的原因:
Cells
在VBA中使用参数重载 。 这意味着,您可以使用以下任何一种方式引用单元格:
Cells(Long, Long)
– > Cells(1,1)
Cells(Long, String)
– > Cells(1,"A")
gridSize
是一个String
,但是在for-loop
使用它是没有问题的,因为它在内部被转换为一个Numeric值。
但是,当您尝试selectCells(i, gridSize)
,VBA首先查找函数Cells(Long, String)
。 它期望String
是一个列名称。 但是,您没有名为111
的列,因此会引发错误。
如果我正确地阅读这个,你想要做的就是绘制一个与周围边界的正方形网格。 您不必执行单独的操作。
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
logging的代码通常执行得比所需要的多得多。 最好把它切成实际需要的东西。