复制范围并粘贴到另一个工作表vba上的特定单元格

时间:2016-07-14 00:25:57

标签: excel vba excel-vba

嗨,我是这个论坛的新手。

  1. 我想将范围a1:a10sheet 1复制到sheet 2的单元格,从a10开始,增加10个单元格。即:

    • sheet1:a1上的sheet2:a10的内容;
    • sheet1:a2上的sheet2:a20的内容;
    • sheet1:a3上的sheet2:a30的内容;
    • ......依旧......
  2. 我知道简单范围复制和粘贴的代码如下

    Sub Copy(
    Dim CopyFrom As Range
    
    Set CopyFrom = Sheets("Sheet1").Range("A2 : A10")
    Sheets("Sheet2").Range("A1").Resize(CopyFrom.Rows.Count).Value = CopyFrom.Value
    
    End Sub
    

    请编辑上面的代码以帮助满足我的需求。

    1. 根据单元格中的值隐藏行数。即,如果单元格a1包含10且单元格a2包含50,则应隐藏第10至50行。
    2. 我可以通过将一个值设置为固定并从单元格调用一个值而不是两个来完成上述操作。

      Sub AUTOHIDE_ROWS_307()
      '
      Application.ScreenUpdating = False
      Sheets("sheet1").Select
      Dim Cval As Variant
      Dim Rng1 As Range
      Cval = ActiveSheet.Range("a1").Value
      Set Rng1 = ActiveSheet.Range("A50:N" & Cval)
      Rng1.Activate
      Selection.EntireRow.Hidden = True
      Application.ScreenUpdating = True
      End Sub
      

      请通过上面的编辑来帮助满足我的需求。

      提前致谢。

2 个答案:

答案 0 :(得分:0)

我知道这不是本论坛的目的,但你在这里迷失了,并且会给你一些你可以作为起点的东西。

Sub doThatCopy()
    'Create your variables
    Dim myFormerSheetRow, myDestinationSheetRow, myFormerColumn, myDestinationColumn As Integer
    Dim myFormerSheetName, myDestinationSheetName As String

    'Setting the variables first values
    myFormerColumn = 1 ' 1 = A, 2 = B...
    myDestinationColumn = 3 '3 = C
    myFormerSheetRow = 0 ' This row, doesn't exists, but we'll increment it before first use
    myDestinationSheetRow = 1

    myFormerSheetName = "Sheet1"
    myDestinationSheetName = "Sheet2"

    For i = 1 To 10
        'You could use  "myFormerSheetRow = i" instead of the row below, but I'll leave it this way to be more understandable for you
        myFormerSheetRow = myFormerSheetRow + 1
        myDestinationSheetRow = myFormerSheetRow * 10
        ActiveWorkbook.Worksheets(myDestinationSheetName).Cells(myDestinationSheetRow, myDestinationColumn).Value = ActiveWorkbook.Worksheets(myFormerSheetName).Cells(myFormerSheetRow, myFormerColumn).Value
    Next i

    MsgBox ("Success!")
End Sub

答案 1 :(得分:0)

1

pkill

2

Dim I as Integer
For I = 1 To 10
    Sheets("Sheet2").Cells(1, I * 10) = Sheets("Sheet1").Cells(1, I)
Next
相关问题