0

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

' Sub ABringData() ' ' BringData 宏 ' ' 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

结束子

4

1 回答 1

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
于 2013-04-16T09:16:27.743 回答