首页 文章

如何将最后一行复制到另一个excel表?

提问于
浏览
0

我有一个Excel文档,可以在一张表中从Google财经获取数据 . 我想要的是将第一张工作表中的最后一行复制到同一工作簿中的另一个工作表 .

这是我正在使用的代码

Sub Macro1()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")

ws1.Range("A1:E100").ClearContents
'CS = "URL;https://finance.google.com/finance/getprices?q=NIFTY&i=900&p=3d&f=o,h,l,c"

CS2 = ws2.Range("A3").Value
CS1 = CS2 & "&i=900&p=3d&f=o,h,l,c"
CS = "URL;https://finance.google.com/finance/getprices?q=" & CS1

ws1.Select

With ws1.QueryTables.Add(Connection:=CS, Destination:=Range("$A$1"))

    .Name = "getprices?q=NIFTY&i=900&p=3d&f=o,h,l,c_12"
    .FieldNames = True
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlAllTables
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

Rows("1:7").Select
Range("A7").Activate
Selection.Delete Shift:=xlUp
Columns("A:A").Select

Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), TrailingMinusNumbers:= _
    True

Selection.ColumnWidth = 10.86
Range("F7").Select
End Sub

请帮我复制并粘贴到另一张表,谢谢

1 回答

  • 0

    您可以在函数中将以下代码添加到 copy last rowanother sheet 中的 paste .

    在我的代码中,我假设你的数据在A列中,所以我复制了 A 列不为空的最后一行 .

    如果要更改它,请将 (ws1.Rows.Count, 1)1 的值更改为数据所在列的编号(例如 (ws1.Rows.Count, 4) ,如果您的数据位于 D 列中,因为它是第4列) .

    'Variable for the destination
    Dim destinationRow As Integer
    
    'Row in the second sheet where it will be paste
    destinationRow = 5
    
    'Copy th last row in your first sheet
    ws1.Rows(ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row - 1).EntireRow.Copy
    
    'Paste it in the second sheet
    ws2.Rows(destinationRow).PasteSpecial
    

    您需要改变什么

    destinationRow = 5 中的 5 的值更改为目标行(第二个工作表中要粘贴的行) .

    例如 destinationRow = 7 要将其粘贴到第二个工作表的第7行或 destinationRow = 6 以将其粘贴到第二个工作表中的第6行 .

相关问题