从一张纸复制数据并粘贴到另一张纸的A列中的第一个空白单元格中

时间:2015-03-23 10:52:59

标签: excel vba excel-vba

Sub ImportFixed()
'
Sheets("Front-Page").Select
    Sheets("SPROC").Visible = True
Sheets("SPROC").Select
ThisWorkbook.RefreshALL
DoEvents
    'Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
    Sheets("SPROC").Select
    Range("J2").Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Master-Data-Sheet").Select
    Range("A1914").Select
    ActiveSheet.Paste
    Sheets("SPROC").Select

    Range("N2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Master-Data-Sheet").Columns("N:N").Range("N1914").Paste

    Cells.Select
    Application.CutCopyMode = False
    With Selection.Font
        .Size = 9
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
    End With
    With Selection.Font
        .Name = "Calibri"
        .Size = 9
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    Columns("A:H").Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With

    Columns("M:N").Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Range("L16108").Select
    Range("J2105").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range("J2137").Select
    Range("N2137").Select
    Sheets("SPROC").Select
    ActiveWindow.SelectedSheets.Visible = False
    ActiveWindow.ScrollWorkbookTabs Sheets:=-2
    Sheets("Master-Data-Sheet").Select
End Sub

我的报告中有一张名为SPROC的表单。每个星期一刷新此工作表,并从SQL查询中提取当天的数据(该工作表上的任何其他数据都将被覆盖)。我当时想要做的是选择所有数据(列A:N - 每周更改行数以使范围不固定)并将其粘贴到名为Master的工作表中的第A列中的第一个空白单元格中-数据表。第二张表包含前几周的所有数据,用于在各种其他工作表上填充所有数据透视表和图表等。目前我已经记录了一个宏,但它没有找到最后一个空行,而是使用一个特定的范围,这意味着当我运行宏时,它会覆盖主数据文件中的数据。有什么建议吗?

我已经包含了VBA代码的副本(它还执行了很多其他功能,如果它有点长,那么道歉)。我认为问题出现在20行和359行,但我不知道该怎么做才能修复它(我尝试了各种不同的变化)。

1 个答案:

答案 0 :(得分:0)

非常经典的事情,必须有很多类似的问题,请在记录宏中摆脱滚动和类似的东西......

试试这个:

Sub Macro2()
'
Dim ShIn As Worksheet
Dim ShOut As Worksheet
Set ShIn = ThisWorkbook.Sheets("SPROC")
Set ShOut = ThisWorkbook.Sheets("Master-Data-Sheet")

'ShIn.Cells(2, 1).End(xlToRight).Column
Dim RgTotalInput As String
Dim RgTotalOutput As String

RgTotalInput = "$A$2:$" & ColLet(ShIn.Cells(1, 1).End(xlToRight).Column) & "$" & ShIn.Cells(Rows.Count, 1).End(xlUp).Row
RgTotalOutput = "$A$" & ShOut.Cells(Rows.Count, 1).End(xlUp).Row + 1

ShIn.Range(RgTotalInput).Copy Destination:=ShOut.Range(RgTotalOutput)


End Sub



Public Function ColLet(ByVal ColNb As Integer) As String
Dim ColLetTemp As String

Select Case ColNb
    Case Is < 27
        ColLetTemp = Chr(64 + ColNb)
    Case Is > 26
        If Int(ColNb / 26) <> ColNb / 26 Then
            ColLetTemp = Chr(64 + Int(ColNb / 26)) & Chr(64 + ColNb - 26 * Int(ColNb / 26))
        Else
            ColLetTemp = Chr(64 + Int(ColNb / 26) - 1) & Chr(64 + 26)
        End If
    Case Else

End Select

ColLet = ColLetTemp
End Function
相关问题