Excel VBA Macro: Pull Specific Columns from One Sheet to Another (Dynamically)

  Рет қаралды 27,508

greggowaffles

greggowaffles

Жыл бұрын

Excel VBA Macro: Pull Specific Columns from One Sheet to Another (Dynamically). In this video, we create a macro that automatically populates a worksheet with data from specific columns on another sheet from the same workbook. 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 (KZfaq doesn't allow brackets; so LT and GT are used for less than and greater than, respectively):
Sub pull_columns_over()
Dim head_count As Integer
Dim row_count As Integer
Dim col_count As Integer
Dim i As Integer
Dim j As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Raw Data")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
ws2.Activate
head_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
ws1.Activate
col_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
row_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlDown)))
For i = 1 To head_count
j = 1
Do While j LT= col_count
If ws2.Cells(1, i) = ws1.Cells(1, j).Text Then
ws1.Range(Cells(1, j), Cells(row_count, j)).Copy
ws2.Cells(1, i).PasteSpecial xlPasteValues
Application.CutCopyMode = False
j = col_count
End If
j = j + 1
Loop
Next i
With ws2
.Activate
.Cells(1, 1).Select
End With
End Sub
#ExcelVBA #ExcelMacro

Пікірлер: 47
@Jojosmith342
@Jojosmith342 Жыл бұрын
Perfect. I follo'w your instruction carefully. Trying to type in from screen first & use your written code as refference. You are GENIUS. Thank you so much Greg for all your hard work, time to educate us. Your tutorials are always valuable & absolutely thumbs up both hands 👌👏
@jasonjohnston5256
@jasonjohnston5256 5 ай бұрын
You completely blew my mind! This is outstanding. Thank you so much!!
@josephraji296
@josephraji296 Жыл бұрын
Very well explained and it is working very much fine for me after researching so much i found nowhere except yours so i subscribed to your channel.
@greggowaffles
@greggowaffles Жыл бұрын
I really appreciate that! Thank you for the feedback
@patriciapetri4792
@patriciapetri4792 8 ай бұрын
I love it!! thank you for teaching us. you made it so simple and easy to understand and follow... great job!!
@drewgossage8842
@drewgossage8842 Жыл бұрын
Really cool, straight to the point, great tutorial!
@greggowaffles
@greggowaffles Жыл бұрын
Appreciate the feedback 😊
@richardmoore7955
@richardmoore7955 Жыл бұрын
@@greggowaffles Dim i As Integer Dim j As Integer Why are these letters used as the integer
@airman-xx9vg
@airman-xx9vg Жыл бұрын
finally, got it!!! thanks. code works great 👍👍👍👍
@greggowaffles
@greggowaffles Жыл бұрын
Glad to hear that!! Thanks for watching!! 💪💪
@arditiljazi6959
@arditiljazi6959 Жыл бұрын
thank you for showing this sample. is there a way of tweaking it to show it can copy and paste the specific columns to another worksheet?
@niakubheka9099
@niakubheka9099 Жыл бұрын
Hi Gregg. Thank you for the vid, it was very helpful. I used the code for my sheets and it worked very well. Can you please assist me with how I can put additional data from a different sheet underneath the already transferred data (on the next empty line) of the worksheet. I need to consolidate from different sheets. Your assistance will be greatly appreciated.
@Donkeys_Dad_Adam
@Donkeys_Dad_Adam 5 ай бұрын
Nicely done. Don't forget to set your objects to nothing before you exit the sub.
@andytawandamahdania7943
@andytawandamahdania7943 Жыл бұрын
It's a great tutorial 👍 But can you tell us how to pull specific column with some applied filter? Thx
@johnnastasi9143
@johnnastasi9143 Жыл бұрын
Excellent, Can this macro be modified to always pull x number of columns and place them in a sheet sequentially Sheet-001, sheet-002, etc?
@gospelmoto2833
@gospelmoto2833 9 ай бұрын
superb! got a new sub here. Thanks!
@greggowaffles
@greggowaffles 9 ай бұрын
Thank you so much!!
@MohAboAbdo
@MohAboAbdo Жыл бұрын
Thank you so much
@greggowaffles
@greggowaffles Жыл бұрын
You are very welcome!
@cathy___m
@cathy___m Жыл бұрын
This is insightful. Thank you. But can you please do it with more than 2 sheets? I am struggling with consolidating my 2nd sheet into the master sheet. I managed to do it for the 1st sheet using the method. I know we have to use LastRow to copy into the lastrow of the master sheet but I still can’t seem to get it right.
@totopanganiban7537
@totopanganiban7537 10 ай бұрын
same here :)
@Jojosmith342
@Jojosmith342 Жыл бұрын
Hi Greg, I have a large spreadsheet with >50,000 records. Problem I am facing is there is a heading row (without data) between these records. To consolidate the db I need to remove these heading rows (contain no data, simply heading) on spreadsheets. It is very challenging as it is a huge database. Could you please kindly help to make a tutorial how to resolve this issue? Thanks a lot Greg
@abrahamruiz7034
@abrahamruiz7034 Жыл бұрын
Do you happen to have a way to pull columns into different sheets at once?
@ladyteamer9331
@ladyteamer9331 Жыл бұрын
can this be modified to pull column names that are not an exact match?
@ahmetokur8224
@ahmetokur8224 Жыл бұрын
Teşekkür ediyorum.
@v12onK
@v12onK Жыл бұрын
What if I want to pull a specific date or date range?
@vinaypharande2637
@vinaypharande2637 Жыл бұрын
Hi, while running this VBA Macros. I'm receiving fix Run-time error '6' Overflow. How should I fix it. Please guide.
@shizmeeamir5307
@shizmeeamir5307 Жыл бұрын
Row Count say shows an error " Runtime Error 6 / Overflow " .. What does this mean ??
@user-tc5hh3ct7p
@user-tc5hh3ct7p 8 ай бұрын
Hi there, how do you deal with copying over a column that contains dates? Thanks
@airman-xx9vg
@airman-xx9vg Жыл бұрын
don't know what's wrong but I always end up on copying and pasting just the first two columns from raw data on sheet 2 after running the codes, what could be the problem?
@zingfam7616
@zingfam7616 Жыл бұрын
Hi, I followed your code but somehow the code is only copy and paste one column. Any idea why ? I want to copy only 8 columns.
@davidlinton4127
@davidlinton4127 4 ай бұрын
Could you use an advanced filter instead?
@Andre-ec4xw
@Andre-ec4xw 6 ай бұрын
Hi, I was using this code and it worked perfectly, but now I get an error code (400) can you help please
@airman-xx9vg
@airman-xx9vg Жыл бұрын
it would be a big help if you'll share the excel file with that VBA code. 😅😅😅
@alializadeh8195
@alializadeh8195 Жыл бұрын
Thanx
@greggowaffles
@greggowaffles Жыл бұрын
No prob!
@avinashbiju5329
@avinashbiju5329 8 ай бұрын
What to do if the value to be copied is a number and text, how would that be assigned?
@h3rw3i
@h3rw3i 9 ай бұрын
Hi I got an error on this line " If ws2.Cells(1, i) = ws1.Cells(1, j).Text Then" - Run-time error '1004': Application-defined or object-defined error. How do i fix? Thanks in advance.
@Sharath_hp
@Sharath_hp 7 ай бұрын
One specific column values are not getting copied. What do do?
@serginhov2
@serginhov2 Жыл бұрын
Hello, I'm getting error to use this macro, could you please help. Application-defined or object-defined error . I'm trying to put from sheet to another. Not workbook
@greggowaffles
@greggowaffles Жыл бұрын
What’s your code look like?
@sarahanyayahan240
@sarahanyayahan240 5 ай бұрын
This doesnt work for me. I dont know the prob
@richardmoore7955
@richardmoore7955 Жыл бұрын
Dim i As Integer Dim j As Integer Why are these letters used as the integer
@greggowaffles
@greggowaffles Жыл бұрын
Just a preference. You can use whatever letter/word you want
@me1234540
@me1234540 Жыл бұрын
Man you can’t even imagine how I hated this video… sorry but who in the hell that doesn’t have any clue about formulas is going to be able to write all the formulas you wrote? I thought the point of the video was just to apply a direct formula to pull the columns from one WS to another. Not helpful at all! Tutorials are meant to be for people who DO NOT know anything about it.
@greggowaffles
@greggowaffles Жыл бұрын
Thank you for watching the video and sharing your feedback. I aim to be as descriptive as possible with the video titles so that people know exactly what to expect in the videos. I have beginner tutorials on my channel if you want to check them out, and if there’s a specific example you have I can try to help you out.
WHAT’S THAT?
00:27
Natan por Aí
Рет қаралды 12 МЛН
One moment can change your life ✨🔄
00:32
A4
Рет қаралды 31 МЛН
Cat Corn?! 🙀 #cat #cute #catlover
00:54
Stocat
Рет қаралды 15 МЛН
ПРОВЕРИЛ АРБУЗЫ #shorts
00:34
Паша Осадчий
Рет қаралды 6 МЛН
VBA Macro to Copy Data from Another Workbook in Excel
13:39
Excel Campus - Jon
Рет қаралды 633 М.
Python in Excel vs. VBA - What You Should Learn in 2024!
10:05
David Langer
Рет қаралды 34 М.
Excel VBA To Copy Data From One Sheet To Another (BEGINNER TO PRO!)
11:46
Tiger Spreadsheet Solutions
Рет қаралды 96 М.
Automate Copying of Column Data from Sheet to Sheet Using Excel VBA
13:02
Dinesh Kumar Takyar
Рет қаралды 319 М.
Copying Data from User Selected Files Using GetOpenFilename
10:26
Excel Macro Mastery
Рет қаралды 18 М.
WHAT’S THAT?
00:27
Natan por Aí
Рет қаралды 12 МЛН