将cell.value从范围传递到另一个子对象的正确方法是什么?

时间:2019-05-13 13:17:22

标签: excel vba range cell

我有以下代码:

Dim WS1, WS2 As Worksheet
Dim chatRange As Range
Dim cell As Range
Dim txt As String

Sub NameTest()

    Set WS1 = ActiveWorkbook.Sheets("Page 1")
    Set WS2 = ActiveWorkbook.Sheets("Sheet1")
    x = 2
    lRow1 = WS1.Cells(Rows.Count, "B").End(xlUp).Row

    Set chatRange = WS1.Range("B" & x, "B" & lRow1)

    For Each cell In chatRange
        If cell.Offset(0, 11).Value = "Accepted" Then
            txt = cell.Offset(0, 18).Value
            NameSplit
        End If

    Next cell

End Sub

Sub NameSplit()
    Dim i As Integer
    Dim FullName As Variant
    Dim x As String, cell As Range
    Dim lRow2 As Long

    FullName = RemoveBlankLines(Split(txt, vbLf))
    lRow2 = WS2.Cells(Rows.Count, 2).End(xlUp).Row + 1
    WS2.Cells(lRow2, 1).Value = cell.Value                 <===================
    WS2.Cells(lRow2, 2).Value = cell.Offset(0, 2).Value    <===================
    WS2.Cells(lRow2, 3).Value = cell.Offset(0, 6).Value    <===================
    WS2.Cells(lRow2, 4).Value = cell.Offset(0, 18).Value   <===================
End Sub

这是一个简单的测试代码的一部分,我正在编写该代码以读取一张纸,然后将所需的信息传递给另一张纸。我希望能够使用在第一个子目录中设置的范围内的“单元格”中的信息来传输第二个子目录中的详细信息。它可以与在顶部声明的WS1和WS2一起使用,但是在范围上并不能做到相同。

当单元格.value返回时,带有箭头的4行是我努力工作的部分:

Run-time error '91': Object variable or With block variable not set

我知道最好的方法是将所有内容放在1个子目录中,但我想尽可能地将其分开

提前欢呼

2 个答案:

答案 0 :(得分:3)

始终使用Option Explicit,尽管在这种情况下它不会突出显示问题。

如前所述,您将cell声明为范围(在NameSplit函数中第二次),但从未设置。这就是您至少得到该错误的原因。

正如其他人所提到的,如果您首先声明它们,则可以轻松地将这些值传递到另一个子中。查看基于您的示例(未试用)代码:

Option Explicit
'Use global variables only if really needed, but was a good try.

Sub NameTest()
    Dim WS1 As Worksheet, WS2 As Worksheet
    Dim chatRange As Range
    Dim cell As Range
    Dim txt As String

    Set WS1 = ActiveWorkbook.Sheets("Page 1")
    Set WS2 = ActiveWorkbook.Sheets("Sheet1")

    x = 2

    lRow1 = WS1.Cells(WS1.Rows.Count, "B").End(xlUp).row

    Set chatRange = WS1.Range("B" & x, "B" & lRow1)

    For Each cell In chatRange
        If cell.Offset(0, 11).Value = "Accepted" Then
            txt = cell.Offset(0, 18).Value
            Call NameSplit(WS2, cell, txt)
        End If

    Next cell

End Sub

Sub NameSplit(wsDest As Worksheet, rngCell As Range, strTxt As String)    'You can pass any objects through. By default they are passed ByRef (search for ByRef vs ByVal)
    Dim i As Integer
    Dim FullName As Variant
    Dim x As String
    Dim lRow2 As Long

    FullName = RemoveBlankLines(Split(strTxt, vbLf))
    lRow2 = WS2.Cells(WS2.Rows.Count, 2).End(xlUp).row + 1

    wsDest.Cells(lRow2, 1).Value = rngCell.Value
    wsDest.Cells(lRow2, 2).Value = rngCell.Offset(0, 2).Value
    wsDest.Cells(lRow2, 3).Value = rngCell.Offset(0, 6).Value
    wsDest.Cells(lRow2, 4).Value = rngCell.Offset(0, 18).Value
End Sub

编辑:对Rows.CountWS1.Rows.Count分别进行WS2.Rows.Count child: MaterialApp( title: 'Test App', debugShowCheckedModeBanner: false, theme: ThemeData( brightness: Brightness.light, primaryColor: Colors.teal[200], // All text is black primaryColor: Colors.teal[800], // ALL text is white ), 的完全限定,以防止出错,并更正注释中的默认值。 (感谢@chris neilsen指出)。

答案 1 :(得分:1)

您的upload过程应这样声明:

NameSplit()

应该这样称呼:

Sub NameSplit(WS2 as Worksheet, cell as Range)
'
'
'your code here
'
'
End Sub

您可以从call NameSplit(WS2, cell) 的声明中删除cell as Range