1

I have one cell with all sheet name write in this way: "Sheet1", "Sheet2", "Sheet3". So I insert this value in single variable SheetNames as String.

Now, I need to get single sheet name and insert. My code is:

varSheets = Array(FogliArray)
    'varSheets = Array("Other", "Research", "IT")

    lngShtCnt = 0
    On Error Resume Next
    For Each varSheet In varSheets
        With wkbSource.Worksheets(varSheet)

If I use varSheets = Array("Other", "Research", "IT") all work correctly. But if I use varSheets = Array(FogliArray) where FogliArray is variable that contains all sheet name ("Other", "Research", "IT") not works.

Please, can you help me? Many thanks, Andrea.

3
  • 2
    Use Split and you may have to replace the quotes too. Commented Mar 30, 2020 at 14:35
  • so FogliArray refers to one cell? Commented Mar 30, 2020 at 14:35
  • 1
    @BigBen you may want to post it as an aswer? Commented Mar 30, 2020 at 14:47

1 Answer 1

3

Use Split to return an array of the worksheet names, Replace the quotes, and Trim$ the spaces, something like the following:

Sub Test()
    Dim FogliList As String
    FogliList = Sheet1.Range("A1").Value
    FogliList = Replace(FogliList, """", "") ' no quotes

    Dim FogliArray
    FogliArray = Split(FogliList, ",")

    Dim i As Long
    For i = LBound(FogliArray) To UBound(FogliArray)
        Dim FoglioName As String
        FoglioName = Trim$(FogliArray(i))

        With wkbSource.Worksheets(FoglioName)
            ' Do whatever you wanted to do
        End With
    Next
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.