[a1:a10].clearcontents
[a1].currentregion.clearcontents
Cells.ClearContents
Sub CopyData()
Dim PstRng As Range
Dim CopyRange As Range
Set CopyRange = Sheet1.UsedRange
With ActiveSheet
'Where are we copying to
Set PstRng = Intersect(.UsedRange, .Range("1:1"))
'Clear old data
.Range("2:" & .Rows.Count).EntireRow.ClearContents
'Copy via Filter
CopyRange.AdvancedFilter xlFilterCopy, , PstRng
End With
'Clear contents of cells
CopyRange.ClearContents
MsgBox "Done"
End Sub
Option Explicit
Sub CopyData()
Dim i As Integer, lastRow As Long
Dim PstRng As Range
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Sheet2.[2:65536].EntireRow.Delete
For i = 1 To 256
Set PstRng = Sheet2.Range("1:1").Find(Cells(1, i).Value, [A1], xlValues, xlWhole)
If Not PstRng Is Nothing Then
Range(Cells(2, i), Cells(lastRow, i)).Copy PstRng.Offset(1, 0)
End If
Next i
MsgBox "Done"
End Sub
Luke M ur macro work perfect i want clear all data in sheet1& sheet2This macro looks a lot like one you posted previously, which I showed you how to improve here:
http://chandoo.org/forum/threads/modified-the-macro.15827/#post-95598
Also, you need to be clearer about which cells you want to clear. You should say "Clear the cells that I copied from", or "Clear the cells on Sheet2", or something like that. Since you weren't precise, I will guess that you want to clear the cells your are copying from (otherwise, why paste them??).
Modifying the macro that I previously linked to:
Code:Sub CopyData() Dim PstRng As Range Dim CopyRange As Range Set CopyRange = Sheet1.UsedRange With ActiveSheet 'Where are we copying to Set PstRng = Intersect(.UsedRange, .Range("1:1")) 'Clear old data .Range("2:" & .Rows.Count).EntireRow.ClearContents 'Copy via Filter CopyRange.AdvancedFilter xlFilterCopy, , PstRng End With 'Clear contents of cells CopyRange.ClearContents MsgBox "Done" End Sub
Final thoughts
- If this doesn't work, please indicate precisely what is wrong. Saying "It's not working" is the least helpful.
- When you start a thread, try to give as much information as possible. Notice in this thread, you started with a single sentence, and then later posted the macro. Posting the macro you were using would have saved a step.
Except the headers in sheet2 rest all data i want clearLuke M ur macro work perfect i want clear all data in sheet1& sheet2
I'll take that to mean that this is solved, then? Or are you wanting to paste the data into Sheet2, and then delete it?Luke M ur macro work perfect i want clear all data in sheet1& sheet2
Data is huge range so Formula i think not work proper it will take time to get the resultAt which number do you want to delete all the data? Parenthesis indicates what process currently controls.
If we clear Sheet2 at end of macro, you won't be able to see the data that just got copied, and this becomes useless. If you are constantly changing the data in Sheet1, I would propose forgetting the macro altogether and just using a quick HLOOKUP formula, like:
- Headers are in Sheet2 (manual)
- Data is Sheet1 (manual)
- Any previous copied data in sheet2 is cleared (macro)
- New copy from Sheet1 (macro)
- Sheet1 is cleared (macro)
=HLOOKUP(A$1,Sheet1!$1:$2,2,FALSE)
Ah, a 2nd macro. That was not clear before. In that case, something like this is fine:Luke M 1st Run macro give data in sheet 2 Then i run 2nd macro that clear data except sheet2 headers rest of data means sheet1 all data sheet 2 data from row 2 below datai want clear. Two Different buttons i want for macro to run
Sub CopyData()
Dim PstRng As Range
Dim CopyRange As Range
Set CopyRange = Sheet1.UsedRange
With ActiveSheet
'Where are we copying to
Set PstRng = Intersect(.UsedRange, .Range("1:1"))
'Clear old data
.Range("2:" & .Rows.Count).EntireRow.ClearContents
'Copy via Filter
CopyRange.AdvancedFilter xlFilterCopy, , PstRng
End With
'Clear contents of cells
CopyRange.ClearContents
MsgBox "Done"
End Sub
'This the the 2nd macro
Sub ClearData()
Sheet1.Cells.ClearContents
With Sheet2
.Range("2:" & .Rows.Count).ClearContents
End With
End Sub
From your previous macro, it looks like it's only transferring 256 cells, which would need 256 formulas, which is not intensive for XL.Data is huge range so Formula i think not work proper it will take time to get the result