简体   繁体   English

根据单元格值设置工作表

[英]Set sheet based on cell value

I have a problem with the following vba script. 以下vba脚本存在问题。 I want to copy some cells from one sheet to another. 我想将一些单元格从一张纸复制到另一张纸。 The first sheet is selected based its name. 根据其名称选择第一张纸。 The sheet where i want to paste the cells is selected based on cell B1 in the first sheet. 我要粘贴单元格的工作表是根据第一张工作表中的单元格B1选择的。 I am using the following code: 我正在使用以下代码:

Dim ws as Worksheet 
Dim LR3 as Long
Dim LR4 as Long
Dim LR5 as Long
Dim ws3 as Worksheet


    For Each ws In ActiveWorkbook.Worksheets
        If Not ws.Name Like "BC-*" Then
    LR3 = ws.Cells(Rows.Count, 1).End(xlUp).Row
    ws.Range("E" & LR3 + 1).Formula = "=SUM(E4:E" & LR3 & ")"


    Dim i As Long, n As Long
       n = ws.Cells(Rows.Count, 1).End(xlUp).Row
        With ws.Range("S1")
            .Formula = "=myJoin(A4:A" & n & ","""")"
            .Value = .Value
        End With

    LR4 = ws.Cells(Rows.Count, 6).End(xlUp).Row
    ws.Range("F4:F" & LR4).Copy
    ws.Range("M4:M" & LR4).PasteSpecial Paste:=xlPasteValues
    ws.Range("M4:M" & LR4).RemoveDuplicates Columns:=1, Header:=xlNo
    LR5 = ws.Cells(Rows.Count, 13).End(xlUp).Row
    ws.Range("M4:M" & LR4).Cut
    Set ws3 = ws.Range("B1").Value
    ws3.Range("A30").PasteSpecial xlPasteValues

You need to use: 您需要使用:

Set ws3 = ActiveWorkbook.Worksheets(ws.Range("B1").Value)

for example. 例如。 Adjust the workbook if required. 如果需要,请调整工作簿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM