Copy cell values to another sheet

A

Albert

Hi Guys,

I am trying to create a macro (unsuccessfully) to:
1. Create a new sheet
2. select a range
3. loop through the range until the range is empty
4. check criteria of cells in range
5. if true copy 2of the cells in the row range to the new worksheet
6. loop through range and create a list in the new worksheet

This is what I got so far:
Sub copyCells()
' Testsubmit Macro

'Declare variables
Dim wb As Workbook
Dim wssheet1 As Worksheet
Dim wssheet2 As Worksheet
Dim ws As Worksheet
Dim newsh As Worksheet
Dim DestinationRange As Range
Dim SourceRange As Range

Dim lRow As Integer


Set wb = ThisWorkbook
Set newsh = wb.Worksheets.Add
newsh.Name = "Orders for next month"
newsh.Range("a1") = "Orders for the month"
newsh.Range("a2") = "Item"
newsh.Range("b2") = "Quantity"

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Do Until IsEmpty(SourceRange)
Set SourceRange = wb.Worksheets("sheet1").Range("a1")
If SourceRange.Offset(1, 0) = "Food" And SourceRange.Offset(1, 0) >
"0" Then
Set DestinationRange = wb.Worksheets("Orders for the month").Range("a1")
SourceRange.Offset(1, 0) = DestinationRange.Offset(3, 0)
SourceRange.Offset(1, 5) = DestinationRange.Offset(3, 1)

Loop
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

Please help?

Thanks
Albert
 
M

Mike H

Albert,

It isn't clear what cells in the row you want to copy. have a look at the
code below and in the bit where we find 'Food' tell us what you want copying.

Bear in mind that when using OFFSET the syntax is OFFSET(Row, Column)

Sub copyCells()
' Testsubmit Macro

'Declare variables
Dim wb As Workbook
Dim ws As Worksheet
Dim newsh As Worksheet
Dim MyRange As Range
Dim LastRow As Long


With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set wb = ThisWorkbook
Set newsh = wb.Worksheets.Add
newsh.Name = "Orders for next month"
newsh.Range("a1") = "Orders for the month"
newsh.Range("a2") = "Item"
newsh.Range("b2") = "Quantity"


LastRow = wb.Worksheets("sheet1").Cells(Cells.Rows.Count, "A").End(xlUp).Row
Set MyRange = wb.Worksheets("sheet1").Range("A1:A" & LastRow)


For Each c In MyRange


If c.Offset(, 1) = "Food" Then
'tell us which cells to copy
End If

Next
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
A

Albert

Hi Mike,

Thanks for the response.

Below is the source table

Category Item Value Quantity Order
Food cereal 12 2 1
Food pet 39 2 0
Garden Outside 78 2 1
Garden Inside 34 2 1

If category = "Food" and order > 0 then copy item and order values to new
worksheet for the full source range.

I hope this helps
 
A

Albert

Hi Mike I also found some code from Ron de Bruin (brilliant)

But still it copies the full autofiltered range headings(columns) to a new
worksheet.

How could I adapt this code to just copy "name" and "birthday" to a new sheet?

'<<<< Filter/Copy to new worksheet (It will ask you for the sheet name)
'This example will add a new worksheet to your workbook (the code will ask
you for a sheet name)
'and will copy every record with Netherlands in the first column to this new
worksheet.

'In the code you see four filter examples that you can use, we use example 1
in this macro.
'1: Criteria in the code (=Netherlands, see the tips below the macro)
'2: Filter on ActiveCell value
'3: Filter on Range value (D1 in this example)
'4: Filter on InputBox value

'Note: this example use the function LastRow in the ModReset module

Sub Copy_With_AutoFilter1()
'Note: This macro use the function LastRow
Dim My_Range As Range
Dim CalcMode As Long
Dim ViewMode As Long
Dim FilterCriteria As String
Dim CCount As Long
Dim WSNew As Worksheet
Dim sheetName As String
Dim rng As Range

'Set filter range on ActiveSheet: A11 is the top left cell of your
filter range
'and the header of the first column, D is the last column in the filter
range.
'You can also add the sheet name to the code like this :
'Worksheets("Sheet1").Range("A11:D" & LastRow(Worksheets("Sheet1")))
'No need that the sheet is active then when you run the macro when you
use this.
Set My_Range = Range("A11:D" & LastRow(ActiveSheet))
My_Range.Parent.Select

If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is
protected", _
vbOKOnly, "Copy to new worksheet"
Exit Sub
End If

'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False

'Firstly, remove the AutoFilter
My_Range.Parent.AutoFilterMode = False

'Filter and set the filter field and the filter criteria :
'This example filter on the first column in the range (change the field
if needed)
'In this case the range starts in A so Field 1 is column A, 2 = column
B, ......
'Use "<>Netherlands" as criteria if you want the opposite
My_Range.AutoFilter Field:=1, Criteria1:="=Netherlands"

'If you want to filter on a cell value you can use this, use "<>" for
the opposite
'This example uses the activecell value
'My_Range.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value

'This will use the cell value from A2 as criteria
'My_Range.AutoFilter Field:=1, Criteria1:="=" & Range("A2").Value

''If you want to filter on a Inputbox value use this
'FilterCriteria = InputBox("What text do you want to filter on?", _
' "Enter the filter item.")
'My_Range.AutoFilter Field:=1, Criteria1:="=" & FilterCriteria

'Check if there are not more then 8192 areas(limit of areas that Excel
can copy)
CCount = 0
On Error Resume Next
CCount =
My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas:" _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Copy to worksheet"
Else
'Add a new Worksheet
Set WSNew = Worksheets.Add(After:=Sheets(ActiveSheet.Index))

'Ask for the Worksheet name
sheetName = InputBox("What is the name of the new worksheet?", _
"Name the New Sheet")

On Error Resume Next
WSNew.Name = sheetName
If Err.Number > 0 Then
MsgBox "Change the name of sheet : " & WSNew.Name & _
" manually after the macro is ready. The sheet name" & _
" you fill in already exists or you use characters" & _
" that are not allowed in a sheet name."
Err.Clear
End If
On Error GoTo 0

'Copy/paste the visible data to the new worksheet
My_Range.Parent.AutoFilter.Range.Copy
With WSNew.Range("A1")
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With

' If you want to delete the rows that you copy, also use this
' With My_Range.Parent.AutoFilter.Range
' On Error Resume Next
' Set rng = .Offset(1, 0).Resize(.Rows.Count - 1,
..Columns.Count) _
' .SpecialCells(xlCellTypeVisible)
' On Error GoTo 0
' If Not rng Is Nothing Then rng.EntireRow.Delete
' End With

End If

'Close AutoFilter
My_Range.Parent.AutoFilterMode = False

'Restore ScreenUpdating, Calculation, EnableEvents, ....
My_Range.Parent.Select
ActiveWindow.View = ViewMode
If Not WSNew Is Nothing Then WSNew.Select
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With

End Sub



'In the example I filter on the first column for the Netherlands
' My_Range.AutoFilter Field:=1, Criteria1:="=Netherlands"
'
'But you can also repeat the line for other fields.
'
'This will filter all males from the Netherlands (column A and C in my
example)
' My_Range.AutoFilter Field:=1, Criteria1:="=Netherlands"
' My_Range.AutoFilter Field:=3, Criteria1:="=M"
'
'Use this to filter for all males from the Netherlands and the USA (column A
and C in my example)
'I use two criteria in field 1 (2 is the maximum for AutoFilter in one field)
' My_Range.AutoFilter Field:=1, Criteria1:="=Netherlands", Operator:=xlOr,
Criteria2:="=USA"
' My_Range.AutoFilter Field:=3, Criteria1:="=M"
'
'Use this to filter for all males born between 23 Feb 1947 and 7 May 1988
from the Netherlands and the USA
'(column A, C and D in my example). I use two criteria in field 1 and 4 (2
is the maximum for AutoFilter)
' My_Range.AutoFilter Field:=1, Criteria1:="=Netherlands", Operator:=xlOr,
Criteria2:="=USA"
' My_Range.AutoFilter Field:=3, Criteria1:="=F"
' My_Range.AutoFilter Field:=4, Criteria1:=">=02/23/1947", _
' Operator:=xlAnd, Criteria2:="<=05/07/1988"
'
'Important: Use always the US mm/dd/yyyy format if you filter Dates.
'Note: You only have the use the mm/dd/yyyy format in the code, no problem
'if the format in the worksheet is different.

'Tip: If you want to copy every record that has a value in the first column
that starts with
'Netherlands you can use Criteria1:="=Netherlands*"
'Or if Netherlands is a part of the string Criteria1:="=*Netherlands*"
'You can also use the wildcard ? for one character.
 
M

Mike H

Albert,

Try this. I'm still not entirely sure which 2 cells to copy. It currently
copies the ITEM and ORDER cells from each row for that's incorrect then this
is the bit off code that selecst the cells to copy

If CopyRange Is Nothing Then
Set CopyRange = Union(c.Offset(, 1), c.Offset(, 4))
Else
Set CopyRange = Union(CopyRange, Union(c.Offset(, 1), c.Offset(, 4)))
End If

Simply change the offset values but you must leave the comma there. As you
see i copy the cells 1 & 4 to the right of the cell with FOOD in. you must
change both lines


Sub copyCells()
Dim wb As Workbook
Dim ws As Worksheet
Dim newsh As Worksheet
Dim MyRange As Range
Dim LastRow As Long
Dim CopyRange As Range

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set wb = ThisWorkbook
Set newsh = wb.Worksheets.Add
newsh.Name = "Orders for next month"
newsh.Range("a1") = "Orders for the month"
newsh.Range("a2") = "Item"
newsh.Range("b2") = "Quantity"

LastRow = wb.Worksheets("sheet1").Cells(Cells.Rows.Count, "A").End(xlUp).Row
Set MyRange = wb.Worksheets("sheet1").Range("A1:A" & LastRow)
For Each c In MyRange
If UCase(c.Value) = "FOOD" And c.Offset(, 4) > 0 Then
If CopyRange Is Nothing Then
Set CopyRange = Union(c.Offset(, 1), c.Offset(, 4))
Else
Set CopyRange = Union(CopyRange, Union(c.Offset(, 1), c.Offset(, 4)))
End If
End If
Next

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

If Not CopyRange Is Nothing Then
CopyRange.Copy Sheets("Orders for next month").Range("A3")
End If
End Sub

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
M

Mike H

Albert see my previous answer. There's no need to filter the range to do what
you want
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
A

Albert

Hi mike,

When I run the code I get a message the "For Each c" is not defined.
Do I
Dim c as integer?

What I am trying to do is list items and orders on a seperate worksheet that
are category is equal to food and the order is > 0.

So I should only get a list of these column values on the second sheet.

Thanks
Albert
 
A

Albert

Hi Mike,

Please could you show me how to only paste certain columns on the second
worksheet (instead of all the range headers), for future use?

I must admit I prefer Ron's code.

Thanks
Albert
 
M

Mike H

Dim C as range
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
A

Albert

Hi Mike,

Works like a charm

Thank you
Albert

Mike H said:
Dim C as range
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top