Vba 最后一行定义提供给目标时是无效的限定符
只需使用一个简单的函数查找最后一行,然后将单元格复制到目标Vba 最后一行定义提供给目标时是无效的限定符,vba,excel,Vba,Excel,只需使用一个简单的函数查找最后一行,然后将单元格复制到目标 Sub CMS() ' ' CMS Macro ' Dim LastRow As Long With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With Worksheets("Sheet5").Range("B2").Copy _ Destination:=LastRow.
Sub CMS()
'
' CMS Macro
'
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Worksheets("Sheet5").Range("B2").Copy _
Destination:=LastRow.Range("B1")
Worksheets("Sheet5").Range("A2").Copy _
Destination:=LastRow.Range("B2")
Worksheets("Sheet5").Range("B4:R4").Copy _
Destination:=LastRow.Range("C2")
End Sub
我是根据这个样品做的
还有那份
但是,我在我的邮件中收到回复,最后一行在我的目的地中是无效的限定符,我如何修复此问题?您已将
LastRow
声明为Long
Dim LastRow As Long
…然后尝试将其用作范围
对象:
LastRow.Range("B1")
使用单元格
:
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Worksheets("Sheet5").Range("B2").Copy _
Destination:=.Cells(LastRow, 2)
Worksheets("Sheet5").Range("A2").Copy _
Destination:=.Cells(LastRow + 1, 2)
Worksheets("Sheet5").Range("B4:R4").Copy _
Destination:=.Cells(LastRow + 1, 3)
End With
注意-您的预期目的地不清楚,因此上面的地址可能是错误的(我不确定
B2
等相对于一行的含义)。这至少可以让您克服编译错误。您最好使用Find
而不是xlUp
方法
- 处理隐藏行(
跳过它们)xlUp
- 满足空白范围(
缺少完整或空列作为边缘情况-因此需要进一步处理)xlUp
Sub main()
With Worksheets("Data") '<--| reference your worksheet
With .Range("A:A").SpecialCells(xlCellTypeConstants, xlTextValues).Offset(, .UsedRange.Columns.Count) '<--| reference its column A cells with "constant" text values offset to the first not-used column
.FormulaR1C1 = "=IF(MID(RC[-" & .Column - 1 & "],6,1)=""C"",1,"""")" '<--| place a formula returning "1" if the 6th character of corresponding column A cell value is "C"
.SpecialCells(xlCellTypeFormulas, xlNumbers).Offset(, -.Column + 1).Font.Color = vbRed '<-- select all cells whose formula returned "1" and color corresponding column A cell
.ClearContents
End With
End With
End Sub
Sub-main()
对于工作表(“数据”),您的实际目标是什么?
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Worksheets("Sheet5").Range("B2").Copy _
Destination:=.Cells(LastRow, 2)
Worksheets("Sheet5").Range("A2").Copy _
Destination:=.Cells(LastRow + 1, 2)
Worksheets("Sheet5").Range("B4:R4").Copy _
Destination:=.Cells(LastRow + 1, 3)
End With
Sub LastRowInOneColumn2()
Dim rng1 As Range
Set rng1 = ActiveSheet.Columns(1).Find("*", ActiveSheet.[a1], xlFormulas)
If Not rng1 Is Nothing Then
MsgBox "last row is " & rng1.Row
Else
MsgBox "column is blank"
End If
End Sub
Sub main()
With Worksheets("Data") '<--| reference your worksheet
With .Range("A:A").SpecialCells(xlCellTypeConstants, xlTextValues).Offset(, .UsedRange.Columns.Count) '<--| reference its column A cells with "constant" text values offset to the first not-used column
.FormulaR1C1 = "=IF(MID(RC[-" & .Column - 1 & "],6,1)=""C"",1,"""")" '<--| place a formula returning "1" if the 6th character of corresponding column A cell value is "C"
.SpecialCells(xlCellTypeFormulas, xlNumbers).Offset(, -.Column + 1).Font.Color = vbRed '<-- select all cells whose formula returned "1" and color corresponding column A cell
.ClearContents
End With
End With
End Sub