简体   繁体   中英

Copy Filtered Data to new sheet

From the below-filtered range, I need to modify data and copy it to new sheet with Excel VBA.

  1. Fetch top 3 (Max) values below a reference value (ex: <= 9) from right side and copy the data w.r.t to least sector code. for example: In figure: top 3 below 9 (right side) are 2586, 1523, 124. so least sector code is 5, copy the data from 9-5 (sector code) entire row to a new sheet.
  2. similarly fetch the top 3 data above the reference value (9) from left side and copy the data w.r.t to highest sector code. for example: In figure top 3 above 9 (left side) are 1000,356,129 and copy the data from 9-15 below of data fetched in 1.
  3. The reference value is dynamic and will be provided from cell value.

在此处输入图像描述

The final data will look like

在此处输入图像描述

I'm very new to VBA and have been learning. Any help is greatly appreciated.

A very useful tool to retrieve VBA code for determined action is the macro recorder, in the ribbon, Developer -> RecordMacro, perform you action and stop recording and then you can check the code generated for the actions you recorded. Its not the cleanest code but you can find there the lines of code for the specific actions you want. Once you step into a one concrete problem with the code you tried, you can then ask for help regarding something more concrete, more than expecting that someone will code that for you.

Hope that helps

From the macro recorder, to sort a set of values, it retrieves this code:

Sub Macro2()
    ActiveWorkbook.Worksheets("Hoja1").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Hoja1").AutoFilter.Sort.SortFields.Add Key:=Range( _
        "A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Hoja1").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

you can start from there. Try the macro recorder and record the user actions that lead tu the uotput you want. You can also try to record little steps to understand the recorded from the user action, to use that however you like.

Try out the macro recorder, its an amazing tool, and if you know what you want to do with user actions you just need to record that actions, and then fix the code a bit.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM