Multiple offset cells in range no idea why this code doesnt work

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
I have this code
VBA Code:
Private Sub CommandButton1_Click()
Dim rng As Range, cell As Range, lr As Long


Set rng = Range("H144:H174")
For Each cell In rng
    If Not IsEmpty(cell) And cell.Value <> 0 Then
    Range(cell.Offset(0, -5), cell.Offset(0, -4), cell.Offset(0, -3), cell.Offset(0, 2)).Copy
    lr = Sheets("Lista materiałowa").Cells(Rows.Count, "B").End(xlUp).Row
    Sheets("Lista materiałowa").Cells(lr + 1, "B").PasteSpecial Paste:=xlPasteValues
    Else
    MsgBox "No data"

End If
Next cell

End Sub

For some reason line:
Range(cell.Offset(0, -5), cell.Offset(0, -4), cell.Offset(0, -3), cell.Offset(0, 2)).Copy does not work dunno why.

1639660101334.png


Can someone help me a little?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try
VBA Code:
    Union(cell.Offset(0, -5), cell.Offset(0, -4), cell.Offset(0, -3), cell.Offset(0, 2)).Copy
 
Upvote 0
Solution
Also there is a problem since the cell I want to use are merged I get
1639660999773.png
 
Upvote 0
Fixed the issue I went about it the wrong way
I wanted to copy those cells in 1 row on another sheet and code above wanted to put them into 1 collumn
Here's the fixed code i made to make it happen just for future "researchers"

VBA Code:
Private Sub CommandButton1_Click()
Dim rng As Range, cell As Range, lr As Long


Set rng = Range("H144:H174")
For Each cell In rng
    If Not IsEmpty(cell) And cell.Value <> 0 Then
    lr = Sheets("Lista materiałowa").Cells(Rows.Count, "B").End(xlUp).Row
    cell.Offset(0, -5).Copy
        Sheets("Lista materiałowa").Cells(lr + 1, "B").PasteSpecial Paste:=xlPasteValues
    lr = Sheets("Lista materiałowa").Cells(Rows.Count, "C").End(xlUp).Row
    cell.Offset(0, -4).Copy
        Sheets("Lista materiałowa").Cells(lr + 1, "C").PasteSpecial Paste:=xlPasteValues
    lr = Sheets("Lista materiałowa").Cells(Rows.Count, "D").End(xlUp).Row
    cell.Offset(0, -3).Copy
        Sheets("Lista materiałowa").Cells(lr + 1, "D").PasteSpecial Paste:=xlPasteValues
    lr = Sheets("Lista materiałowa").Cells(Rows.Count, "E").End(xlUp).Row
    cell.Copy
        Sheets("Lista materiałowa").Cells(lr + 1, "E").PasteSpecial Paste:=xlPasteValues
    lr = Sheets("Lista materiałowa").Cells(Rows.Count, "F").End(xlUp).Row
    cell.Offset(0, 2).Copy
        Sheets("Lista materiałowa").Cells(lr + 1, "F").PasteSpecial Paste:=xlPasteValues
    
    Else
    MsgBox "No data"

End If
Next cell

End Sub

However the Union instead of Range would've worked aswell if these cells weren't merged so thumbs up for you :)
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,518
Messages
6,119,988
Members
448,935
Latest member
ijat

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