为什么在我的代码中不能使用多种“ NextFree = Range”语言?

时间:2019-02-15 19:20:16

标签: excel vba

我正在用Excel编写VBA代码,将公式从数据的最后一行复制到它的下一行,然后复制该最后一行(现在倒数第二行)并粘贴为值。我想为多张纸做这个。问题在于,它在第一张纸上正常工作后,会在下一张纸上(可能是其余的纸上)出错。

该代码适用于第一个工作表,但是当它移到下一个工作表时,Excel给我一个“运行时错误'1004':找不到单元格”错误消息。当我调试错误时,第二行在下面的第三段中,是给我带来问题的,我该怎么做才能使此代码适用于同一工作簿中的多个工作表?

Sheets("BrentSkew").Select
NextFree = Range("A2:A" & 
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
NextFree = Range("A2:A" & 
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, 
SkipBlanks _
    :=False, Transpose:=False

Sheets("BrentSkew").Select
NextFree = Range("A2:A" & 
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 2).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
NextFree = Range("A2:A" & 
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks 
_
    :=False, Transpose:=False

Sheets("LLSSkew").Select
'the line below is the problem
NextFree = Range("A2:A" & 
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
NextFree = Range("A2:A" & 
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, 
SkipBlanks _
    :=False, Transpose:=False

Sheets("LLSSkew").Select
NextFree = Range("A2:A" & 
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 2).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
NextFree = Range("A2:A" & 
Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree - 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks 
_
    :=False, Transpose:=False

0 个答案:

没有答案