Object variable or with block not set error

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I got this code for @DanteAmor . I expect bring data from multiple sheet into sheet TOTAL based on match the headers in sheet TOTAL with the headers in others sheets , but it gives "Object variable or with block not set error" in this line
VBA Code:
lr = sh.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
Code:
Sub through_all_sheets()
  Dim sh1 As Worksheet, sh As Worksheet
  Dim f As Range
  Dim j As Long, lr1 As Long, lr As Long
 
  Set sh1 = Sheets("Total")
 
  For Each sh In Sheets
    If sh.Name <> sh1.Name Then
      lr1 = sh1.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
      lr = sh.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
      For j = 1 To sh.Cells(1, Columns.Count).End(1).Column
        Set f = sh1.Rows(1).Find(sh.Cells(1, j), , xlValues, xlWhole, , , False)
        If Not f Is Nothing Then
          sh1.Cells(lr1, f.Column).Resize(lr).Value = sh.Cells(2, j).Resize(lr).Value
        End If
      Next
    End If
  Next
End Sub
any help to fix this error ,please?
 

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
You may have a blank worksheet(s). Does this work?

VBA Code:
Sub through_all_sheets()
 
    Dim sh1 As Worksheet, sh As Worksheet
    Dim f As Range
    Dim j As Long, lr1 As Long, lr As Long
    
    Set sh1 = Sheets("Total")
    
    For Each sh In Sheets
        If sh.Name <> sh1.Name And sh.UsedRange.Address <> "$A$1" Then
            lr1 = sh1.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
            lr = sh.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
            For j = 1 To sh.Cells(1, Columns.Count).End(1).Column
                Set f = sh1.Rows(1).Find(sh.Cells(1, j), , xlValues, xlWhole, , , False)
                If Not f Is Nothing Then
                    sh1.Cells(lr1, f.Column).Resize(lr).Value = sh.Cells(2, j).Resize(lr).Value
                End If
            Next
        End If
    Next
End Sub
 
Upvote 0
That is because you have a blank sheet.
Try this:

VBA Code:
Sub through_all_sheets()
  Dim sh1 As Worksheet, sh As Worksheet
  Dim f As Range
  Dim j As Long, lr1 As Long, lr As Long
  Dim n As Variant
  
  Set sh1 = Sheets("Total")
 
  For Each sh In Sheets
    If sh.Name <> sh1.Name Then
      lr1 = sh1.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
      On Error Resume Next
      lr = sh.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
      n = Err.Number
      On Error GoTo 0
      If n = 0 Then
        For j = 1 To sh.Cells(1, Columns.Count).End(1).Column
          Set f = sh1.Rows(1).Find(sh.Cells(1, j), , xlValues, xlWhole, , , False)
          If Not f Is Nothing Then
            sh1.Cells(lr1, f.Column).Resize(lr).Value = sh.Cells(2, j).Resize(lr).Value
          End If
        Next
      End If
    End If
  Next
End Sub
 
Upvote 0
guys you're right . I have sheet is empty .I deleted . the error is gone, but unfortunately your code both doesn't work at all.
what's the problem?:unsure:
 
Upvote 0
.... but unfortunately your code both doesn't work at all.
what's the problem?:unsure:

I'm not sure what you mean by "doesn't work". Can you please explain in more detail?

If you have deleted any blank sheet(s), then all three codes should work identically.
 
Upvote 0
it doesn't show data under column which contain the header in sheet Total based on others sheets. there is no error .
 
Upvote 0
First, the names of the headings of all the sheets must be in row1. Also the "Total" sheet headings must be in row 1.
Second, the names of the sheet headings must match the names of the headings on the "Total" sheet.
Finally, put here a sample of one of your sheets and your "Total" sheet, where we can see the names of the headings.
 
Upvote 0
@DanteAmor thanks for your advices. I re-arranged data again and works , but I have some problems . based on the picture shows empty cells when run the macro repeatedly. it shouldn't be

second when run the macro continuously .it repeats copying data have already copy to the bottom I want just if there is new data then should copy to the bottom

finally I would implement the code just for sheets(sh1,sh2) not all sheets .
aa.xlsx
ABCD
21A-SS1123
32A-SS2124
43A-SS356
54A-SS478
65A-SS599
76A-SS656
87A-SS734
98A-SS845
101A-SS115
112A-SS220
123A-SS330
134A-SS440
145A-SS534
156A-SS655
167A-SS712
178A-SS813
181A-SS1123
192A-SS2124
203A-SS356
214A-SS478
225A-SS599
236A-SS656
247A-SS734
258A-SS845
261A-SS115
272A-SS220
283A-SS330
294A-SS440
305A-SS534
316A-SS655
327A-SS712
338A-SS813
Total


thanks again
 
Upvote 0
finally I would implement the code just for sheets(sh1,sh2) not all sheets .

Update the name of the sheets you need in this line:
VBA Code:
Case "sh1", "sh2"

Try this:

VBA Code:
Sub through_all_sheets()
  Dim sh1 As Worksheet, sh As Worksheet
  Dim f As Range
  Dim j As Long, lr1 As Long, lr As Long
  Dim n As Variant
  
  Set sh1 = Sheets("Total")
  sh1.Rows("2:" & Rows.Count).ClearContents
  
  For Each sh In Sheets
    Select Case sh.Name
      Case "sh1", "sh2"
        For j = 1 To sh.Cells(1, Columns.Count).End(1).Column
          lr = sh.Cells(Rows.Count, j).End(3).Row
          Set f = sh1.Rows(1).Find(sh.Cells(1, j), , xlValues, xlWhole, , , False)
          If Not f Is Nothing Then
            lr1 = sh1.Cells(Rows.Count, f.Column).End(3).Row + 1
            sh1.Cells(lr1, f.Column).Resize(lr).Value = sh.Cells(2, j).Resize(lr).Value
          End If
        Next
    End Select
  Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,965
Messages
6,122,495
Members
449,088
Latest member
Melvetica

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