Build Array Macro for copying cells and pasting basis criteria

centaur87

New Member
Joined
May 9, 2016
Messages
13
Hi Excel Guru's..need a vba to resolve this.

Below table is my Onedrive source Data Set residing in Sheet1 (Table 1 )

IDSTNAMEAssetQtyUOMAsset2Qty2UOM2Asset3Qty3UOM3Asset4Qty4UOM4Asset5Qty5UOM5Req typeJustyEnclose
116-07-19xyztop5100rock880soft10110FormYes
216-07-19abcrock-h940soft670EmailNo

<tbody>
</tbody>


I want a vba to convert above data set into below format (Table 2) pasting in same book under sheet2, but only for new entries done in Table 1

Sub ID will be generated based on Asset column : if value exists in Asset2 or Asset3, a new row is generatd below under same ID. If Value is Null in Asset2 or Asset3 then the array moves to next ID item line.

IDSub IDSTNAMEAssetQtyUOMReq typeJustyEnclose
11.116-07-19xyztop5100FormYes
1.216-07-19xyzrock880FormYes
1.316-07-19xyzsoft10110FormYes
22.116-07-19abcrock-h940EmailNo
2.216-07-19abcsoft670EmailNo

<tbody>
</tbody>

Using VBA MACRO Button.

I appreciate your time and efforts.
Please write if you find any difficulty to understand.

Regards
Centaur
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try this

Change Sheet1 and Sheet2 by the names of your sheets.
I assume that the data starts in row 2 and the headers are in row 1.

Code:
Sub Build_Array()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim c As Range, j As Long, k As Long, col As Long, n As Long
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    sh2.Rows("2:" & Rows.Count).ClearContents
    k = 2
    col = sh1.Cells(1, Columns.Count).End(xlToLeft).Column - 3
    For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
        n = 1
        sh2.Cells(k, "A").Value = c.Value
        For j = 4 To col Step 3
            If sh1.Cells(c.Row, j).Value <> "" Then
                sh2.Cells(k, "B").Value = c.Value & "." & n
                sh2.Cells(k, "C").Value = c.Offset(, 1).Value
                sh2.Cells(k, "D").Value = c.Offset(, 2).Value
                sh2.Cells(k, "E").Resize(1, 3).Value = sh1.Cells(c.Row, j).Resize(1, 3).Value
                sh2.Cells(k, "H").Resize(1, 3).Value = sh1.Cells(c.Row, col + 1).Resize(1, 3).Value
                n = n + 1
                k = k + 1
            End If
        Next
    Next
    MsgBox "End"
End Sub
 
Upvote 0
Hey you are awesome...
Actually i just realised i need a change in Table 2. Could you please check once more..

IDSub IDSTnameAssetQtyUOMReqJustyEnclose
107-11-2019xyzFormYes
1.1Top5100
1.2Rock880
1.3soft-hard10110
207-11-2019abcCallYes
2.1Rock-hard940
2.2soft670

<tbody>
</tbody>

Sorry again.



Try this

Change Sheet1 and Sheet2 by the names of your sheets.
I assume that the data starts in row 2 and the headers are in row 1.

Code:
Sub Build_Array()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim c As Range, j As Long, k As Long, col As Long, n As Long
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    sh2.Rows("2:" & Rows.Count).ClearContents
    k = 2
    col = sh1.Cells(1, Columns.Count).End(xlToLeft).Column - 3
    For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
        n = 1
        sh2.Cells(k, "A").Value = c.Value
        For j = 4 To col Step 3
            If sh1.Cells(c.Row, j).Value <> "" Then
                sh2.Cells(k, "B").Value = c.Value & "." & n
                sh2.Cells(k, "C").Value = c.Offset(, 1).Value
                sh2.Cells(k, "D").Value = c.Offset(, 2).Value
                sh2.Cells(k, "E").Resize(1, 3).Value = sh1.Cells(c.Row, j).Resize(1, 3).Value
                sh2.Cells(k, "H").Resize(1, 3).Value = sh1.Cells(c.Row, col + 1).Resize(1, 3).Value
                n = n + 1
                k = k + 1
            End If
        Next
    Next
    MsgBox "End"
End Sub
 
Upvote 0
Hey you are awesome...
Actually i just realised i need a change in Table 2. Could you please check once more..
Sorry again.

You are leaving many spaces in your table, that will not help you in future processes, such as sorting, filtering or copying.
Ideally, it should be like this:
Code:
Sub [COLOR=#0000ff]Build_Array2[/COLOR]()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim c As Range, j As Long, k As Long, col As Long, n As Long
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    sh2.Rows("2:" & Rows.Count).ClearContents
    k = 2
    col = sh1.Cells(1, Columns.Count).End(xlToLeft).Column - 3
    For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
        n = 1
        For j = 4 To col Step 3
            If sh1.Cells(c.Row, j).Value <> "" Then
                sh2.Cells(k, "A").Value = c.Value
                sh2.Cells(k, "B").Value = c.Value & "." & n
                sh2.Cells(k, "C").Value = c.Offset(, 1).Value
                sh2.Cells(k, "D").Value = c.Offset(, 2).Value
                sh2.Cells(k, "E").Resize(1, 3).Value = sh1.Cells(c.Row, j).Resize(1, 3).Value
                sh2.Cells(k, "H").Resize(1, 3).Value = sh1.Cells(c.Row, col + 1).Resize(1, 3).Value
                n = n + 1
                k = k + 1
            End If
        Next
    Next
    MsgBox "End"
End Sub


--------------
My intention is to give advice on how you should have your information. But I give you what you need.

Try this:

Code:
Sub [COLOR=#0000ff]Build_Array3[/COLOR]()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim c As Range, j As Long, k As Long, col As Long, n As Long
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    sh2.Rows("2:" & Rows.Count).ClearContents
    k = 2
    col = sh1.Cells(1, Columns.Count).End(xlToLeft).Column - 3
    For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
        n = 1
        sh2.Cells(k, "A").Value = c.Value               'ID
        sh2.Cells(k, "C").Value = c.Offset(, 1).Value   'ST
        sh2.Cells(k, "D").Value = c.Offset(, 2).Value   'name
        sh2.Cells(k, "H").Resize(1, 3).Value = sh1.Cells(c.Row, col + 1).Resize(1, 3).Value
        k = k + 1
        For j = 4 To col Step 3
            If sh1.Cells(c.Row, j).Value <> "" Then
                sh2.Cells(k, "B").Value = c.Value & "." & n
                sh2.Cells(k, "E").Resize(1, 3).Value = sh1.Cells(c.Row, j).Resize(1, 3).Value
                n = n + 1
                k = k + 1
            End If
        Next
    Next
    MsgBox "End"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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