Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 49888

[SOLVED] Check Sheet Existence Continues to Raise Error

$
0
0
Seemingly trivial task - I have looked here and elsewhere, and tried a multitude of functions and subs dedicated to testing for existence of a worksheet equal to a string name - all with no luck.

I'm attempting to loop a range (rDept) and, if a sheet already exists equal to the active cell (rCell) in the loop range, copy the row range to the next available row of the sheet; if not, create a new worksheet with the name equal to the active cell of the loop and repeat .value = .value.

I continue to get a subscript out of range error at line
Code:

Set wsTemp = wb.Sheets(sDept)
Many thanks.

Code:

Dim wb as Workbook
Set wb = ThisWorkbook

        With wsMaster
            Set rHeader = .Range("A1", .Range("A1").End(xlToRight))
            Set rDept = .Range("Q2", .Range("Q2").End(xlDown))
            For Each rCell In rDept
                sDept = CStr(rCell)
                Set rSource = rCell.Offset(, -16).Resize(1, 20)
                On Error Resume Next
                Set wsTemp = Nothing
                Set wsTemp = wb.Sheets(sDept)
                If Not wsTemp Is Nothing Then

                    With Sheets(sDept)
                        Set rDest = .Range("A1000").End(xlUp).Offset(1).Resize(1, 20)
                        rDest.Value = rSource.Value
                    End With
                Else
                    Set wsTemp = wb.Sheets.Add(, Sheets.Count)
                    With wsTemp
                        .Name = sDept
                        .Range("A1:T1").Value = rHeader.Value
                        Set rDest = .Range("A1000").End(xlUp).Offset(1).Resize(1, 20)
                        rDest.Value = rSource.Value
                    End With
                End If
            Next rCell
        End With


Viewing all articles
Browse latest Browse all 49888

Trending Articles