将下一个空多单元格范围分配给函数返回的变量

时间:2015-05-14 18:34:58

标签: excel vba excel-vba

我正在尝试使用函数使用工作表中的下一个空行返回仅1 x 4大小的Range对象,这样我就可以在不同的工作表上将值从一个范围传输到另一个范围。不幸的是,我似乎无法解决这个问题。我试图寻找答案,但我能找到的只有1个细胞解决方案。

Private Function AssignNextRow(project As String) As Range

    Dim projectSheet As Worksheet
    Set projectSheet = Worksheets(project)
    Dim firstEmptyRow As Range
    Set firstEmptyRow = projectSheet.Range(Cells("A" & projectSheet.Rows.count) _
      .End(xlUp).Offset(1, 0), Cells("D" & projectSheet.Rows.count).End(xlUp).Offset(1, 0))
    AssignNextRow = firstEmptyRow

End Function

那么将Range设置为变量的最佳方法是什么?

修改

我已经修复了它将运行的代码,但现在下一个工作表范围的开始是偏移的,以便在最后一个工作表的范围结束时开始。新代码如下:

Private Function AssignNextRow(project As String) As Range

    Dim projectSheet As Worksheet
    Set projectSheet = Worksheets(project)
    Dim firstEmptyRow As Range
    Set firstEmptyRow = projectSheet.Range("A" & projectSheet.Rows.count) _
      .End(xlUp).Offset(1, 0).Resize(1, 4)
    Set AssignNextRow = firstEmptyRow

End Function

我不确定在哪里解决这个问题。

2ND编辑:

  

"下一张表"和#34;最后一张"究竟?您可能需要提供调用AssignNextRow()-Tim Williams

的代码
Private Sub DailyTransfer()
    Dim dailyRange As Variant
    dailyRange = Worksheets("Daily").Range("A3:D23").Value
    Dim x As Integer, y As Integer, z As Integer, projectName As String
    Dim entryDate As Date
    x = 1
    y = 1
    z = 1
    For x = 1 To 20
        projectName = dailyRange(x, 2)
        entryDate = dailyRange(x, 1)
        If projectName = "" Then Exit For
        If WkshtCheck(projectName) = False Then Call WkshtMake(projectName, entryDate)
        For y = 1 To 4
            Dim emptyRow As Range
            Set emptyRow = AssignNextRow(projectName)
            emptyRow(1, z).Value = dailyRange(x, y)
            If y = 1 Then y = y + 1
            z = z + 1
        Next y
    Next x
End Sub

1 个答案:

答案 0 :(得分:0)

只要ColA中的值始终为

,就应该这样做
Set firstEmptyRow = projectSheet.Range(Cells("A" & projectSheet.Rows.count) _
  .End(xlUp).Offset(1, 0).Resize(1,4)