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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
The test results are on separate sheets or all the same sheet?
 
Upvote 0
Try this. I put the result on Sheet2
VBA Code:
Sub Test()

Dim row As Long, col 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("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")

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
            col = CLng(Trim(Split(cell.Offset(0, 1), "Test Cycle")(1))) + 2
            ws2.Cells(1, col) = 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
            row = ws2.Cells(Rows.Count, "B").End(xlUp).Offset(1).row
            With ws2.Cells(row, col - 1)
                .Value = cell
                .Offset(0, 1) = cell.Offset(0, 1)
            End With
        Else
            With ws2.Cells(rngRowFound.row, col)
                .Value = cell.Offset(0, 1)
            End With
        End If
    End Select
Next

End Sub
 
Upvote 0
Try this. I put the result on Sheet2
VBA Code:
Sub Test()

Dim row As Long, col 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("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")

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
            col = CLng(Trim(Split(cell.Offset(0, 1), "Test Cycle")(1))) + 2
            ws2.Cells(1, col) = 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
            row = ws2.Cells(Rows.Count, "B").End(xlUp).Offset(1).row
            With ws2.Cells(row, col - 1)
                .Value = cell
                .Offset(0, 1) = cell.Offset(0, 1)
            End With
        Else
            With ws2.Cells(rngRowFound.row, col)
                .Value = cell.Offset(0, 1)
            End With
        End If
    End Select
Next

End Sub
It shows that the line "With ws2.Cells (row, col - 1)" has application-defined or object-defined error.
 
Upvote 0
It shows that the line "With ws2.Cells (row, col - 1)" has application-defined or object-defined error.
if your workbook don't have Sheet2, Add new sheet and name it Sheet2
Or Change Sheet2 at VBA Code to one of Empty sheets within Workbook.
 
Upvote 0
if your workbook don't have Sheet2, Add new sheet and name it Sheet2
Or Change Sheet2 at VBA Code to one of Empty sheets within Workbook.
I have the Sheet2 in my workbook but it still cant work.
 
Upvote 0
I have the Sheet2 in my workbook but it still cant work.
Maybe you are referring to index Sheet2 but not the name of the sheet. My code was refering to sheet name, Sheet2. See example below
The index is Sheet2 but the sheet name is BOM
 

Attachments

  • Sheet Name.jpg
    Sheet Name.jpg
    15.5 KB · Views: 9
Upvote 0
If you want to use index, then need to change to

VBA Code:
Set ws1 = ActiveWorkbook.Sheets(1)
Set ws2 = ActiveWorkbook.Sheets(2)
 
Upvote 0
Maybe you are referring to index Sheet2 but not the name of the sheet. My code was refering to sheet name, Sheet2. See example below
The index is Sheet2 but the sheet name is BOM
But my sheet name is also Sheet2
1635750597778.png
 
Upvote 0

Forum statistics

Threads
1,214,594
Messages
6,120,436
Members
448,964
Latest member
Danni317

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