Search for sheets' name if matched condition then copy paste value with format?

自古美人都是妖i 提交于 2020-04-05 06:21:37

问题


Update: I was able to solve this problem. I just remembered VBA use * and ? to present the string 'If Sheet.name Like "*CopyA"'


I would like to make a copy special value with original format of the sources of all sheets contains the word: "CopyA" for example "Apple CopyA","Mango CopyA"

I can use the macro record to see how VBA create sheet and copy special like similar example below.

I am having trouble in the searching for sheet name - All examples I found either I must know exact name (same as Macro record) or I have to make a copy of all sheets (loop though all sheet and copy, no matter the name is).

Sub Macro1()
    Sheets("Sheet1").Select
    Sheets("Sheet1").Copy Before:=Sheets(1)
    Sheets("Sheet1(2)").Select
    Cells.Select
    Selection.Copy
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
End Sub

How can I search for sheet's name? In this case, any sheet that has "CopyA"?


回答1:


Another way :) You can also use Instr(). For example

For all sheets in the workbook, use this

Option Explicit

Sub Macro1()
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Sheets
        If InStr(1, ws.Name, "CopyA") Then
            '~~ > Your code
        End If
    Next
End Sub

For ActiveSheet use this

Option Explicit

Sub Macro1()
    If InStr(1, ActiveSheet.Name, "CopyA") Then
        '~~ > Your code
    End If
End Sub


来源:https://stackoverflow.com/questions/10670402/search-for-sheets-name-if-matched-condition-then-copy-paste-value-with-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!