Subscript out range

Navith

New Member
Joined
Mar 2, 2018
Messages
38
I am getting a subscript out range error on the line Set wsLr = Workbooks(wsName).Worksheets("F17-F18 Releases")
in the following code can any one help



Code:
Public wbSrc As Excel.Workbook
 


 Dim wsSrc As Worksheet
 Dim wsLr As Worksheet
 
 Public wsName As String
 
Private Sub Filter()
'Dim wb1 As Excel.Workbook
'Set wb1 = Workbooks.Open("C:\Users\Vibc\Downloads\cbc\Copy of PBPTReleaseBookofRecords.xlsx")
 Dim i As Date, j As Date
 Dim k As String
    i = STARTDATE.Value
    j = ENDDATE.Value
    k = LOBUNIT.Value
Dim m As Long, n As Long
m = i
n = j
Set wbSrc = Workbooks.Open("C:\Users\Vibc\Downloads\cbc\Copy of PBPTReleaseBookofRecords.xlsx")




wbSrc.Worksheets("Item Master").Activate
    
With wbSrc.Worksheets("Item Master")
wbSrc.Sheets("Item Master").Range("B4").Select
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    
    ActiveSheet.AutoFilterMode = False
    
    .Rows(lastRow + 1).EntireRow.Hidden = True


    .Range("E4").AutoFilter Field:=5, Criteria1:=">=" & m _
        , Operator:=xlAnd, Criteria2:="<=" & n + 1


    .Range("E4").AutoFilter Field:=7, Criteria1:=LOBUNIT.Value


End With






End Sub


Private Function VisibleCells(rng As Range) As Range
    Dim r As Range
    For Each r In rng
        If r.EntireRow.Hidden = False Then
            If VisibleCells Is Nothing Then
                Set VisibleCells = r
            Else
                Set VisibleCells = Union(VisibleCells, r)
            End If
        End If
    Next r
End Function




'Main script
Private Sub Execute_Click()


wsName = UserForm1.WorksheetName


'Turn off flickering screen
Application.ScreenUpdating = False


 'Filter function
 Call Filter
 UserForm1.Hide




 'Search for ids
 Dim cl As Variant, rng As Range
 Dim lastRow As Long
 lastRow = 0
'Selection.SpecialCells(xlCellTypeVisible).Select
'    Sheets("Item Master").Select
 'Set rng = wbSrc.Worksheets("Item Master").Range("C:C").Cells
 'loop the usedrange
 Workbooks.Open Filename:=ThisWorkbook.Path & "" & wsName
 
' Dim rng4 As Range
' Set rng4 = selectvisiblecolc()
 For Each cl In SelectVisibleInColC()
 'rng.Cells.SpecialCells (xlCellTypeVisible)
    Dim value1 As String
    Dim value2 As String
    
    value1 = Replace(Trim(cl.Value), "-", " ")
    'Compare id with worksheet-to-extract-to's id
    Dim cl2 As Range, rng2 As Range


    'Set wsLr = Workbooks("F17-F18 Lending Releases_V1.xlsx").Worksheets("F17-F18 Releases")
    Set wsLr = Workbooks(wsName).Worksheets("F17-F18 Releases")
    Set rng2 = wsLr.Range("C:C")


    
    Set wsSrc = wbSrc.Worksheets("Item Master")
    
    Dim matchExists As Boolean
    
    matchExists = False
    
    For Each cl2 In rng2
        value2 = Replace(Trim(cl2.Value), "-", " ")
        If value1 = value2 Then
            matchExists = True
            wsLr.Cells(cl2.Row, 4).Value = wsSrc.Cells(cl.Row, 4).Value
            wsLr.Cells(cl2.Row, 8).Value = wsSrc.Cells(cl.Row, 6).Value
            wsLr.Cells(cl2.Row, 21).Value = wsSrc.Cells(cl.Row, 5).Value
            wsLr.Range(wsLr.Cells(cl2.Row, "V"), wsLr.Cells(cl2.Row, "CI")).value2 = wsSrc.Range(wsSrc.Cells(cl.Row, "AG"), wsSrc.Cells(cl.Row, "CT")).value2
        End If
    Next cl2
    
    'if there does not exist a match in the worksheet to be exported, then add the item to the bottom
    If matchExists = False Then
        Dim lRow As Long
        With wsLr
            lRow = .Cells(.Rows.Count, "F").End(xlUp).Row
            If lastRow = 0 Then
                 lastRow = lRow + 2
            Else
                 lastRow = lastRow + 1
            End If
            wsLr.Cells(lastRow, 3).Value = wsSrc.Cells(cl.Row, 3).Value
            wsLr.Cells(lastRow, 4).Value = wsSrc.Cells(cl.Row, 4).Value
            wsLr.Cells(lastRow, 8).Value = wsSrc.Cells(cl.Row, 6).Value
            wsLr.Cells(lastRow, 21).Value = wsSrc.Cells(cl.Row, 5).Value
            wsLr.Range(wsLr.Cells(lastRow, "V"), wsLr.Cells(lastRow, "CI")).value2 = wsSrc.Range(wsSrc.Cells(cl.Row, "AG"), wsSrc.Cells(cl.Row, "CT")).value2
        End With
    End If
    
    
 Next cl




Workbooks(wsName).SaveCopyAs (ThisWorkbook.Path & "\Filtered_" & wsName)


Unload UserForm1




End Sub




Private Function SelectVisibleInColC() As Range
    Dim lRow1 As Long


    With wbSrc.Sheets("Item Master")


        lRow1 = .Cells(.Rows.Count, 3).End(xlUp).Row


        If lRow1 < 3 Then Exit Function


       Set SelectVisibleInColC = .Cells(1, 3).Offset(1, 0).Resize(lRow1 - 2).SpecialCells(xlCellTypeVisible)


    End With
End Function
 
Last edited by a moderator:
Well you'll want to declare it as a worksheet, but yeah, that's about what I was suggesting.

Code:
Public wsLr As Worksheet
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
the code runs completely.
but the excel is hang now. looks like running in a loop. I left it run for more than 3 hours. all the excel work books are open but its not responding. Excel drawing 76% of CPU memory and runs for 3.5 hours . I had to close excel by using windows task manager.
Firstly change where you set Rng2 to
Code:
Set rng2 = wsLr.Range("C2", wsLr.Range("C" & Rows.Count).End(xlUp))
That should speed things up a bit.
Also what are the values in Col C that you are comparing?
 
Upvote 0
Firstly change where you set Rng2 to
Code:
Set rng2 = wsLr.Range("C2", wsLr.Range("C" & Rows.Count).End(xlUp))
That should speed things up a bit.
Also what are the values in Col C that you are comparing?

General - text
 
Upvote 0
Can the values in the "Item Master" sheet occur more than once in the "F17-F18 Releases" sheet?
 
Upvote 0
Can the values in the "Item Master" sheet occur more than once in the "F17-F18 Releases" sheet?

The Set rng2 command did speed up the process, now I have the updated "F17-F18 Releases" sheet. only issue is its not comparing the existing value and replace the rows. it simply adds the newly found values to the bottom rows.

the values in Item master shouldn't occur more than once in the "F17-F18 Releases" sheet.
 
Upvote 0
Try this
Code:
Private Sub Execute_Click()
   Dim cl As Range
   Dim rng2 As Range
   Dim NxtRw As Long
   Dim Fnd As Range
   wsName = UserForm1.WorksheetName
   
   
   'Turn off flickering screen
   Application.ScreenUpdating = False
   
   
    'Filter function
    Call Filter
    UserForm1.Hide
   
    'Search for ids
    
   Workbooks.Open FileName:=ThisWorkbook.Path & "" & wsName
   Set wsLr = Workbooks("F17-F18 Releases.xlsx").Worksheets("F17-F18 Releases")
   Set rng2 = wsLr.Range("C2", wsLr.Range("C" & Rows.Count).End(xlUp))
   Set wsSrc = wbSrc.Worksheets("Item Master")
   
   NxtRw = wsLr.Cells(wsLr.Rows.Count, "F").End(xlUp).Offset(1).Row
   
   For Each cl In SelectVisibleInColC()
      Set Fnd = rng2.Find(cl.Value, , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Fnd.Offset(, 1).Value = cl.Offset(, 1).Value
         Fnd.Offset(, 5).Value = cl.Offset(, 3).Value
         Fnd.Offset(, 19).Value = cl.Offset(, 2).Value
         Intersect(Fnd.Row, wsLr.Range("V:CI")).value2 = Intersect(cl.Row, wsSrc.Range("AG:CT")).value2
      Else
         wsLr.Cells(NxtRw, 3).Value = wsSrc.Cells(cl.Row, 3).Value
         wsLr.Cells(NxtRw, 4).Value = wsSrc.Cells(cl.Row, 4).Value
         wsLr.Cells(NxtRw, 8).Value = wsSrc.Cells(cl.Row, 6).Value
         wsLr.Cells(NxtRw, 21).Value = wsSrc.Cells(cl.Row, 5).Value
         wsLr.Range(wsLr.Cells(NxtRw, "V"), wsLr.Cells(NxtRw, "CI")).value2 = wsSrc.Range(wsSrc.Cells(cl.Row, "AG"), wsSrc.Cells(cl.Row, "CT")).value2
      End If
   Next cl


Workbooks(wsName).SaveCopyAs (ThisWorkbook.Path & "\Filtered_" & wsName)


Unload UserForm1




End Sub
 
Upvote 0
The Set rng2 command did speed up the process, now I have the updated "F17-F18 Releases" sheet. only issue is its not comparing the existing value and replace the rows. it simply adds the newly found values to the bottom rows.

the values in Item master shouldn't occur more than once in the "F17-F18 Releases" sheet.

there is new excel sheet created "Filtered_F17-F18 Lending Releases_V1.3" in the same folder where rest of the files are.

The headings Rows 1 - 4 are being copied again in the bottom. ( I have changed the Set rng2 = wsLr.Range("C2", wsLr.Range("C" & Rows.Count).End(xlUp)) to Set rng2 = wsLr.Range("C4", wsLr.Range("C" & Rows.Count).End(xlUp)) as the first 4 rows are headings.
 
Upvote 0
Type mismatch
Code:
Intersect(Fnd.Row, wsLr.Range("V:CI")).value2 = Intersect(cl.Row, wsSrc.Range("AG:CT")).value2
 
Upvote 0
Oops it should be
Code:
         Intersect(Fnd.Row[COLOR=#ff0000]s[/COLOR], wsLr.Range("V:CI")).value2 = Intersect(cl.Row[COLOR=#ff0000]s[/COLOR], wsSrc.Range("AG:CT")).value2
 
Upvote 0
now only one row is being added to the bottom of the "F17-F18 Releases" worksheet and new work book "Filtered_F17-F18 Releases"
Cell B of the new row added is offset to cell C. no values in cells E:G, K:T. cells D, H an U are updated properly.
Heading cells from A1:H1 and cell U1 updated as blank
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top