Searching 3 columns of values greater than 0 then pasting it to another sheet

xjpx

New Member
Joined
Jan 3, 2022
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi all, I recently started VBA for Excel for a work of mine but I haven't been able to carry out a task I am doing. I hope someone could help me out. The procedure is as follows:

1. Search columns "BR" , "BU" and "BX" for all values above 0.
2. Copy all these values and paste it in a column of another sheet of the same workbook, "Sheet 2"
3. For all the searched values, copy another set of data in the same row as it in column "GU" into "Sheet 2"

I hope my explanation is enough. If anyone requires more information please let me know.

John
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
1) Do you want to paste into same column, same row of sheet2?
2) Or solid paste (BR, then BU, then BX, one after one) into which column sheet2?
3) it would help if you upload a screenshot/image
 
Upvote 0
@bebo021999 Hi, for example, for Column BR has a value 99, column BU has a value 70 and 8110 (first to third image). Then I want to paste it into "Sheet2" in a column (fourth image). At the same time I want to copy the data in the same rows as the 3 values "99","70" and "8110" in column "GU" which is the time (fifth image). The final product should look like the sixth image which is in sheet 2.
1641193927351.png
1641193869595.png
1641193971929.png
1641194365536.png
1641194399800.png
1641194502396.png
 
Upvote 0
VBA Code:
Option Explicit
Sub test()
Dim Lr1&, Lr2&, Lr3&, i&
Dim cell As Range
Dim arr(1 To 1000000, 1 To 2)
With Sheets("Sheet1")
    Lr1 = .Cells(Rows.Count, "BR").End(xlUp).Row
    Lr2 = .Cells(Rows.Count, "BU").End(xlUp).Row
    Lr3 = .Cells(Rows.Count, "BX").End(xlUp).Row
    For Each cell In .Range("BR2:BR" & Lr1)
        If cell > 0 Then
            i = i + 1
            arr(i, 1) = cell
            arr(i, 2) = cell.Offset(, 133)
        End If
    Next
        For Each cell In .Range("BU2:BU" & Lr2)
            If cell > 0 Then
                i = i + 1
                arr(i, 1) = cell
                arr(i, 2) = cell.Offset(, 130)
            End If
        Next
            For Each cell In .Range("BX2:BX" & Lr3)
                If cell > 0 Then
                    i = i + 1
                    arr(i, 1) = cell
                    arr(i, 2) = cell.Offset(, 127)
                End If
            Next
End With
Sheets("Sheet2").Cells(2, 1).Resize(i, 2).Value = arr
End Sub
 
Upvote 0
Solution
VBA Code:
Option Explicit
Sub test()
Dim Lr1&, Lr2&, Lr3&, i&
Dim cell As Range
Dim arr(1 To 1000000, 1 To 2)
With Sheets("Sheet1")
    Lr1 = .Cells(Rows.Count, "BR").End(xlUp).Row
    Lr2 = .Cells(Rows.Count, "BU").End(xlUp).Row
    Lr3 = .Cells(Rows.Count, "BX").End(xlUp).Row
    For Each cell In .Range("BR2:BR" & Lr1)
        If cell > 0 Then
            i = i + 1
            arr(i, 1) = cell
            arr(i, 2) = cell.Offset(, 133)
        End If
    Next
        For Each cell In .Range("BU2:BU" & Lr2)
            If cell > 0 Then
                i = i + 1
                arr(i, 1) = cell
                arr(i, 2) = cell.Offset(, 130)
            End If
        Next
            For Each cell In .Range("BX2:BX" & Lr3)
                If cell > 0 Then
                    i = i + 1
                    arr(i, 1) = cell
                    arr(i, 2) = cell.Offset(, 127)
                End If
            Next
End With
Sheets("Sheet2").Cells(2, 1).Resize(i, 2).Value = arr
End Sub
Thank you! It runs and is able to copy out the Faults. But how do I copy the time, column "GU", into sheet 2 as well? As seen in the second image
1641199856657.png
1641199925077.png
 
Upvote 0
What! I believe that I copy both:
arr(i, 1) = cell =>" Fault
arr(i, 2) = cell.Offset(, 127) => Time

And paste:
Sheets("Sheet2").Cells(2, 1).Resize(i, 2).Value = arr

Above is for column BX. 127 is distance from column BX to GU.
 
Upvote 0
What! I believe that I copy both:
arr(i, 1) = cell =>" Fault
arr(i, 2) = cell.Offset(, 127) => Time

And paste:
Sheets("Sheet2").Cells(2, 1).Resize(i, 2).Value = arr

Above is for column BX. 127 is distance from column BX to GU.
Yes, I understand what you are doing but it still doesn't copy the time.
 
Upvote 0
It still works for me!
You can test by debug.print within first loop
IN VBA edit mode, Ctrl-G to open "Immediate window" to see:
PHP:
Option Explicit
Sub test()
Dim Lr1&, Lr2&, Lr3&, i&
Dim cell As Range
Dim arr(1 To 1000000, 1 To 2)
With Sheets("Sheet1")
    Lr1 = .Cells(Rows.Count, "BR").End(xlUp).Row
    Lr2 = .Cells(Rows.Count, "BU").End(xlUp).Row
    Lr3 = .Cells(Rows.Count, "BX").End(xlUp).Row
    For Each cell In .Range("BR2:BR" & Lr1)
        If cell > 0 Then
            i = i + 1
            arr(i, 1) = cell
            arr(i, 2) = cell.Offset(, 133)
debug.print arr(i,2) ' add this to print out the instant results.
        End If
    Next
        For Each cell In .Range("BU2:BU" & Lr2)
            If cell > 0 Then
                i = i + 1
                arr(i, 1) = cell
                arr(i, 2) = cell.Offset(, 130)
            End If
        Next
            For Each cell In .Range("BX2:BX" & Lr3)
                If cell > 0 Then
                    i = i + 1
                    arr(i, 1) = cell
                    arr(i, 2) = cell.Offset(, 127)
                End If
            Next
End With
Sheets("Sheet2").Cells(2, 1).Resize(i, 2).Value = arr
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

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