【问题标题】:how to copy last row to another excel sheet?如何将最后一行复制到另一个excel表?
【发布时间】:2017-10-13 20:45:16
【问题描述】:

我有一个 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

请帮我复制粘贴到另一张纸上,谢谢

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以在您的函数中添加以下代码以复制整个最后一行粘贴另一张表强>。

    在我的代码中,我假设您的数据在 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 = 55 的值更改为目标行(第二张工作表中将要粘贴的行)。

    例如 destinationRow = 7 将其粘贴到第二张纸的第 7 行或 destinationRow = 6 将其粘贴到第二张纸的第 6 行。

    【讨论】:

    • 嗨,最后一行是空白的!
    • 其实数据填完的时候,最后一行是空白的!我想要倒数第二行
    猜你喜欢
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多