clear sheet when new value found in range

VeryForgetful

Board Regular
Joined
Mar 1, 2015
Messages
242
Hi,

Can anyone assist with the code below please.

I'm looking to clear my destinatio sheet (excluding the header row) each time a new value is located in column A.

So for example, if I have the following account numbers:

12345
12345
12345
123456

It will list 3 rows for 12345 then as soon as it encounters a different account number the sheet will clear and the loop will start doing the same again for 123456 etc...

My code errors on accountCheck = TrgSheet.Cells.Find(what:=account, LookIn:=xlFormulas, lookat:=xlWhole)

Object variable or With block variable not set


Code:
Sub SplitData()


    Const NameCol = "A"
    Const HeaderRow = 1
    Const FirstRow = 2
    
    Dim SrcSheet As Worksheet
    Dim TrgSheet As Worksheet
    Dim SrcRow As Long
    Dim LastRow As Long
    Dim TrgRow As Long
    Dim account As String
    Dim accountCheck As Range
    
    'Application.ScreenUpdating = False
    
    Set SrcSheet = Sheet1
    Set TrgSheet = Sheet7
    LastRow = SrcSheet.Cells(SrcSheet.Rows.Count, NameCol).End(xlUp).Row
     
    For SrcRow = FirstRow To LastRow
        account = SrcSheet.Cells(SrcRow, NameCol).Value
        accountCheck = TrgSheet.Cells.Find(what:=account, LookIn:=xlFormulas, lookat:=xlWhole)
        
        If Not accountCheck Is Nothing Then
            Range(TrgSheet.UsedRange.Rows(2), TrgSheet.UsedRange.Rows(TrgSheet.UsedRange.Rows.Count)).Delete
        End If
        
        TrgSheet.Name = account
        TrgRow = TrgSheet.Cells(TrgSheet.Rows.Count, NameCol).End(xlUp).Row + 1
        SrcSheet.Rows(SrcRow).Copy Destination:=TrgSheet.Rows(TrgRow)
        Debug.Print account
        
        'rest of code here to..................
        
    Next SrcRow
    
    'Application.ScreenUpdating = True
    
End Sub

Thanks
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Code:
accountCheck = TrgSheet.Cells.Find(what:=account, LookIn:=xlFormulas, lookat:=xlWhole)

Since accountCheck is a Range object, you need to use a Set statement...

Code:
Set accountCheck = TrgSheet.Cells.Find(what:=account, LookIn:=xlFormulas, lookat:=xlWhole)

Cheers,

tonyyy
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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