我是VBA的新手,昨天开始对其进行编程。我正在编写一个数据处理程序,该程序需要跟踪两个单元格,每个电子表格上一个。下面的代码再现了我遇到的错误。当我在sub Processor()中调用sub moveCell()时,DIRow和DIColumn没有任何反应,并且代码在指示的行吐出了错误1004。我曾尝试使用DICell = DICell.Offset(),但它会返回相同的错误。
如何将Range变量重新定义为其他单元格?
'<<Main Processor Code>>'
Sub Processor()
Dim PDRow As Integer
Dim PDColumn As Integer
Dim DIRow As Integer
Dim DIColumn As Integer
PDRow = 1
PDColumn = 1
DIRow = 1
DIColumn = 1
Dim PDCell As Range
Dim DICell As Range
Set PDCell = Worksheets("Processed Data").Cells(PDRow, PDColumn)
Set DICell = Worksheets("Data Input").Cells(DIRow, DIColumn)
Call moveCell(2, 0, "Data Input")
End Sub
'<<Function which moves the cell which defines the range>>'
Sub moveCell(r As Integer, c As Integer, sheet As String)
If sheet = "Processed Data" Then
PDRow = PDRow + r
PDColumn = PDColumn + c
Set PDCell = Worksheets("Data Input").Cells(PDRow, PDColumn)
ElseIf sheet = "Data Input" Then
DIRow = DIRow + r '<<<<<<This line does nothing to DIRow's value
DIColumn = DIColumn + c
Set DICell = Worksheets("Data Input").Cells(DIRow, DIColumn) '<<<<<<This line causes error 1004
End If
End Sub
答案 0 :(得分:1)
据我所知,您可以改用快速功能。 fitFunc
函数的If
语句结果似乎与您使用的是哪个工作表没有什么区别。
我们可以通过参考传递给moveCell()
的{{1}}来简化此过程。
Range
我已经包含了一些不需要的行(此后我已经用注释标记了),但这将向您展示例程的工作方式。您可以逐步使用 F8 逐步了解它。
编辑:尽管,您根本不需要单独的功能。只需使用OFFSET()
。
moveCell