使用 vba 将值从工作表 1 复制到工作表 2 并粘贴到所需位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14539152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:22:48  来源:igfitidea点击:

To copy values from sheet 1 to sheet 2 and paste it at desired location using vba

excel-vbavbaexcel

提问by Amit

My objective is to copy values from cells in sheet 1 "B5" onwards and paste it to sheet 2 "C11" onwards below is the code not working properly

我的目标是从工作表 1“B5”中的单元格复制值并将其粘贴到工作表 2“C11”以下是代码无法正常工作

Sub SCMPROCUREMENT()

' SUPPLY CHAIN MANAGEMENT PROCUREMENT


Worksheets("Sheet1").Select


Range("B5:B100000").Select

finalrow = Cells(Rows.Count, 2).End(xlUp).Row

For x = 5 To finalrow

If Worksheets("sheet1").Cells(x, 2).Font.Bold = False Then

Worksheets("sheet1").Select

Cells(x, 2).Select

Selection.Copy

ThisWorkbook.Worksheets("Sheet2").Range("C11").Select


ActiveSheet.Paste


End If
Next x

End Sub

回答by Edmund Moshammer

a) your ThisWorkbook.Worksheets("Sheet2").Range("C11")doesn't work like that

a) 你的ThisWorkbook.Worksheets("Sheet2").Range("C11")不是这样工作的

b) you need a counter for the second list, otherwise you keep overwriting C11

b) 第二个列表需要一个计数器,否则你会继续覆盖 C11

Sub SCMPROCUREMENT()

    ' SUPPLY CHAIN MANAGEMENT PROCUREMENT

    Dim count As Integer

    For x = 5 To Worksheets("Sheet1").Cells(Rows.count, 2).End(xlUp).Row

        If Worksheets("Sheet1").Cells(x, 2).Font.Bold = False Then

            Worksheets("Sheet1").Cells(x, 2).Copy

            Worksheets("Sheet2").Cells(11 + count, 3).Select
            ActiveSheet.Paste

            count = count + 1

        End If
    Next x

End Sub

回答by InContext

Sub SCMPROCUREMENT()

    ' SUPPLY CHAIN MANAGEMENT PROCUREMENT

    Application.ScreenUpdating = False

    Dim count As Integer

    With Worksheets("Sheet1")

        For x = 5 To .Cells(Rows.count, 2).End(xlUp).Row

            If .Cells(x, 2).Font.Bold = False Then

                .Cells(x, 2).Copy Worksheets("Sheet2").Cells(11 + count, 3)
                count = count + 1
            End If
        Next x

    End With

    Application.ScreenUpdating = True

End Sub