VBA and macros to copy values and insert into column (left to right)

L

Legacy 485190

Guest
Hi all,

I am having difficulties to use vba and macros, to copy values (rows of data) and paste as value into a column after it. I tried using Record macros but failed to get the result as the column that I need to select and copy from always changes.

This is the origin report :
Test Cycle 1
1Test 1Pass
2Test 2Pass
3Test 3Pass
4Test 4Pass
5Test 5Pass

Test Cycle 2
1Test 1Fail
2Test 2Pass
3Test 3Pass
4Test 4Pass
5Test 5Pass


Test Cycle 3
1Test 1Fail
2Test 2Pass
3Test 3Pass
4Test 4Pass
5Test 5Pass


I need the result like this :
Test Cycle 1Test Cycle 2Test Cycle 3Test Cycle 4
1Test 1PassFailPassPass
2Test 2PassPassFailPass
3Test 3PassPassPassPass
4Test 4PassPassPassPass
5Test 5PassPassPassPass

Can anyone help me ? I need to automate this process.

Thanks in advance.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Okay. I should have avoid putting conflicting name since row is also in VBA term. My bad. Try
VBA Code:
Sub Test()

Dim nRow As Long, nCol As Long
Dim rngRowFound As Range, rngResult As Range
Dim cell As Range, rngData As Range
Dim ws1 As Worksheet, ws2 As Worksheet

Set ws1 = ActiveWorkbook.Sheets(1)
Set ws2 = ActiveWorkbook.Sheets(2)

Set rngData = ws1.Range("B1", ws1.Cells(Rows.Count, "B").End(xlUp))

For Each cell In rngData
    Select Case cell
        Case ""
        If cell.Offset(0, 1) Like "Test Cycle*" Then
            nCol = CLng(Trim(Split(cell.Offset(0, 1), "Test Cycle")(1))) + 2
            ws2.Cells(1, nCol) = cell.Offset(0, 1)
        End If
    Case Else
        Set rngResult = ws2.Range("B2", ws2.Cells(Rows.Count, "B").End(xlUp))
        If rngResult.row = 1 Then Set rngResult = ws2.Range("B2")
        Set rngRowFound = rngResult.Find(cell.Value, LookAt:=xlWhole)
        If rngRowFound Is Nothing Then
            nRow = ws2.Cells(Rows.Count, "B").End(xlUp).Offset(1).row
            With ws2.Cells(nRow, nCol - 1)
                .Value = cell
                .Offset(0, 1) = cell.Offset(0, 1)
            End With
        Else
            With ws2.Cells(rngRowFound.row, nCol)
                .Value = cell.Offset(0, 1)
            End With
        End If
    End Select
Next

End Sub
 
Upvote 0
Okay. I should have avoid putting conflicting name since row is also in VBA term. My bad. Try
VBA Code:
Sub Test()

Dim nRow As Long, nCol As Long
Dim rngRowFound As Range, rngResult As Range
Dim cell As Range, rngData As Range
Dim ws1 As Worksheet, ws2 As Worksheet

Set ws1 = ActiveWorkbook.Sheets(1)
Set ws2 = ActiveWorkbook.Sheets(2)

Set rngData = ws1.Range("B1", ws1.Cells(Rows.Count, "B").End(xlUp))

For Each cell In rngData
    Select Case cell
        Case ""
        If cell.Offset(0, 1) Like "Test Cycle*" Then
            nCol = CLng(Trim(Split(cell.Offset(0, 1), "Test Cycle")(1))) + 2
            ws2.Cells(1, nCol) = cell.Offset(0, 1)
        End If
    Case Else
        Set rngResult = ws2.Range("B2", ws2.Cells(Rows.Count, "B").End(xlUp))
        If rngResult.row = 1 Then Set rngResult = ws2.Range("B2")
        Set rngRowFound = rngResult.Find(cell.Value, LookAt:=xlWhole)
        If rngRowFound Is Nothing Then
            nRow = ws2.Cells(Rows.Count, "B").End(xlUp).Offset(1).row
            With ws2.Cells(nRow, nCol - 1)
                .Value = cell
                .Offset(0, 1) = cell.Offset(0, 1)
            End With
        Else
            With ws2.Cells(rngRowFound.row, nCol)
                .Value = cell.Offset(0, 1)
            End With
        End If
    End Select
Next

End Sub
Still have the same error
1635751966557.png
 
Upvote 0
When you stuck at that row, move your cursor to hover on nRow and nCol. See if any value popped up. However, it seem like ws2 has not been defined which cannot be right. Something weird here.
 
Upvote 0
When you stuck at that row, move your cursor to hover on nRow and nCol. See if any value popped up. However, it seem like ws2 has not been defined which cannot be right. Something weird here.
The nRow is 2 and the nCol is 0
 
Upvote 0
Your sheet 1 is like this?
Bookcol.xlsm
ABC
1Test Cycle 1
21Test 1Pass
32Test 2Pass
43Test 3Pass
54Test 4Pass
65Test 5Pass
7
8Test Cycle 2
91Test 1Fail
102Test 2Pass
113Test 3Pass
124Test 4Pass
135Test 5Pass
14
15
16Test Cycle 3
171Test 1Fail
182Test 2Pass
193Test 3Pass
204Test 4Pass
215Test 5Pass
Sheet1
 
Upvote 0
Your sheet 1 is like this?
Bookcol.xlsm
ABC
1Test Cycle 1
21Test 1Pass
32Test 2Pass
43Test 3Pass
54Test 4Pass
65Test 5Pass
7
8Test Cycle 2
91Test 1Fail
102Test 2Pass
113Test 3Pass
124Test 4Pass
135Test 5Pass
14
15
16Test Cycle 3
171Test 1Fail
182Test 2Pass
193Test 3Pass
204Test 4Pass
215Test 5Pass
Sheet1
This is my sheet 1
1635753448930.png
 
Upvote 0
That is why nCol cannot be found because I was looking for Test Cycle 1 and it is located elsewhere. That is why it is important to have exact copy unless you just want to get the idea and can modify code as required. ;)
 
Upvote 0
That is why nCol cannot be found because I was looking for Test Cycle 1 and it is located elsewhere. That is why it is important to have exact copy unless you just want to get the idea and can modify code as required. ;)
Because this is my first time using VBA code and now I know where is the problem but still no have any idea to solve the errors. ?
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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