Array code not written correctly

panyagak

Active Member
Joined
Feb 24, 2017
Messages
299
kindly refer to this link from which this Array code was "improved upon", but which cant work due to "object code error". The array code was untested by the coder.

PICK OUT/EXTRACT column header "COMMON" IN ALL 5 SHEETS


Code:
Sub FindAll2()
    Dim lr As Long 'Last row in Wk1
    Dim nr As Long ' next available row in New All
    Dim i As Integer 'counter for worksheets
    Dim outarr As Variant
    On Error GoTo errHandle
    Application.EnableEvents = False
    Application.ScreenUpdating = False
   ' load all 5 sheets into varaint arrays
    lr1 = Worksheets("Wk1").Range("A" & Rows.Count).End(xlUp).Row
    w1arr = Worksheets("Wk1").Range(Cells(1, 1), Cells(lr1, 26))  ' I put 26 columns in because you wanted to check columnn z
    lr2 = Worksheets("Wk2").Range("A" & Rows.Count).End(xlUp).Row
    w2arr = Worksheets("Wk2").Range(Cells(1, 1), Cells(lr2, 26))  ' I put 26 columns in because you wanted to check columnn z
    lr3 = Worksheets("Wk3").Range("A" & Rows.Count).End(xlUp).Row
    w3arr = Worksheets("Wk3").Range(Cells(1, 1), Cells(lr3, 26))  ' I put 26 columns in because you wanted to check columnn z
    lr4 = Worksheets("Wk4").Range("A" & Rows.Count).End(xlUp).Row
    w4arr = Worksheets("Wk4").Range(Cells(1, 1), Cells(lr4, 26))  ' I put 26 columns in because you wanted to check columnn z
    lr5 = Worksheets("Wk5").Range("A" & Rows.Count).End(xlUp).Row
    w5arr = Worksheets("Wk5").Range(Cells(1, 1), Cells(lr5, 26))  ' I put 26 columns in because you wanted to check columnn z
    nr = Worksheets("New All").Range("A" & Rows.Count).End(xlUp).Row + 1
    ReDim outarr(1 To lr1, 1 To 26)
    'Get Names range
   ' Set rNames = Worksheets("Wk1").Range("B2:B" & lr)
    indi = 1
    For i = 1 To lr1
        wkcnt = 0
        thisname = w1arr(i, 2) ' column b of first worksheet
        For j = 1 To lr2
         If thisname = w2arr(j, 2) Then
          wkcnt = wkcnt + 1
          Exit For
         End If
        Next j
        For j = 1 To lr3
         If thisname = w3arr(j, 2) Then
          wkcnt = wkcnt + 1
          Exit For
         End If
        Next j
        For j = 1 To lr4
         If thisname = w4arr(j, 2) Then
          wkcnt = wkcnt + 1
          Exit For
         End If
        Next j
        For j = 1 To lr5
         If thisname = w5arr(j, 2) Then
          wkcnt = wkcnt + 1
          Exit For
         End If
        Next j
        If wkcnt = 4 Then
        'only get here if name is found in each sheet
        ' copy input row to output array
        For kk = 1 To 26
         outarr(indi, kk) = w1arr(i, kk)
        Next kk
        indi = indi + 1
        End If
    Next i
    ' write output array to workhseet
   Worksheets("New All").Range(Cells(nr, 1), Cells(nr + indi, 26)) = outarr
    Application.EnableEvents = True
    Application.ScreenUpdating = True
Exit Sub
errHandle:
    MsgBox Err.Description, vbCritical, Err.Number
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

Kindly help out.
 
Try
VBA Code:
Sub FindAll2()
    Dim lr As Long    'Last row in Wk1
    Dim nr As Long    ' next available row in New All
    Dim i As Integer    'counter for worksheets
    Dim outarr As Variant
    Dim lr1, lr2, lr3, lr4, lr5
    Dim w1arr As Variant
    Dim w2arr As Variant
    Dim w3arr As Variant
    Dim w4arr As Variant
    Dim w5arr As Variant
    Dim indi, wkcnt, thisname, j, kk
    
    On Error GoTo errHandle
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    ' load all 5 sheets into varaint arrays
    lr1 = Worksheets("Wk1").Range("A" & Rows.Count).End(xlUp).Row
    w1arr = Worksheets("Wk1").Cells(1, 1).Resize(lr1, 26)
'    w1arr = Worksheets("Wk1").Range(Cells(1, 1), Cells(lr1, 26))  ' I put 26 columns in because you wanted to check columnn z
    lr2 = Worksheets("Wk2").Range("A" & Rows.Count).End(xlUp).Row
    w2arr = Worksheets("Wk2").Cells(1, 1).Resize(lr2, 26)
'    w2arr = Worksheets("Wk2").Range(Cells(1, 1), Cells(lr2, 26))  ' I put 26 columns in because you wanted to check columnn z
    lr3 = Worksheets("Wk3").Range("A" & Rows.Count).End(xlUp).Row
    w3arr = Worksheets("Wk3").Cells(1, 1).Resize(lr3, 26)
'    w3arr = Worksheets("Wk3").Range(Cells(1, 1), Cells(lr3, 26))  ' I put 26 columns in because you wanted to check columnn z
    lr4 = Worksheets("Wk4").Range("A" & Rows.Count).End(xlUp).Row
    w4arr = Worksheets("Wk4").Cells(1, 1).Resize(lr4, 26)
'    w4arr = Worksheets("Wk4").Range(Cells(1, 1), Cells(lr4, 26))  ' I put 26 columns in because you wanted to check columnn z
    lr5 = Worksheets("Wk5").Range("A" & Rows.Count).End(xlUp).Row
    w5arr = Worksheets("Wk5").Cells(1, 1).Resize(lr5, 26)
'    w5arr = Worksheets("Wk5").Range(Cells(1, 1), Cells(lr5, 26))  ' I put 26 columns in because you wanted to check columnn z
    nr = Worksheets("New All").Range("A" & Rows.Count).End(xlUp).Row + 1
    ReDim outarr(1 To lr1, 1 To 26)
    'Get Names range
    ' Set rNames = Worksheets("Wk1").Range("B2:B" & lr)
    indi = 1
    For i = 1 To lr1
        wkcnt = 0
        thisname = w1arr(i, 2)    ' column b of first worksheet
        For j = 1 To lr2
            If thisname = w2arr(j, 2) Then
                wkcnt = wkcnt + 1
                Exit For
            End If
        Next j
        For j = 1 To lr3
            If thisname = w3arr(j, 2) Then
                wkcnt = wkcnt + 1
                Exit For
            End If
        Next j
        For j = 1 To lr4
            If thisname = w4arr(j, 2) Then
                wkcnt = wkcnt + 1
                Exit For
            End If
        Next j
        For j = 1 To lr5
            If thisname = w5arr(j, 2) Then
                wkcnt = wkcnt + 1
                Exit For
            End If
        Next j
        If wkcnt = 4 Then
            'only get here if name is found in each sheet
            ' copy input row to output array
            For kk = 1 To 26
                outarr(indi, kk) = w1arr(i, kk)
            Next kk
            indi = indi + 1
        End If
    Next i
    ' write output array to workhseet
    Worksheets("New All").Range(Cells(nr, 1), Cells(nr + indi, 26)) = outarr
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Exit Sub
errHandle:
    MsgBox Err.Description, vbCritical, Err.Number
'    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
mohadin.

thanks with few clarifications & hope it runs

Dim lr1, lr2, lr3, lr4, lr5 (as.......)

Dim indi, wkcnt, thisname, j, kk (as.......)


former
w1arr = Worksheets("Wk1").Range(Cells(1, 1), Cells(lr1, 26)) still in the new code to delete
w2arr etc

regards
 
Upvote 0
Well
VBA Code:
 lr1, lr2, lr3, lr4, lr5 ,j,kk,indi as Long
thisname as variant
w1arr = Worksheets("Wk1").Range(Cells(1, 1), Cells(lr1, 26)) still in the new code to delete
w2arr etc
They colored green so you can delete them
 
Upvote 0
Well
VBA Code:
 lr1, lr2, lr3, lr4, lr5 ,j,kk,indi as Long
thisname as variant
w1arr = Worksheets("Wk1").Range(Cells(1, 1), Cells(lr1, 26)) still in the new code to delete
w2arr etc
They colored green so you can delete them


your new approach of loadind sheets to Arrays is same/superior as one proposed before by Marty....he deleted his answers;

this
Code:
w1arr = Worksheets("Wk1").Cells(1, 1).Resize(lr1, 26)


regards
 
Upvote 0
@mohadin:
Worksheets("New All").Range(Cells(nr, 1), Cells(nr + indi, 26)) = outarr
This works only if worksheet New All is active.
 
Upvote 0
@mohadin:
Code:
Worksheets("New All").Range(Cells(nr, 1), Cells(nr + indi, 26)) = outarr
This works only if worksheet New All is active.

mart37
If you review mohadin's code in the spirit of "mutualness", would you make some changes which would deem the answer as final OR REWRITE THE CODE altogether

thanks for that observation

regards
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: I cant Loop through sheets using Arrays code
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: I cant Loop through sheets using Arrays code
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.

FLUFF.

THERE IS NO OPTION FOR DELETING MY THREAD THERE: HAD GIVEN UP, NO MUCH HELP WAS FORTHCOMING!!!!

this not fair
 
Upvote 0
So you are quite happy for members to potentially waste their time are you? IMO THAT is not fair!!
and as for
NO MUCH HELP WAS FORTHCOMING!!!!
Rory was helping you 2 days ago & you only came back to him about 25mins before starting this thread, so give me that rubbish.
In future please obey the rules & do not shout.
 
Upvote 0

Forum statistics

Threads
1,215,819
Messages
6,127,047
Members
449,356
Latest member
tstapleton67

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