+ Reply to Thread
Results 1 to 8 of 8

Select statement producing an error "Select method of worksheet failed"

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-10-2011
    Location
    swindon, england
    MS-Off Ver
    Excel 2010
    Posts
    317

    Select statement producing an error "Select method of worksheet failed"

    Hi

    I this code below but the select satement keeps producing and error of "Select method of worksheet failed"

    If bLang = vbYes Then 'if the user selected yes to importing langauge data
        Set PPOld = Workbooks.Open(StrFldr & "\" & "HDE_PPIII_MONTH_Input_Reference_Table_V" & bVImport & ".xlsx")
        PPNew.Sheets.Add.Name = "DE": PPNew.Sheets.Add.Name = "PL"
        PPNew.Sheets("EN").Select    Sheets("EN").Select: Range("A:A").Copy: Sheets("DE").Select: Range("a1").PasteSpecial Paste:=xlPasteValues
        Sheets("EN").Select: Range("A:A").Copy: Sheets("PL").Select: Range("a1").PasteSpecial Paste:=xlPasteValues
        
        For Each shName In Array("DE", "PL")
            shFound = False
            For Each sh In Worksheets
                If LCase(sh.Name) = LCase(shName) Then
                    shFound = True
                PPOld.Sheets(shName).Range("B2:P2000").Copy Destination:=PPNew.Sheets(shName).Range("AA1")
                End If
            Next sh
        Next shName
        
        PPOld.Close
        
        For Each shName2 In Array("DE", "PL")
            For Each sht In Array(27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43) 'Loop through the vlookup cells and autofill the rest of the columns
                With PPNew.Sheets(shName).cells(2, 2)
                    .Formula = "=IF(A2=0,0,VLOOKUP($A2,ConvertManual!$A$2:$Y$500," & sht & ",FALSE))"
                    .AutoFill .Resize(500)
                End With
            Next
        Next shName2
    End If
    Does anyone know why its keeps producing an error?

    Thanks

    Jeskit
    Last edited by jeskit; 05-10-2011 at 11:51 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Select statement producing an error "Select method of worksheet failed"

    Your multi-commands on a single row is difficult to read properly and you have at least one colon missing. But you should really put your commands on a separate lines AND remove all the selecting, not necessary in VBA.
    If bLang = vbYes Then 'if the user selected yes to importing langauge data
        Set PPOld = Workbooks.Open(StrFldr & "\" & "HDE_PPIII_MONTH_Input_Reference_Table_V" & bVImport & ".xlsx")
        PPNew.Sheets.Add.Name = "DE"
        PPNew.Sheets.Add.Name = "PL"
        PPOld.Sheets("EN").Copy
        PPNew.Sheets("DE").Range("A1").PasteSpecial Paste:=xlPasteValues
        PPOld.Sheets("EN").Range("A:A").Copy
        PPNew.Sheets("PL").Range("A1").PasteSpecial Paste:=xlPasteValues
        
        For Each shName In Array("DE", "PL")
            shFound = False
            For Each sh In Worksheets
                If LCase(sh.Name) = LCase(shName) Then
                    shFound = True
                PPOld.Sheets(shName).Range("B2:P2000").Copy Destination:=PPNew.Sheets(shName).Range("AA1")
                End If
            Next sh
        Next shName
        
        PPOld.Close
        
        For Each shName2 In Array("DE", "PL")
            For Each sht In Array(27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43) 'Loop through the vlookup cells and autofill the rest of the columns
                With PPNew.Sheets(shName).Cells(2, 2)
                    .Formula = "=IF(A2=0,0,VLOOKUP($A2,ConvertManual!$A$2:$Y$500," & sht & ",FALSE))"
                    .AutoFill .Resize(500)
                End With
            Next
        Next shName2
    End If
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor
    Join Date
    01-10-2011
    Location
    swindon, england
    MS-Off Ver
    Excel 2010
    Posts
    317

    Re: Select statement producing an error "Select method of worksheet failed"

    Hi

    Thanks for replying!

    Ok i have changed it to the macro below and its fixed the first erro but i am now gettign a "Subscript out of range" error when it gets to the line i highlighted red

    If bLang = vbYes Then 'if the user selected yes to importing langauge data
        Application.StatusBar = "Importing previous language data"
        Set PPOld = Workbooks.Open(StrFldr & "\" & "HDE_PPIII_MONTH_Input_Reference_Table_V" & bVImport & ".xlsx")
            
        For Each shName In Array("DE", "PL")
            shFound = False
            For Each sh In Worksheets
                If LCase(sh.Name) = LCase(shName) Then
                    shFound = True
                PPOld.Sheets(shName).Range("B2:P2000").Copy Destination:=PPNew.Sheets(shName).Range("AA1")
                End If
            Next sh
        Next shName
        
        PPOld.Close
        NCol = 2
        
        For Each shName2 In Array("DE", "PL")
            For Each sht In Array(27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43) 'Loop through the vlookup cells and autofill the rest of the columns
                With PPNew.Sheets(shName).cells(2, NCol)                .Formula = "=IF(A2=0,0,VLOOKUP($A2,ConvertManual!$A$2:$Y$500," & sht & ",FALSE))"
                    .AutoFill .Resize(500)
                End With
            Next
            PPNew.Sheets(shName2).cells.Copy
            PPNew.Sheets(shName2).Range("A1").PasteSpecial Paste:=xlPasteValues
        Next shName2
              
    End If

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Select statement producing an error "Select method of worksheet failed"

    Shouldn't that be:
    With PPNew.Sheets(shName2)

  5. #5
    Valued Forum Contributor
    Join Date
    01-10-2011
    Location
    swindon, england
    MS-Off Ver
    Excel 2010
    Posts
    317

    Re: Select statement producing an error "Select method of worksheet failed"

    Hi

    Thanks its working great now but this line:

         For Each shName2 In Array("DE", "PL")
            For Each sht In Array(27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43) 
                 With PPNew.Sheets(shName).cells(2, NCol)                
                    .Formula = "=IF(A2=0,0,VLOOKUP($A2,ConvertManual!$A$2:$Y$500," & sht & ",FALSE))"
                    .AutoFill .Resize(500)
                    NCol = NCol + 1
                  End With
            Next
            PPNew.Sheets(shName2).cells.Copy
            PPNew.Sheets(shName2).Range("A1").PasteSpecial Paste:=xlPasteValues
        Next shName2
    This bit should be looping through the two sheets and for each sheet it should be looping through the columns in the array and for each one putting a vlookup formla into Row 2 and starting at column B and then for each column in the array it should go to a new column

    And it is doing this but its starting at column S instead of B

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Select statement producing an error "Select method of worksheet failed"

                 With PPNew.Sheets(shName).cells(2, NCol)                
                    .Formula = "=IF(A2=0,0,VLOOKUP($A2,ConvertManual!$A$2:$Y$500," & sht & ",FALSE))"
                    .AutoFill .Resize(500)
                    NCol = NCol + 1
                  End With
    You are using a variable called NCol to designate the column. Apparently the value of NCol at the start of the is not "2", which is what it would need to be to start at column B.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1