简体   繁体   中英

Set sheet based on cell value

I have a problem with the following vba script. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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