Copy from one sheet to another, ignoring the zero value

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello. I want to copy data from Sheet1 to Sheet2 provided that there is a zero value from the column F to N The row is discarded and the rest copied. The following code just checks column F I want to ignore the row only if there is zero value in all cells

VBA Code:
Sub CopyData()
 Dim x, y(), i As Long, ii As Long, iii As Long
Dim lr As Long
Set st = Sheets("sheet1")
Set WS = Sheets("résultat")

 lr = st.Range("D" & Rows.Count).End(xlUp).Row
 lr2 = WS.Range("A" & Rows.Count).End(xlUp).Row
 x = st.Range("D1:N" & lr)
 For i = 1 To UBound(x, 1)
 If x(i, 3) <> 0 Then
 iii = iii + 1: ReDim Preserve y(1 To UBound(x, 2), 1 To iii)
 For ii = 1 To UBound(x, 2)
 y(ii, iii) = x(i, ii)
 Next
 End If
 Next
 With Sheets("résultat")
 WS.Range("a2:k" & lr2).ClearContents
 .[a1].Resize(iii, UBound(y, 1)) = Application.Transpose(y)
 End With
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
'If x(Cells(i, 3), Cells(i, 11)) <> 0 Then
'column F to N
 
Upvote 0

Forum statistics

Threads
1,223,992
Messages
6,175,825
Members
452,672
Latest member
missbanana

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