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
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,944
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
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,944
I would appreciate if you would post an update, and give feedback.
 

Forum statistics

Threads
1,081,575
Messages
5,359,711
Members
400,545
Latest member
Damntheman30

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top