Copying filtered rows to a different sheet with VBA, but with a twist

downrightcynical

New Member
Joined
Mar 28, 2022
Messages
3
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello all! I am a new user here hoping to resolve a slight issue I am having with VBA...

I am trying to use a simple button function to send filtered out rows to another sheet by way of VBA.
I have managed to accomplish copying over the entire row, but I need to add a slight twist. Instead of copying the entire row, I would like to only copy a specific column from the source row and paste that columns data into a predetermined column in the end sheet then repeat this process for the rest of the data in the source row(s). The reason why I want to do it this way is because the format of the two sheets is drastically different and I must specify column by column where the data is going to go, copying the whole row simply will not work for handling the data.

The code I am working with now appears like so:

VBA Code:
   iRow = sht2.Range("B" & Application.Rows.Count).End(xlUp).Row + 1

   With frm
        
        ActiveSheet.UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("screen").Cells(iRow, 1)
        
    End With

I am using the offset function to prevent the sheet from copying the source header.

Thank you!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I think we need more information. Which columns in the source sheet go to which columns in the destination sheet? Do they get copied to a particular row, or appended to the bottom of existing data? Could you provide your entire code (not just a snippet) and a sample of your data using the XL2BB Tool?
 
Upvote 0
I assume that you are just looking for an example?
This would copy the filtered values in column C of the active sheet to column J, starting at row iRow, of sheet 'screen'

Rich (BB code):
Intersect(ActiveSheet.UsedRange.Offset(1, 0), Columns("C")).SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("screen").Cells(iRow, "J")
 
Upvote 0
Solution
I think we need more information. Which columns in the source sheet go to which columns in the destination sheet? Do they get copied to a particular row, or appended to the bottom of existing data? Could you provide your entire code (not just a snippet) and a sample of your data using the XL2BB Tool?

Sure thing!

1.) From the source sheet (database) I am pulling all the columns to the destination sheet (screen), they are going in a random with no recognizable pattern. Column 1 and 2 will go both to 1 and 2 at the destination, column 3 from source will go into 7 at its destination, 4 -> 6, we skip 5, 6 -> 3, 7 -> 4, 8 -> 5, 9 -> 8, 9 -> 9, 10 -> 8, and there are a few more but it continues in random fashion.

2.) They will append at the bottom of the destination sheet.

3.) Full Code (its pretty messy, sorry im a novice)
VBA Code:
Sub Screen_Send()

    Dim frm As Worksheet
    Dim sht2 As Worksheet
    Dim lastRow As Long
    Dim iRow As Long
    Dim lstrow As Integer
    
    Set frm = Sheets("database")
    Set sht2 = Sheets("screen")
    
    iRow = sht2.Range("B" & Application.Rows.Count).End(xlUp).Row + 1
    lastRow = ActiveSheet.UsedRange.Rows.Count
    
    With frm
        
        ActiveSheet.UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("screen").Cells(iRow, 1)
        
    End With
    
End Sub

4.) Sample data:
Soyboy Portfolio.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1tickeridnamewritesectorindustry groupindustrysub industryrev tagnationownershipscreenmore 1more 2flavorpricemktcapvaluevalue %confidencevalue typetrackerinst. %sins. %sdate added
19ABT19ABBOTT LABORATORIES (XNYS:ABT)Makes critical healthcare products for the treatment of Diabities amongst other common ailments.Health CareHealth Care Equipment & ServicesHealth Care Equipment & SuppliesHealth Care Equipmentmultinationalstablevalue117.25206.8 B$88.48132.52%4conservativewatch75%0.73%10/17/2021 16:11
59ABBV59ABBVIE INC. (XNYS:ABBV)Big pharma company that makes very popular drugs such as immunology drugs including: humira, rinvoq, skyrizi and including mental drugs like lexapro, amongst a plethora of other drugs. Health CarePharmaceuticals, Biotechnology & Life SciencesPharmaceuticalsPharmaceuticalsbig pharmabuffettorphanvalue146.76259.6 B$100.53145.99%4parwatch69%0.12%10/21/2021 15:26
70RTX70RAYTHEON TECHNOLOGIES CORPORATION (XNYS:RTX)Holds several valuable contracts with the US government for the production of defense systems, mainly revolving around aerospace. Has revenue segments that breakdown as: Collins Aerospace Systems, Pratt & Whitney, Intelligence & Space, and finally, Missiles & Defense.IndustrialsCapital GoodsAerospace & DefenseAerospace & Defense92.24137.7 B$54.07170.59%4par80%0.11%10/22/2021 9:55
71ZYME71ZYMEWORKS INC. (XNYS:ZYME)Develops treatments for several types of cancers through protein cell therapy. Health CarePharmaceuticals, Biotechnology & Life SciencesBiotechnologyBiotechnologycancer6.9300.4 B$16.3842.31%2aggressive77%0.83%10/22/2021 10:20
77IT77GARTNER, INC. (XNYS:IT)Research and Analytics firm that delivers data to customers in large quantities for enterprise use so that enterprises can better understand their business and potential growth opprotunities. Information TechnologySoftware & ServicesIT ServicesIT Consulting & Other Serviceshighfuture techgrowth273.5722.5 B$140.16195.18%3conservative94%2.97%10/22/2021 11:51
79ALB79ALBEMARLE CORPORATION (XNYS:ALB)Manufatures chemicles like lithium and bromine, is also the largest producer of lithium used in EV batteries.MaterialsMaterialsChemicalsSpecialty Chemicalslithiumevgrowth182.0021.3 B$224.3881.11%4aggressivewatch85%0.33%10/22/2021 11:59
80KEYS80KEYSIGHT TECHNOLOGIES, INC. (XNYS:KEYS)Acting as a key innovator, Keysight's goal is to help enterprises develop and test new technologies that can help them innovate for the future all across several sectors, such as aerospace, defense, electronics, ect. They also own Eggplant which offers AI solutions for testing purposes. Information TechnologyTechnology Hardware & EquipmentElectronic Equipment, Instruments & ComponentsElectronic Equipment & Instrumentsmargins, roicfuture techai153.2828.0 B$163.3793.82%4par91%0.47%10/22/2021 12:08
82NVO82Novo Nordisk A/S (XNYS:NVO)Danish biotech company that produces treatments for several diseases. Has 14% revenue generated from diabete treatments. Health CarePharmaceuticals, Biotechnology & Life SciencesPharmaceuticalsPharmaceuticalssimonsmarginsdiabetes97.68176.3 B$79.45122.95%2aggressive8%0.01%10/22/2021 20:00
95FRPT95FRESHPET, INC. (XNAS:FRPT)Creates alternative food products for domesticated animals. Sells food in retail stores by way of public refrigeration units. Consumer StaplesFood ProductsPackaged Foods & Meatspetshigh83.3503.6 B$12.01694.00%3conservative98%3.51%10/23/2021 17:59
97MEDP97MEDPACE HOLDINGS, INC. (XNAS:MEDP)Organizes clinical contract research providing phase I-IV clinical development services to biotechs.Health CareLife Sciences Tools & ServicesLife Sciences Tools & Serviceshighlabs140.2004.9 B$114.89122.03%4conservative79%20.31%10/23/2021 18:06
99PENN99PENN NATIONAL GAMING, INC. (XNAS:PENN)Operates small to medium size casinos around the United States with most of its vesting interest being targeted in the Northeast of the United States. Future Tech interests lie in onling gambling, which last quarter comprimised 8% of their revenue. Consumer DiscretionaryHotels, Restaurants & LeisureCasinos & Gamingdruckenmillershort intfuture tech47.2408.0 B$36.33130.03%3conservative86%1.69%10/23/2021 18:11
106SABR106SABRE CORPORATION (XNAS:SABR)Creates technology to assist travel firms in developing their businesses' programs by way of AI developments. Information TechnologyIT ServicesData Processing & Outsourced Servicesairobbinsshort int.future techtravelgrowth11.1103.6 B$9.13121.69%1aggressive113%1.05%10/23/2021 21:20
database
Cell Formulas
RangeFormula
S19,S59,S70:S71,S77,S79:S80,S82,S95,S97,S99,S106S19=P19/R19
Q19,Q59,Q70:Q71,Q77,Q79:Q80,Q82,Q95,Q97,Q99,Q106Q19=C19.[Market cap]
P19,P59,P70:P71,P77,P79:P80,P82,P95,P97,P99,P106P19=C19.Price
Cells with Data Validation
CellAllowCriteria
A2:A106Custom=COUNTIF($A$2:$A$1093,A2)=1
 
Upvote 0
I assume that you are just looking for an example?
This would copy the filtered values in column C of the active sheet to column J, starting at row iRow, of sheet 'screen'

Rich (BB code):
Intersect(ActiveSheet.UsedRange.Offset(1, 0), Columns("C")).SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("screen").Cells(iRow, "J")

This does the trick Peter! Thank you!!
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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