尝试将行从一个工作簿复制到另一个工作簿

时间:2018-08-17 09:28:24

标签: excel vba

我目前正在尝试创建一个宏,以从销售报价模板工作簿上的特定行中提取所有数据,然后将其粘贴到报价记录器工作簿上的下一个可用行中,但我确实在努力寻找正确的位置使其工作的代码。香港专业教育学院搜索,但没有发现我可以使用的具体。

我到目前为止有什么在下面。

Option Explicit
Sub Test()

Dim sht1 As Worksheet, sht2 As Worksheet
Dim i As Long

Set sht1 = ThisWorkbook.Worksheets("Sheet1")
Set sht2 = "..............RAYOTEC LOGGER.xlsm"

For i = 2 To sht1.Cells(sht1.Rows.Count, "M").End(xlUp).Row
    If sht1.Range("M" & i).Value = "No" Then
        sht1.Range("A" & i).EntireRow.Cut sht2.Range("A" & sht2.Cells(sht2.Rows.Count, "M").End(xlUp).Row + 1)
    End If
Next i

End Sub

预先感谢您的帮助

2 个答案:

答案 0 :(得分:3)

以此在FOR循环之前更改代码的一部分,

Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim wb As Workbook
Dim i As Long

Set sht1 = ThisWorkbook.Worksheets("Sheet1")
Set wb = Application.Workbooks.Open("..............RAYOTEC LOGGER.xlsm")
Set sht2 = wb.Sheets("Sheet1")

答案 1 :(得分:0)

此外,您还可以在其余的代码中使用以下内容,因为它只需轻松地获取值并删除不需要的行数据。

Dim sht1 As Worksheet 
Dim sht2 As Worksheet
Dim i, n As Long
Dim iMx, nMx As Long

iMx = sht1.Cells(sht1.Rows.Count, "M").End(xlUp).Row  

For i = 2 To iMx

    nMx = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row
    If sht1.Range("M" & i).Value = "No" Then
        sht2.Range("A" & nMx + 1).EntireRow.Value = sht1.Range("A" & i).EntireRow.Value
        sht1.Range("A" & i).EntireRow.Delete Shift:=xlUp
        i = i - 1
    End If

Next i