Тёмный

Excel VBA Macro: Pull Specific Columns from Another Workbook (Based on Header Values) Dynamically 

greggowaffles
Подписаться 6 тыс.
Просмотров 38 тыс.
50% 1

Excel VBA Macro: Pull Specific Columns from Another Workbook (Based on Header Values) Dynamically. In this video, we create a macro that automatically populates the current workbook with data from specific columns in another workbook from a specified location. We choose the columns we wish to pull by typing the corresponding headers in our workbook, and using a Do While Loop nested in a For Loop to match off the headers and copy and paste the desired columns.
Code (RU-vid doesn't allow brackets; so LT and GT are used for less than and greater than, respectively):
Sub pull_columns()
Dim head_count As Integer
Dim row_count As Integer
Dim col_count As Integer
Dim i As Integer
Dim j As Integer
Dim ws As Worksheet
Application.ScreenUpdating = False
Set ws = ThisWorkbook.Sheets("Sheet1")
'count headers in this workbook
head_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
'open other workbook and count rows and columns
Workbooks.Open Filename:="C:\Users\greggowaffles\Documents\RU-vid Videos\Test\Some Files\Cities.xlsx"
ActiveWorkbook.Sheets(1).Activate
row_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlDown)))
col_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
For i = 1 To head_count
j = 1
Do While j LT = col_count
If ws.Cells(1, i) = ActiveSheet.Cells(1, j).Text Then
ActiveSheet.Range(Cells(1, j), Cells(row_count, j)).Copy
ws.Cells(1, i).PasteSpecial xlPasteValues
Application.CutCopyMode = False
j = col_count
End If
j = j + 1
Loop
Next i
ActiveWorkbook.Close savechanges:=False
ws.Cells(1, 1).Select
Application.ScreenUpdating = True
End Sub
#ExcelVBA #ExcelMacro

Опубликовано:

 

1 янв 2022

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 85   
@amalhafiane7937
@amalhafiane7937 2 года назад
Thank you so much!! You literally saved my internship
@greggowaffles
@greggowaffles 2 года назад
No problem!! 👊😎
@siddharthmudaliar8720
@siddharthmudaliar8720 Год назад
I really cannot thank you much for a brilliant tutorial there. I was trying to work on a piece and was just not able to wrap my head around it. This video solved everything and the code looks so decent and clean. Fantastic job there mate. *Subscribed the channel 🎉🎉 Keep up the good work
@greggowaffles
@greggowaffles Год назад
Thank you so much for the feedback! I’ll keep doing my best!!
@judithvalentine8931
@judithvalentine8931 2 года назад
Hi Greg, Thank you very much for your videos, it's really helping me.
@greggowaffles
@greggowaffles 2 года назад
So glad to hear that! I’ll start making more soon!! Thanks for the motivation
@tknaved
@tknaved Год назад
very much thankful to u for this video...
@user-lp3ve9ct4w
@user-lp3ve9ct4w 4 месяца назад
This code is really great and simple Greg thanks
@greggowaffles
@greggowaffles 2 месяца назад
Glad you like it!
@user-fu1mf2xv8q
@user-fu1mf2xv8q 11 месяцев назад
Hi Greg, this is fantastic, thank you very much this is a great help and very well explained
@greggowaffles
@greggowaffles 11 месяцев назад
Glad it helped and appreciate the feedback!!
@user-nm4ul6gr6o
@user-nm4ul6gr6o 8 месяцев назад
Thank you so much Sir
@tknaved
@tknaved Год назад
excellent demonstration...
@greggowaffles
@greggowaffles Год назад
Thanks!
@alexrosen8762
@alexrosen8762 Год назад
A very useful tutorial explained in an excellent way
@greggowaffles
@greggowaffles Год назад
Thank you!!
@nani21n58
@nani21n58 Год назад
greatwork buddy
@greggowaffles
@greggowaffles Год назад
Thanks!
@Sk11257
@Sk11257 Год назад
its wonderfull, short vba code for excellent report generation
@greggowaffles
@greggowaffles Год назад
Thank you!
@snehalpanchal4565
@snehalpanchal4565 2 года назад
Awesome!!!! Point Blank... Bulls Eye... :)
@greggowaffles
@greggowaffles 2 года назад
🎯🤏😎
@mowgli536
@mowgli536 2 года назад
Hi Greg. This is amazing! Thank you for sharing. I'm trying to do a reorder of 70+ columns within a table. And then potentially delete some columns that I do not need. Background: I have 1 master report that is used for multiple teams and some teams want it cut down to a smaller amount of columns. So what I'm hoping to do is create a bunch of macros (buttons) to allow the master report that has been pasted to "worksheet2'" to modify based on the button choice. How can I create a cleaner version of reordering columns within a table? ' Daily_Shortlist Macro ' ' Range("Customize_Shortlist[[#Headers],[Recruiter Name]]").Select Selection.Cut Range("Customize_Shortlist[[#All],[Super Sector]]").Select Selection.Insert Shift:=xlToRight Range("Customize_Shortlist[[#Headers],[Hiring Manager Name]]").Select Selection.Cut Range("Customize_Shortlist[[#All],[Super Sector]]").Select Selection.Insert Shift:=xlToRight Range("Customize_Shortlist[[#Headers],[Posting Status]]").Select Selection.Cut Range("Customize_Shortlist[[#All],[Super Sector]]").Select Selection.Insert Shift:=xlToRight Range("Customize_Shortlist[[#Headers],[Status]]").Select Selection.Cut Range("Customize_Shortlist[[#All],[Super Sector]]").Select Selection.Insert Shift:=xlToRight Range("Customize_Shortlist[[#Headers],[Senior Leader]]").Select Selection.ListObject.ListColumns(15).Delete Selection.ListObject.ListColumns(15).Delete Selection.ListObject.ListColumns(15).Delete Selection.ListObject.ListColumns(15).Delete Range("Customize_Shortlist[[#Headers],[Job Title(2)]]").Select Selection.Cut Range("Customize_Shortlist[[#All],[Super Sector]]").Select Selection.Insert Shift:=xlToRight End Sub
@andrewliu309
@andrewliu309 Год назад
Hi Greg. This is very helpful. I'm creating this macro for other users to use so I've inserted a 2nd tab with comments and a macro button. Once I added a new tab, the macro does not work and the debug is highlighting the 'ws.Cells(1, 1).Select' line which I'm assuming could be because this works if there was only one tab in the workbook. How could I rewrite this if there was more than one tab?
@connorloze5418
@connorloze5418 7 месяцев назад
Hey Greg - this has been super helpful. One question I have is do you know a way to set criteria where the data I am pulling into the new workbook matches a specific date?
@blakehorner2949
@blakehorner2949 2 года назад
Lots of good videos I've already seen and they've really helped me! So thank you! I'm currently investigating the ability of one excel document to pull information from another document. Two questions I have so far in this: 1. Is it possible to make the file path adaptable? Where the document name can be changed but the column locations and everything are in the same location in every version of the document? 2. Does all of this work whether you are in a normal file explorer or Microsoft Teams? I apologize, I've been working in Excel VBA for about 6 months now, but I have so much more to go, so I imagine this will not be the last time you will see me in your comments haha. I have managed to do a number of things with what I've learned, including changing a duplicate detection and deletion code to what I needed it to do, but I can't say for sure if it is the most efficient method possible. Thanks!
@greggowaffles
@greggowaffles 2 года назад
thanks for watching!! 1. yeah, definitely. you can use wildcards and all that 2. not exactly sure. i'll have to look into that apologies for the late reply btw! 😅
@ashercrush8953
@ashercrush8953 Год назад
hi Greg tks so much for the sharing ! btw how can i workout if i want to copy multiple sheets with selected column to a same destination sheet?
@AmitKumar-ng1od
@AmitKumar-ng1od 2 года назад
Awesome Video,. Would be great help if we will be able to do the same thing from multiple workbooks.
@umamakeshvasanth5786
@umamakeshvasanth5786 Год назад
Thank you man. Is there a way to pull columns from multiple sheets than just 1
@CriticAL_EX
@CriticAL_EX Год назад
works great ,but one problem , i have columns that have formulas and dates , and double or single variables , what should i change in this code so it does copy everything ?
@timtom2150
@timtom2150 10 месяцев назад
The most helpful vba tutorial I've ever watched! Thank you so much! I had to modify a few lines since activating the source sheet didn't work for me, the row_count and col_count gave me values based on the worksheet I was importing the data to. So all the places that assumed using the active sheet, i added "ws_source." in front of like this: csv_path = "C:\Users\my_username\Documents\vba\source_data.csv" Set wb_source = Workbooks.Open(Filename:=csv_path) Set ws_source = wb_source.ActiveSheet Debug.Print "csv_path =" & csv_path row_count = WorksheetFunction.CountA(ws_source.Range("A1", ws_source.Range("A1").End(xlDown))) col_count = WorksheetFunction.CountA(ws_source.Range("A1", ws_source.Range("A1").End(xlToRight))) Debug.Print "row_count = " & row_count Debug.Print "col_count = " & col_count
@greggowaffles
@greggowaffles 10 месяцев назад
Niiiice!! Glad it was helpful and that you got it working on your end!!
@timtom2150
@timtom2150 10 месяцев назад
@@greggowaffles Working really well thanks! I was running into an overflow issue however from the rows and columns being declared as Int rather than Long. Now all is good again
@namith89
@namith89 Год назад
Hey Greg.. can you help with code for multiple worksheet.
@faizanmokashi5884
@faizanmokashi5884 2 года назад
Hi, this one really helped but got some questions. Does this pull the data which is only text format? I tried the same codes but I m unable to pull the data from read only file
@pankajpathania5698
@pankajpathania5698 9 месяцев назад
Thanks for this. How to add to the first available row in the same code, if you can please reply. Thanks
@Foskbou
@Foskbou 9 месяцев назад
Hello Greg - this is the closest I’ve come to solving a problem! Thanks! I am looking to run this through a dynamic workbook I have been creating. What would advise on troubleshooting for no returns ? I am working with a large data set A1:AS1220 and various data types, copying this code did not work. Also need tips on how to copy filtered data.
@nagashreepradeepa
@nagashreepradeepa Год назад
Hello, thanks for sharing this it is amazing..... I have tried this it work very well.... I have one question on this can we extract data from multiple files at one shot ...
@justinsingh6498
@justinsingh6498 11 месяцев назад
Hello Greg, I cannot thank you enough for this incredible tutorial ! You crushed it. The question I have after watching, and following along, is what needs to happen if you header row is not starting at A1 in the data workbook? "Thisworkbook" header is located at A1 but "Activeworkbook" is starting on row A5.
@aparnasundaresan7441
@aparnasundaresan7441 3 месяца назад
Hi Greg, will be helpful if you could give a solution for this.
@Jojosmith342
@Jojosmith342 Год назад
Hi Greg. I love all your tutorials as they are very practical and great code. There are some that you do not provide code and the screen hard to see even though I used to screenshot & magnify it I could not see either the colon, comma, colon lower L or 1. Very confusing when copying down your code. Videos with your code provided & clear screen help us so much and always received the most liked and I hope more will subscribe too. Your valuable time to make our lives easier, happier is always greatly appreciated. I always liked from the beginning class even though sometimes the program could not run smoothly because of the visibility. It took me sometimes to figure out ex: rg1 (it is number 1 but I write as L lower case) or criteria1 (number 1 but I wrote as letter L lower case) or the equal sign - I wrote as hyphen -. It would be nice if you could spell it out these little things or zoom the screen closer so that we can see better. Greatful for your hard work. thank you very much Greg!
@greggowaffles
@greggowaffles Год назад
Thanks! I’ll do better with that. And I’ll get the code added for all of my older videos
@Jojosmith342
@Jojosmith342 Год назад
@@greggowaffles You're genius coder and with strong passion to help people I believe you're successful. The number of subscribers will be increasing once you attach codes along with tutorials. 🙏
@greggowaffles
@greggowaffles Год назад
I really appreciate that!!
@jorge-3768
@jorge-3768 Год назад
Hi. Firstable thnx for the video its really hopefull. Then onlyn trouble ive got its that the columns i wanna import and match with the text of the header starts in A:3 i tried different stuff without sucess. Any idea please?
@koushiknarendran2584
@koushiknarendran2584 Год назад
Hello, thanks for the video. I tried fetching data for 15 columns out of 120 columns but my destination sheet is blank. I don’t get any error message. Please help me out.
@MF-cf8ez
@MF-cf8ez Год назад
Great tutorial! I m getting a Run-time 1004 error and I am not sure where I went wrong. Can you assist?
@naomifagan2885
@naomifagan2885 2 года назад
How do you pull columns in across multiple workbooks ??
@RahulPatil-jr6uu
@RahulPatil-jr6uu Месяц назад
How to possible?
@SantoshSoni-ly2sq
@SantoshSoni-ly2sq 2 года назад
Amazing, Very Good Stuff... Can you also add a column and put the sheet name there. How to get data into one sheet if other workbook has multiple sheet (By country) with same header and column ?
@jellybeanjellio8294
@jellybeanjellio8294 2 года назад
Hi, I just found your channel. Y ou are amazing. You have helped me a lot so Thank You! I have a question. How do I write a macro code that refers to a list of delimiters in a lookup table in excel to remove any delimiter used in a column copied from a CSV file?
@greggowaffles
@greggowaffles 2 года назад
Thank you! Glad to hear that! When you say “remove”, do you want to delete that cell, the contents of the cell, or the column itself?
@sharadpatel07
@sharadpatel07 Месяц назад
Hi Greg its help me a lot but i have to paste data in other sheet & create multiple forms from master sheet, how can i do it ?
@zingfam7616
@zingfam7616 Год назад
Hi Greg, this is great, but when i tried, why did it only copy and paste to Coloum 1 only. where I have 6 headers and all matched. Any idea why ?
@RohitRajput01
@RohitRajput01 2 года назад
Hi Thanks for the such h valuable video but i have try this but it's only pulled 15 row data not to the bottom why i don't know please help me out
@lenecaro8865
@lenecaro8865 Год назад
Great video! Using your example, how could I make it so it only pulled specific information? For example only want the data to pull if population is >= 150000
@yeiayel5813
@yeiayel5813 2 года назад
mine can’t paste the values, it only closes the destination workbook 😅
@keithrussellroldan7155
@keithrussellroldan7155 2 года назад
This is a very nice share to us. I would like to add what if the header name is different from another worksheet/workbook (e.g. Population and Population (in K) )
@greggowaffles
@greggowaffles 2 года назад
You could use an if statement that uses the string “Population (in K)” instead of the cell
@keithrussellroldan7155
@keithrussellroldan7155 2 года назад
@@greggowaffles Thanks for the reply. Will try to sort like that.
@greggowaffles
@greggowaffles 2 года назад
No prob!
@rautoramarautorama9324
@rautoramarautorama9324 Год назад
How do we do the same, using arrays?
@ritwiknautiyal2022
@ritwiknautiyal2022 Год назад
Hello Greg can you please modify the code as i want to paste the specific columns into a workbook but this workbook already has some data so i want to paste it below that. Mainly to the last available row of ws workbook. Can you please help me with that.
@Babs.K
@Babs.K Год назад
Did you ever figure this out?
@totopanganiban7537
@totopanganiban7537 9 месяцев назад
how about adding another data from another sheet? thanks if you'll notice
@nextgenerationforchrist320
@nextgenerationforchrist320 Год назад
hi all, I was using this vba code in a project but as from yesterday it stops to copy in the destination excel. Can anyone help
@rodneyjones-dd8dr
@rodneyjones-dd8dr 10 месяцев назад
I'm not a coder. I need help with my spreadsheet. I need to move data from a specific column to another sheet using a button. It's data from a pricing sheet to an invoice.
@CubicleCouples
@CubicleCouples 5 месяцев назад
Hi Greg code working but data no pasted
@adamsreef
@adamsreef 2 года назад
Hello greg. I need help with macro on how to auto filter, copy and save to new multiple workbooks a raw sheet and a summary sheet. So basically i have the first sheet as raw data and the second worksheet as with pivot table summary. Hope you can see this.
@greggowaffles
@greggowaffles 2 года назад
i hope this video helps!! ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-unUgUlEBvu4.html i'll be making videos on using excel vba with pivot tables soon
@IT.Solution
@IT.Solution Год назад
Today after asking chatgpt this question again and again couldnt find the solution. Thats what i wanted.. Will test tomorrow
@greggowaffles
@greggowaffles Год назад
Happy to help!
@IT.Solution
@IT.Solution Год назад
@@greggowaffles great it worked for me.. PS for all please note no header should be blank otherwise data wont be pulled, recommendation to author, to use in a title pull data from excel based on header values
@greggowaffles
@greggowaffles Год назад
Thanks! I will make that update in the title
@irenesanjay
@irenesanjay Год назад
do u have the data file for this.
@franklin07180
@franklin07180 6 месяцев назад
Hi, bro.... i'm really appreciated for this job. i tried to do it and followed all your path but it still not run yet. it shows run time erro '438' Object doesn't support this property or method. please help me out to fix this error. i use excel 2010. thanks in advance.
@dhavalahir6028
@dhavalahir6028 2 года назад
if possible kindly update greg
@hekittyta9088
@hekittyta9088 Год назад
Hi greg, this is great, what if I'm pulling the data from the same workbook but in a different sheet
@wzclips4838
@wzclips4838 Год назад
Me too, same taks
@greggowaffles
@greggowaffles Год назад
hope this helps! ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-FxJpUajNOVs.html
@dhavalahir6028
@dhavalahir6028 2 года назад
application defined or object defined error on below lines ActiveSheet.Range(Cells(1, j), Cells(row_count, j)).Copy ws.Cells(1, i).PasteSpecial xlPasteValues
@charmisawla723
@charmisawla723 Год назад
Hi I am having the same error , did you get the way out?
@rajsatarkar3605
@rajsatarkar3605 Год назад
Data header starts from 5th row what to do ???? Please help
@greggowaffles
@greggowaffles Год назад
row_count = WorksheetFunction.CountA(Range("A5", Range("A5").End(xlDown))) + 4
Далее
Лайфхак с колой не рабочий
00:16
Просмотров 215 тыс.
Кто Первый Получит Миллион ?
27:44
Excel VBA: Copy Data Between ANY Two Sheets
12:56
Просмотров 117 тыс.
VBA Copy Specific Column From Another Workbook
12:41
Просмотров 11 тыс.
VBA Macro to Copy Data to Another Workbook
10:30
Просмотров 68 тыс.
Лайфхак с колой не рабочий
00:16
Просмотров 215 тыс.