Creating a second table based off values on the last column of the original table

vbaturk

New Member
Joined
May 19, 2018
Messages
3
I'm trying to create a second table from an original table based off of non zero values indicated in the third column of the original table. I need to do this using vba / macro, here is a visual of what I need to do.

the range of the table is cells G10 to I47, this is just a snippet. I was thinking of looping through the range and if last column value isn't zero copy to new range. But not sure how to do it... Thank you!


CompIDWt%CompIDwt%
x1aa0x2bb2
x2bb2x5ee3
x3cc0x6ff4
x4dd0
x5ee3
x6ff4

<tbody>
</tbody>
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Here is one option

Code:
Option Explicit


Sub NewTbl()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    s1.Range("G1:I1").Copy s2.Range("A1")
    Dim i As Long
    Dim lr As Long, last As Long
    lr = s1.Range("G" & Rows.Count).End(xlUp).Row
    For i = 2 To lr
        last = s2.Range("A" & Rows.Count).End(xlUp).Row
        If s1.Range("I" & i) <> 0 Then
            s1.Range("G" & i & ":I" & i).Copy s2.Range("A" & last + 1)
        End If
    Next i
End Sub

Alternatively, you could filter on Column I and then copy and paste the results.

Code:
Option Explicit


Sub Macro1()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim lr As Long
    lr = s1.Range("G" & Rows.Count).End(xlUp).Row
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    s1.Range("G1:I1").AutoFilter
    s1.Range("G1:I" & lr).AutoFilter Field:=3, Criteria1:=Array("2", _
        "3", "4"), Operator:=xlFilterValues
    lr = s1.Range("G" & Rows.Count).End(xlUp).Row
    s1.Range("G1:I" & lr).Copy s2.Range("A1")
    Sheets("Sheet2").Select
End Sub
 
Last edited:
Upvote 0
so I'm adding another level of complexity
I want to create multiple tables next to the new table, but still by just hitting the macro button. Essentially I would update the values in the original table again, hit my macro button and a second new table would be populated next to the first new table. I want to be able to do this multiple times.

Would I essentially just create a counter and offset + 4 on every column variable within the code? since I know the width of every table is 3?
Not sure about the best way of going about doing this.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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