我是新来的VBA很抱歉,如果这似乎是一个简单的问题。
我试图创建一个宏将还原,并包括一对夫妇的公式工作表中的,但是当我尝试包括公式,直到最后一排,我得到一个错误“运行时错误1004 - 应用程序定义或对象定义错误”下面的代码:
ActiveSheet.Range("U2:U" & LastRow).Formula = "=L2/86400"
如果我改变了“最后一行”为一些宏工作正常。下面是整个代码。
Sheets("DLASpotPlacement").Select
Dim LastRow As Double
LastRow = Sheets("DLASpotPlacement").Cells(Rows.Count, 1).Rows
Range("A1").Select
ActiveSheet.Range("U:U, V:V, W:W").NumberFormat = "[h]:mm:ss;@"
ActiveSheet.Range("U2:U" & LastRow).Formula = "=L2/86400"
ActiveSheet.Range("V2:V" & LastRow).Formula = "=VALUE(H2)"
ActiveSheet.Range("W2:W" & LastRow).FormulaLocal = "=IF(AND(H2>0,0416666666666667;H2<=0,249988425925926);""01 - 06"";IF(AND(H2>=0,25;H2<0,4166551);""06 - 10"";IF(AND(H2>=0,4166667;H2<0,4999884);""10 - 12"";IF(AND(H2>=0,5;H2<0,7499884);""12 - 18"";""18 - 01""))))"
感谢所有帮助
出现此错误的原因有两个:
您在End(xlUp)
计算,e.g忘记LastRow
:
LastRow = Sheets("DLASpotPlacement").Cells(Rows.Count, 1).End(xlUp).Row
它必须被声明为整数例如为:
Dim LastRow as Long
Option Explicit
Sub CopyFormulas()
Const cCol As Variant = "A" ' Last Row Column Letter/Number
Const cFirstR As Long = 2 ' First Row Number
Dim LastRow As Long ' Last Row Number
With ThisWorkbook.Worksheets("DLASpotPlacement")
LastRow = .Cells(.Rows.Count, cCol).End(xlUp).Row
'.Cells(1, cCol).Select ' uncomment if necessary
' You don't need to format the entire columns.
.Range("U" & cFirstR & ":W" & LastRow).NumberFormat = "[h]:mm:ss;@"
.Range("U" & cFirstR & ":U" & LastRow).Formula = "=L2/86400"
.Range("V" & cFirstR & ":V" & LastRow).Formula = "=VALUE(H2)"
.Range("W" & cFirstR & ":W" & LastRow).FormulaLocal = _
"=IF(AND(H2>0,0416666666666667;H2<=0,249988425925926);""" _
& "01 - 06"";IF(AND(H2>=0,25;H2<0,4166551);""06 - 10"";IF(" _
& "AND(H2>=0,4166667;H2<0,4999884);""10 - 12"";IF(AND(H2>=0" _
& ",5;H2<0,7499884);""12 - 18"";""18 - 01""))))"
End With
End Sub
使用FormulaLocal
是一个很好的“绝招”记住。
@麦克风;你的问题是在这条线:
LastRow = Sheets("DLASpotPlacement").Cells(Rows.Count, 1).Rows
你所做的LastRow
一个数组,而不是一个数字。另外,是不是双而是Iteger(数学)。然而,该整数数据类型,太小了,如果你把它声明“作为整数”,你会得到一个“溢出”错误。下面是你需要做这一切工作的两个变化:
Dim LastRow As Long
LastRow = Sheets("DLASpotPlacement").Rows.Count
...
对于LASTROW,使用Worksheet.UsedRange属性。
你也可以使用Range.Resize属性来选择的范围,并更换为“用”“选择”。
Dim LastRow As Double
With Sheets("DLASpotPlacement")
LastRow = .UsedRange.Rows.count
.Range("U:W").NumberFormat = "[h]:mm:ss;@"
.Range("U1").Resize(LastRow - 1).Formula = "=L2/86400"
.Range("V1").Resize(LastRow - 1).Formula = "=VALUE(H2)"
.Range("W1").Resize(LastRow - 1).FormulaLocal = "..."
End With