vba code select page, hide columns and filter values

DanielFran

New Member
Joined
Apr 19, 2018
Messages
35
Hi,

My objective is to select a worksheet which has several columns, and I need to filter a specific column for true values; then i need to copy some columns (not all), therefore I am hiding these and paste it into another worksheet.

The recorded macro i have does the job but it sometimes selects the wrong worksheet.

The macro I have is the following:

Sub ActiveItem()
'
' ActiveItem Macro
'


Range("B4:I5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("B4").Select
Workbooks.Open Filename:="H:\Client Project\Employee Data.xlsx"
ActiveWindow.ScrollColumn = 30
ActiveWindow.ScrollColumn = 29
ActiveWindow.ScrollColumn = 28
ActiveWindow.ScrollColumn = 27
ActiveWindow.ScrollColumn = 26
ActiveWindow.ScrollColumn = 25
ActiveWindow.ScrollColumn = 24
ActiveWindow.ScrollColumn = 23
ActiveWindow.ScrollColumn = 22
ActiveWindow.ScrollColumn = 21
ActiveWindow.ScrollColumn = 20
ActiveWindow.ScrollColumn = 19
ActiveWindow.ScrollColumn = 18
ActiveWindow.ScrollColumn = 17
ActiveWindow.ScrollColumn = 16
ActiveWindow.ScrollColumn = 15
ActiveWindow.ScrollColumn = 14
ActiveWindow.ScrollColumn = 13
ActiveWindow.ScrollColumn = 11
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Columns("F:Y").Select
Selection.EntireColumn.Hidden = True
Columns("AA:AE").Select
Selection.EntireColumn.Hidden = True
ActiveSheet.ListObjects("fEmployee").Range.AutoFilter Field:=4, Criteria1:= _
"TRUE"
Columns("E:E").Select
Selection.EntireColumn.Hidden = True
Range("B2:AI6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Data Operations.xlsm").Activate
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B2").Select
Windows("Employee Data.xlsx").Activate
ActiveWindow.Close
Range("B2").Select
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi Fluff,

1)The first workbook is "Data Operation" and sheet is "TRUE" that clear contents
2)The second workbook that opens is Workbooks.Open Filename:="H:\Client Project\Employee Data.xlsx" and worksheet is "Employee" where the hide columns and autofilter happens
3)The third workbook is "Data Operation" and sheet is "TRUE" and where all the data from the 2nd worksheet is copied and to be pasted here
 
Upvote 0
How about
Code:
Sub ActiveItem()
   Dim Wbk As Workbook
   Dim Tws As Worksheet, Ews As Worksheet
   
   Set Tws = Sheets("True")

   Tws.Range("B4", Tws.Range("B" & Rows.count).End(xlUp)).Resize(, 8).ClearContents
   Set Wbk = Workbooks.Open("H:\Client Project\Employee Data.xlsx")
   Set Ews = Wbk.Sheets("Employee")
   Ews.Columns("F:Y").Hidden = True
   Ews.Columns("AA:AE").Hidden = True
   Ews.ListObjects("fEmployee").Range.AutoFilter Field:=4, Criteria1:="TRUE"
   Ews.Columns("E:E").Hidden = True
   Ews.Range("B2", Ews.Range("B" & Rows.count).End(xlUp)).Resize(, 34).Copy
   Tws.Range("B4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
      :=False, Transpose:=False
   Wbk.Close False
   
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top