首页 文章

将值复制到多个工作表

提问于
浏览
-1

我在主工作表中有一列值要复制到特定单元格中的多个工作表 . 每个单元格值复制到每个工作表一次,重复超过625次 . 我在主工作表中有一列值(A2到A626)要复制到特定单元格中的多个工作表 . A2被复制到纸张2,单元格R4 . 重复这一过程,直到A626被复制到单元格R4的单元626 . 包含A2到A626数据的工作表是“唯一的”

我已从其他网站解除了此代码,但这并不符合我的期望 . Sub copyPasteData()Dim strSourceSheet As String Dim strDestinationSheet As String Dim lastRow As Long

strSourceSheet = "Unique"

Sheets(strSourceSheet).Visible = True
Sheets(strSourceSheet).Select

Range("A2").Select
Do While ActiveCell.Value <> ""
strDestinationSheet = ActiveCell.Value
Selection.Copy
Sheets(strDestinationSheet).Visible = True
Sheets(strDestinationSheet).Select
lastRow = LastRowInOneColumn("R")
Cells(lastRow + 1, 0).Select
Selection.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Sheets(strSourceSheet).Select
ActiveCell.Offset(0, 2).Select
ActiveCell.Offset(1, 0).Select
Loop

1 回答

  • 0

    你提供的细节很少,所以我会给你一些一般的提示 .

    要从特定工作表访问(设置或获取)特定单元格的值,您可以使用以下语法: Worksheets("sheetName").Cells(row, col).Valuerowcol 是整数,指定单元格的行和列),因此为了将值粘贴到工作表中的单元格B3来自表"Sheet1"的单元格C4的"Sheet2"你应该写: Worksheets("Sheet2").Cells(3, 2).Value = Worksheets("Sheet1").Cells(4, 3).Value .

    此外,您可以使用整数(1 - 第一张等)而不是名称访问工作表,这样您就可以使用循环遍历每个工作表:

    Worksheets(i).Cells(row, col).Value ,其中 irowcol 是整数 .

    咨询OP后,代码应如下所示:

    Sub CopyToMultipleSheets()
    Dim i As Long
    
    For i = 2 To 626
        'cell A2 will be referenced to as Cells(2, 1).Value
        'generally Ai cell is Cells(i, 1)
        'R4 cell is Cells(4, 18)
        Worksheets(i).Cells(4, 18).Value = Worksheets("unique").Cells(i, 1).Value
    
    Next i
    
    End Sub
    

相关问题