jeudi 21 novembre 2019

VBA Copy Worksheet to a new Workbook with conditions

I am quite new to VBA and came to an issue where I am struggling. I am aiming to copy over a worksheet to a new workbook if certain condition is met.

If my output row says "1" (which is also in a different worksheet lets say Sheet2) for my row number than it should copy the field/fields to a new workbook but if it my output row is empty for a specific number than it should empty or delete the row or column.

It should look like follows: Index

if that condition is met like index 3 has a output of 1, then my Code should Copy that row and all other rows where there is a 1 and paste them into a new workbook. For the rows where there is no 1 it should empty or delete them. The result should look as follows: Result

The difficulty is that for the rows and columns from 1 to 4 I want the code to empty the cells if <> 1 and for 5 onward I want the code to delete the entire columns.

For 1 to 4 I tried something like that:

 Worksheets("Sheet1").Select
ActiveSheet.Buttons.Delete
ActiveSheet.Range("G1:AZ29").Clear


For i = 1 To 12
    If Worksheets("Sheet2").Cells(11, i).Value = 1 Then

    Else
       Row = ActiveSheet.Range("A15:E26").Find(index_range.Columns(i))
       ActiveSheet.Range(Cells(Row, 2), Cells(Row, 5)).Clear
    End If
Next i

Though I got an error message 1004: "We can't do that to a merged cell"

Any help would be appreciated

Aucun commentaire:

Enregistrer un commentaire