Hi Im beginner to VBA excel
I have written a code which autofilter all the columns to my requirement. My requirement is ,
- the result has to be displayed in new sheet(say sheet2) rather showing in the same sheet(say sheet1).
- Suppose, if i excute the code mutiple times, it always open only one sheet( i.e sheet2) not many sheets as well as it auto refresh the sheet2 if i excute the code again and must display the expected result.
Here is my code:
Sub stack()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim filterrange As Range
Set ws1 = ThisWorkbook.Sheets("sheet1")
Set ws2 = ThisWorkbook.Worksheets.Add(after:=ActiveSheet)
ws2.Name = "abc"
Set filterrange = ThisWorkbook.Sheets("sheet1").Cells(2, ThisWorkbook.Sheets("sheet1").Cells(1, Columns.Count).End(xlToLeft).Column) ' get columns e.g. name, state, etc.
filterrange.AutoFilter Field:=11, Criteria1:=Array("GBR" _
, "MAD", "NCE", "="), Operator:=xlFilterValues
filterrange.AutoFilter Field:=21, Criteria1:="Yes" ' activeconnect
filterrange.AutoFilter Field:=24, Criteria1:="=" ' clustername
filterrange.AutoFilter Field:=6, Criteria1:= _
"<>*@sca.com*", Operator:=xlAnd ' e-mail
filterrange.AutoFilter Field:=10, Criteria1:=Array( _
"Madrid", "Sophia-antipolis"), Operator:=xlFilterValues
For Each cell In filterrange.CurrentRegion.SpecialCells(xlCellTypeVisible).Rows
If Cells(cell.Row, 24) = "" Then
Select Case Cells(cell.Row, 11).Value
Case "NCE"
Cells(cell.Row, 24) = "ncew.net"
Case "MAD"
Cells(cell.Row, 24) = "muc.net"
End Select
End If
Next cell
filterrange.SpecialCells(xlCellTypeVisible).Copy
ws2.Activate
ws2.Range("a1").PasteSpecial (xlPasteValues)
End Sub
My code is showing same result in two different sheets( i.e sheet1 and sheet2). The actual data must remain unchanged in sheet1 and the result should be displayed in sheet2. can anyone please help me out.