Copy sheets only if condition is satisfied (mat)

  • Thread starter Thread starter matthias
  • Start date Start date
M

matthias

Hi guys,

i want a macro to do the following:

I have a masterfile with several sheets. If I run the macro i want it
to copy sheet1 and/orsheet2 and /or sheet 3 to a new workbook
depending on a condition.

Sheet 1 has to be copied always
Sheet 2 has to be copied only if the cell A2 from sheet 1 is the same
as the cell A2 from sheet 2
Sheet 3 has to be copied only if the cell A2 from sheet 1 is the same
as the cell A2 from sheet3

the sheets have to be copied as values and with the format off the
sheets from the masterfile.

Thanks guys

mat
 
Hi matthias

Try this basic code example

Sub TestCopy()
If Sheets("Sheet1").Range("A2").Value = Sheets("Sheet2").Range("A2").Value Then
If Sheets("Sheet1").Range("A2").Value = Sheets("Sheet3").Range("A2").Value Then
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Copy
Else
Sheets(Array("Sheet1", "Sheet2")).Copy
End If
Else
If Sheets("Sheet1").Range("A2").Value = Sheets("Sheet3").Range("A2").Value Then
Sheets(Array("Sheet1", "Sheet3")).Copy
Else
Sheets("Sheet1").Copy
End If
End If

Worksheets.Select
Cells.Copy
Cells.PasteSpecial xlPasteValues
Cells(1).Select
Worksheets(1).Select
Application.CutCopyMode = False

End Sub
 
thanks ron!! now, can I ask you another thing?
now the macro runs perfectly, but can i also make sure that the copies
of the sheets are in "landscape form" as the originals?

thanks

ps ik spreek ook nederlands
 
I test it with one of the sheets set to landscape and in the new workbook it is also landscape ?
Do you get a different result
 
hi maybe one extra question

if i run the macro via a button, he copies the button to the new
workbook since it is part of sheet1. how can i avoid this. if know it
is something with shapes...(it is a button from forms), it has to be
put in the code above?

mat
 
you can delete the shapes on the activesheet with this


Sub Forms1()'Delete All Forms buttons ActiveSheet.Buttons.DeleteEnd Sub
 
This is better

Sub Forms1()
'Delete All Forms buttons
ActiveSheet.Buttons.Delete
End Sub
 

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

Back
Top