vba 将多列从一张纸复制到另一张纸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16886265/
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 21:24:36  来源:igfitidea点击:

copy multiple columns from one sheet to another

vbaexcel-vbaexcel

提问by OMK28

I have a time sheet for my workers who log their time using the sheet having below order

我有一个时间表,供我的工人使用以下顺序记录他们的时间

   Categories   Washing Cleaning   Office   Duties   Baking Cooking

Date          Hours  Hours     Hours    Hours     Hours Hours
Jan/1/13              3.00              6.00
Jan/2/13                        
Jan/6/13                        3.00    
Jan/10/13                       

Basically what I want is to have a code which copy dates with number of hours according to related category to another sheet called Report. I need three columns as output Date Hours Category.

基本上我想要的是有一个代码,它根据相关类别将日期和小时数复制到另一个名为 Report 的工作表中。我需要三列作为输出日期小时类别。

采纳答案by Santosh

Try below code :

试试下面的代码:

Sub sample()


    Dim lastRow As Long
    Dim col As Long
    Dim a As String, b As String, c As String

    With Sheets("sheet1")
        lastRow = .Range("A" & Rows.Count).End(xlUp).Row

        If lastRow < 4 Then lastRow = 4

        For i = 4 To lastRow
            For col = 2 To 7
                If .Cells(i, col) <> "" Then

                    a = .Cells(i, 1)
                    b = .Cells(i, col)
                    c = .Cells(1, col)

                    With Sheets("Report")
                        .Range("A" & .Range("A" & .Rows.Count).End(xlUp).Row + 1).Value = a
                        .Range("B" & .Range("B" & .Rows.Count).End(xlUp).Row + 1).Value = b
                        .Range("C" & .Range("C" & .Rows.Count).End(xlUp).Row + 1).Value = c
                    End With
                End If
            Next
        Next

    End With
End Sub

enter image description here

在此处输入图片说明

回答by Ramiro

Have you tried something like:

您是否尝试过类似的事情:

Sheet2.Range("A1").Value = Sheet1.Range("A1").Value

or

或者

Sheet2.Range("A1").Text = Sheet1.Range("A1").Text

Maybe you could make a loop and do something like:

也许你可以做一个循环并做一些类似的事情:

Sheet2.Range("A" & i).Value = Sheet1.Range("A" & i).Value

There're a lot of options available to do this... even Range.Copy (see: http://msdn.microsoft.com/en-us/library/office/ff837760.aspx)

有很多选项可以做到这一点......甚至 Range.Copy (参见:http: //msdn.microsoft.com/en-us/library/office/ff837760.aspx

Good luck.

祝你好运。