对于范围r中的每个c,当将值打印到其他工作表时吐出错误?

时间:2018-01-18 23:01:57

标签: excel vba excel-vba

出于某种原因,我不断收到“应用程序或对象定义错误”。尝试运行此循环时发出警告?我似乎无法弄清楚我做错了什么?它作为for循环工作得很好;但是,出于某种原因,每个人似乎都搞砸了。总共有10列,但我只想循环遍历第3到第9列,从第3行第3列开始。有些单元格完全是空的,而其他单元格包含文本,有些甚至可能包含数字(目前不是)。

sub基本上设置了范围,而该函数用于实际测试条件,如果条件为真,则将活动单元格的值与另一个工作表上的附加文本放在一起。

Sub Together()
'~~> all variables not shown here are Public variables
Dim cell As Range

With ActiveSheet
    lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    Set r = ActiveSheet.Range(Cells(3, 3), Cells(lastRow, lastCol - 1))
End With

        For Each c In Range(Cells(3, 3), Cells(lastRow, lastCol - 1))
            Debug.Print c.Address '~~> outputs '$C$3'
            Debug.Print lastRow & ":" & lastCol '~~> Output is correct: 64:10
            Select Case True
                Case Ready_For_Import(c)
            End Select
        Next c


End Sub

Function TogetherFunc(c As Variant)
Dim SR As Worksheet
Set SR = Worksheets("SR")
Debug.Print c.Address
         If Not c.Value = "" And Not IsNumeric(c.Value) Then
         gives error '~~>   Worksheets("SR").Cells(i - 1, 61).Value = "True" + c.Value + ""
         End If


End Function

1 个答案:

答案 0 :(得分:0)

Yowe3k解决了问题,但未提交解决方案。谢谢你!

Sub Together()
Dim cell As Range

  With ActiveSheet
    lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    Set r = ActiveSheet.Range(Cells(3, 3), Cells(lastRow, lastCol - 1))
End With

For Each c In Range(Cells(3, 3), Cells(lastRow, lastCol - 1))
        Debug.Print c.Address 
        Debug.Print lastRow & ":" & lastCol 64:10
        Select Case True
            Case Ready_For_Import(c)
        End Select
    Next c


End Sub

Function TogetherFunc(c As Variant)
    Dim SR As Worksheet
    Set SR = Worksheets("SR")
    Debug.Print c.Address
    If Not c.Value = "" And Not IsNumeric(c.Value) Then
        Worksheets("SR").Cells(**c.row - 1**, 61).Value = "True" + c.Value + ""
    End If
End Function