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

Поделиться
HTML-код
  • Опубликовано: 1 окт 2022
  • 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 (RUclips 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!!

  • @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 Год назад +1

    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

  • @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

  • @airman-xx9vg
    @airman-xx9vg Год назад

    finally, got it!!! thanks. code works great 👍👍👍👍

    • @greggowaffles
      @greggowaffles  Год назад

      Glad to hear that!! Thanks for watching!! 💪💪

  • @Donkeys_Dad_Adam
    @Donkeys_Dad_Adam 5 месяцев назад

    Nicely done. Don't forget to set your objects to nothing before you exit the sub.

  • @MohAboAbdo
    @MohAboAbdo Год назад

    Thank you so much

  • @gospelmoto2833
    @gospelmoto2833 9 месяцев назад

    superb! got a new sub here. Thanks!

  • @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.

  • @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?

  • @ahmetokur8224
    @ahmetokur8224 Год назад

    Teşekkür ediyorum.

  • @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?

  • @andytawandamahdania7943
    @andytawandamahdania7943 Год назад

    It's a great tutorial 👍
    But can you tell us how to pull specific column with some applied filter? Thx

  • @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

  • @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.

  • @ladyteamer9331
    @ladyteamer9331 Год назад

    can this be modified to pull column names that are not an exact match?

  • @abrahamruiz7034
    @abrahamruiz7034 Год назад

    Do you happen to have a way to pull columns into different sheets at once?

  • @alializadeh8195
    @alializadeh8195 Год назад

    Thanx

  • @vinaypharande2637
    @vinaypharande2637 Год назад

    Hi, while running this VBA Macros. I'm receiving fix Run-time error '6' Overflow. How should I fix it. Please guide.

  • @v12onK
    @v12onK Год назад

    What if I want to pull a specific date or date range?

  • @shizmeeamir5307
    @shizmeeamir5307 Год назад

    Row Count say shows an error " Runtime Error 6 / Overflow " .. What does this mean ??

  • @user-tc5hh3ct7p
    @user-tc5hh3ct7p 9 месяцев назад

    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?

  • @Sharath_hp
    @Sharath_hp 7 месяцев назад

    One specific column values are not getting copied. What do do?

  • @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.

  • @avinashbiju5329
    @avinashbiju5329 8 месяцев назад

    What to do if the value to be copied is a number and text, how would that be assigned?

  • @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

  • @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

  • @airman-xx9vg
    @airman-xx9vg Год назад +1

    it would be a big help if you'll share the excel file with that VBA code. 😅😅😅

  • @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 Год назад +1

    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  Год назад +1

      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.