我是Excel vba的新手,我正在尝试从Siddharth Rout(感谢)更改宏。
我需要指定在单元格中开始的范围,例如B13,并在C14中复制到另一个工作表。 如何管理?
提前致谢。
Option Explicit
Sub Sample()
Dim lastRow As Long, i As Long
Dim CopyRange As Range
'~~> Change Sheet1 to relevant sheet name
With Sheets("Sheet1")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To lastRow
If Len(Trim(.Range("A" & i).Value)) <> 0 Then
If CopyRange Is Nothing Then
Set CopyRange = .Rows(i)
Else
Set CopyRange = Union(CopyRange, .Rows(i))
End If
End If
Next
If Not CopyRange Is Nothing Then
'~~> Change Sheet2 to relevant sheet name
CopyRange.Copy
Sheets("Sheet2").Rows(1).PasteSpecial xlValues
End If
End With
End Sub
答案 0 :(得分:0)
由于CopyRange
是一个完整行宽,因此您需要在粘贴到Range("B13")
之前将其修剪掉。作为附加要求,OP希望从源范围中排除A列。
Sub Sample()
Dim lastRow As Long, i As Long
Dim CopyRange As Range
'~~> Change Sheet1 to relevant sheet name
With Sheets("Sheet1")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To lastRow
If Len(Trim(.Range("A" & i).Value)) <> 0 Then
If CopyRange Is Nothing Then
Set CopyRange = .Rows(i)
Else
Set CopyRange = Union(CopyRange, .Rows(i))
End If
End If
Next
If Not CopyRange Is Nothing Then
'~~> Change Sheet2 to relevant sheet name
Intersect(Sheets("Sheet1").UsedRange, CopyRange).Offset(0, 1).Copy
Sheets("Sheet2").Range("B13").PasteSpecial xlValues
End If
End With
End Sub