如何将特定列和行复制到另一个工作表

时间:2019-01-06 01:37:29

标签: excel vba

我有一个工作表,其中包含A:M列和1到5000行。当L和M列的数字大于0时,我想将行复制到另一个工作表。我也只需要A:F和新工作表上的K:M

2 个答案:

答案 0 :(得分:0)

您需要命名源工作表和目标工作表,但是下面的代码应该可以解决问题。

Sub SheetTransfer()

Dim i As Long
Dim j As Long
Dim t As Double
Dim LastRow As Long
Dim ws1 As String
Dim ws2 As String

'name source worksheet here
ws1 = "Sheet1"
'name target worksheet here
ws2 = "Sheet2"

'set the threshold value for a row to be copied over
t = 0

' set to column L
j = 12

    For i = 1 To 5000

        If Worksheets(ws1).Cells(i, j).Value > 0 Or Cells(i, j + 1).Value > t Then

            'find last row of target worksheet
            With Worksheets(ws2)
                LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            End With

            'copy/paste columns A-F
            Worksheets(ws1).Range(Cells(i, 1), Cells(i, 6)).Copy
            Worksheets(ws2).Cells(LastRow + 1, 1).PasteSpecial xlPasteValues
            'copy paste columns K-M
            Worksheets(ws1).Range(Cells(i, 11), Cells(i, 13)).Copy
            Worksheets(ws2).Cells(LastRow + 1, 11).PasteSpecial xlPasteValues

        End If
    Next i

End Sub

答案 1 :(得分:0)

将数据复制到其他工作表

调整常量部分中的值以适合您的需求。

代码

from noaa_sdk import noaa
from datetime import datetime
date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
n = noaa.NOAA()
res = n.get_observations('25311', 'US', start=date, end=None, num_of_stations=1)
temp= (next(res))
value =(temp.get('temperature'))
temperature = (value['value'])
temperature = temperature*9/5+32
print(temperature, ' F')