Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ActiveCell.Value运行时发生VBA类型不匹配错误<&燃气轮机&引用&引用;_Vba_Excel - Fatal编程技术网

ActiveCell.Value运行时发生VBA类型不匹配错误<&燃气轮机&引用&引用;

ActiveCell.Value运行时发生VBA类型不匹配错误<&燃气轮机&引用&引用;,vba,excel,Vba,Excel,嘿,我有代码将行移动到另一个带有单元格名称的工作表中,一个循环,直到它碰到数据末尾的空白,这里是代码的摘录 Range("AF2").Select Do While ActiveCell.Value <> "" strDestinationSheet = ActiveCell.Value ActiveCell.Offset(0, -31).Resize(1, ActiveCell.CurrentRegion.Columns.count).Select Sele

嘿,我有代码将行移动到另一个带有单元格名称的工作表中,一个循环,直到它碰到数据末尾的空白,这里是代码的摘录

Range("AF2").Select
Do While ActiveCell.Value <> ""
    strDestinationSheet = ActiveCell.Value
    ActiveCell.Offset(0, -31).Resize(1, ActiveCell.CurrentRegion.Columns.count).Select
    Selection.Copy

    Sheets(strDestinationSheet).Select
    N = Cells(Rows.count, "AF").End(xlUp).Row
    lastRow = N

    Cells(lastRow + 1, 1).Select
    Selection.PasteSpecial xlPasteValues
    Application.CutCopyMode = False

    Sheets(strSourceSheet).Select
    ActiveCell.Offset(0, 31).Select
    ActiveCell.Offset(1, 0).Select
Loop
范围(“AF2”)。选择
当ActiveCell.Value“”时执行此操作
strDestinationSheet=ActiveCell.Value
偏移量(0,-31)。调整大小(1,ActiveCell.CurrentRegion.Columns.count)。选择
选择,复制
工作表(标准工作表)。选择
N=单元格(Rows.count,“AF”).End(xlUp).Row
lastRow=N
单元格(lastRow+1,1)。选择
Selection.Paste特殊XLPaste值
Application.CutCopyMode=False
工作表(strSourceSheet)。选择
ActiveCell.Offset(0,31)。选择
ActiveCell.Offset(1,0)。选择
环

然而,虽然这部分代码以前在最新的运行实例上运行得很好,但现在在第二行Do while ActiveCell.Value“”上抛出了一个错误,表示类型不匹配。我不确定是什么改变突然停止了工作,有什么想法吗?非常感谢。

我个人不喜欢循环遍历一组单元格。我更喜欢为每个循环选择一个循环

正如@bruceWayne所述,避免使用按原样选择会降低代码的速度

适当的缩进使代码更容易阅读,避免简单的错误

Dim cel As Range
With Sheets(strSourceSheet)
    For Each cel In .Range("AF2", .Range("AF2").End(xlDown))
        If Not IsError(cel) Then
            strDestinationSheet = cel.Value
            cel.Offset(0, -31).Resize(1, cel.CurrentRegion.Columns.Count).Copy
            N = Sheets(strDestinationSheet).Cells(Sheets(strDestinationSheet).Rows.Count, "AF").End(xlUp).Row
            Sheets(strDestinationSheet).Cells(N + 1, 1).PasteSpecial xlPasteValues
        End If
    Next cel
End With

我个人不喜欢循环遍历一组单元格。我更喜欢为每个循环选择一个循环

正如@bruceWayne所述,避免使用按原样选择会降低代码的速度

适当的缩进使代码更容易阅读,避免简单的错误

Dim cel As Range
With Sheets(strSourceSheet)
    For Each cel In .Range("AF2", .Range("AF2").End(xlDown))
        If Not IsError(cel) Then
            strDestinationSheet = cel.Value
            cel.Offset(0, -31).Resize(1, cel.CurrentRegion.Columns.Count).Copy
            N = Sheets(strDestinationSheet).Cells(Sheets(strDestinationSheet).Rows.Count, "AF").End(xlUp).Row
            Sheets(strDestinationSheet).Cells(N + 1, 1).PasteSpecial xlPasteValues
        End If
    Next cel
End With

您可能在其中一个单元格中出错。感谢Scott,专栏中确实有一些“N/A”,我是否可以在遇到这些错误时设置一个要跳过的错误陷阱?如果你适当地缩进代码,你的代码将更容易理解/阅读和维护/调试。你可能在其中一个单元格中有一个错误。感谢Scott,专栏中确实有一些#N/As,我能在遇到这些错误时设置一个错误陷阱跳过吗?如果你正确地缩进代码,你的代码将更容易理解/阅读和维护/调试。