宏将列数据复制到另一个工作表中,直到源表中的最后一行数据

时间:2013-04-15 16:14:02

标签: excel-2010

我正在尝试将数据从源表复制到另一张表中,直到最后一行数据。 如何将宏限制到最后一行数据。下面是为获取数据而创建的宏。

'Sub ABringData() “ '带来数据宏 “ “ Sub ABringData()

Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("G1").Select
ActiveCell.FormulaR1C1 = "=Data!RC[-4]"
Range("G2").Select
Columns("G:G").EntireColumn.AutoFit
Range("G1").Select
Selection.AutoFill Destination:=Range("G:G")
Range("G:G").Select
Columns("G:G").EntireColumn.AutoFit
Columns("G:G").Select
With Selection.Font
    .Color = -16776961
    .TintAndShade = 0
End With
Columns("AA:AA").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("AA1").Select
ActiveWindow.SmallScroll ToRight:=1
ActiveCell.FormulaR1C1 = "=Data!RC[-20]"
Range("AA1").Select
Selection.AutoFill Destination:=Range("AA:AA")
Range("AA:AA").Select
With Selection
    .HorizontalAlignment = xlLeft
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
With Selection.Font
    .Color = -16776961
    .TintAndShade = 0
End With
ActiveWindow.SmallScroll ToRight:=-12
Columns("G:G").Select
With Selection
    .HorizontalAlignment = xlLeft
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
Range("G4").Select

End Sub

1 个答案:

答案 0 :(得分:0)

Sub ABringData()

'************* Add this ***************
Dim intLastRow As Integer
Dim strRange As String

intLastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'************** End Add ****************


Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("G1").Select
ActiveCell.FormulaR1C1 = "=Data!RC[-4]"
Range("G2").Select
Columns("G:G").EntireColumn.AutoFit
Range("G1").Select

'************* Add this ***************
strRange = "G1:G" & intLastRow
Selection.AutoFill Destination:=Range(strRange)
 '************** End Add ****************

Range("G:G").Select
Columns("G:G").EntireColumn.AutoFit
Columns("G:G").Select

With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With

Columns("AA:AA").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("AA1").Select
ActiveWindow.SmallScroll ToRight:=1
ActiveCell.FormulaR1C1 = "=Data!RC[-20]"
Range("AA1").Select

'************* Add this ***************
strRange = "AA1:AA" & intLastRow
Selection.AutoFill Destination:=Range(strRange)
'************** End Add ****************

Range("AA:AA").Select

With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With

With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With

ActiveWindow.SmallScroll ToRight:=-12
Columns("G:G").Select

With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With

Range("G4").Select

End Sub