Please help = VBA code not working

Rehmatm

New Member
Joined
Mar 17, 2014
Messages
6
Can somebody please help!!

I have set this macro up on a button, so whenever it is clicked a mastersheet is populated from the data in sheets "test1" and "test2".

What i ideally want is everytime its clicked it replaces the old data and replaces. (to take into consideration of any changes) however at the moment it is adding in the data again. please please can someone help.

Thanks in advance

R

Code:
 Private Sub CommandButton1_Click()
Dim Sheet As Worksheet
For Each Sheet In Me.Parent.Sheets
    If Sheet.Name = "test1" Or _
    Sheet.Name = "test2" Then
        If Sheet.Cells(Rows.Count, 1).End(xlUp).Row <> 1 Then
            Sheet.Range(Sheet.Cells(2, 1), Sheet.Cells(Sheet.Cells(Rows.Count, 1).End(xlUp).Row, 10)).Copy Destination:=Me.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        End If
    Else
        Me.Range(Cells(20, 20), Cells(Rows.Count, 10)).Clear
    End If
Next Sheet
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
For instance:

Code:
Private Sub CommandButton1_Click()

    Dim Sheet As Worksheet

    Me.Range(Cells(20, 20), Cells(Rows.Count, 10)).Clear

    For Each Sheet In Me.Parent.Sheets
        With Sheet
            If .Name = "test1" Or .Name = "test2" Then
                If .Cells(.Rows.Count, 1).End(xlUp).Row <> 1 Then
                    .Cells(1).CurrentRegion.Resize(, 10).Offset(1).Copy Destination:=Me.Cells(.Rows.Count, 1).End(xlUp).Offset(1)
                End If
            End With
        End If
    Next
    
End Sub
 
Upvote 0
I would appreciate if you would post an update, and give feedback.
 
Upvote 0
Thank you for the update!
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

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