TheRedCardinal
Active Member
- Joined
- Jul 11, 2019
- Messages
- 252
- Office Version
- 365
- 2021
- Platform
- Windows
Hi all,
My basic requirement is this:
I have a table of data that has been through some processing already. In it, there will be rows where a certain column, labelled "Invoice" will be blank.
In those cases, I want to cut the whole row, and then paste it underneath the table in a new row.
I have made the following code:
The purpose of this code should be obvious. It starts at the bottom of the table and works up. It looks in Cell (Counter, InvoiceColumn) to see if it's blank.
If it is, it cuts the whole row, and pastes it into PasteRow.
It then increases Paste Row by 1 so the next row goes in the right place.
Loop up until 2.
I think the problem is in the characterisation of the Column Tab, which when I debug is called "Invoice" rather than what I would expect, which is 3 (Invoice in this example is found in column c).
So I think this line:
Is not not doing the thing I thought it would, which is to return the column reference to the cell which matches "Invoice".
The error by the way is "Runtime Error 1004 - Method Range of Object Global Failed.
My basic requirement is this:
I have a table of data that has been through some processing already. In it, there will be rows where a certain column, labelled "Invoice" will be blank.
In those cases, I want to cut the whole row, and then paste it underneath the table in a new row.
I have made the following code:
Code:
[SIZE=2][FONT=arial]'A Sub to deal with intrastat lines with no invoices[/FONT][/SIZE]
[SIZE=2][FONT=arial]'Assumption = will be removed[/FONT][/SIZE]
[SIZE=2][FONT=arial]
[/FONT][/SIZE]
[SIZE=2][FONT=arial]Dim TableRows As Long, PasteRow As Long, Counter As Long, InvoiceColumn As Range[/FONT][/SIZE]
[SIZE=2][FONT=arial]
[/FONT][/SIZE][SIZE=2][FONT=arial]Set ws1 = Sheets("2. Final Data")[/FONT][/SIZE]
[SIZE=2][FONT=arial]ws1.Activate[/FONT][/SIZE]
[SIZE=2][FONT=arial]
[/FONT][/SIZE]
[SIZE=2][FONT=arial] With ws1[/FONT][/SIZE]
[SIZE=2][FONT=arial] TableRows = .Cells(Rows.Count, 1).End(xlUp).Row[/FONT][/SIZE]
[SIZE=2][FONT=arial] Set InvoiceColumn = .Range("A1:Z1").Find("Invoice")[/FONT][/SIZE]
[SIZE=2][FONT=arial] End With[/FONT][/SIZE]
[SIZE=2][FONT=arial]Cells(TableRows + 2, 1) = "Lines Removed From Intrastat"[/FONT][/SIZE]
[SIZE=2][FONT=arial]PasteRow = TableRows + 3[/FONT][/SIZE]
[SIZE=2][FONT=arial]
[/FONT][/SIZE]
[SIZE=2][FONT=arial] For Counter = TableRows To 2 Step -1[/FONT][/SIZE]
[SIZE=2][FONT=arial] If Range(Counter, InvoiceColumn).Value = "" Then[/FONT][/SIZE]
[SIZE=2][FONT=arial] Rows(Counter).EntireRow.Cut[/FONT][/SIZE]
[SIZE=2][FONT=arial] Rows(PasteRow).Insert Shift:=xlDown[/FONT][/SIZE]
[SIZE=2][FONT=arial] PasteRow = PasteRow + 1[/FONT][/SIZE]
[SIZE=2][FONT=arial] End If[/FONT][/SIZE]
[SIZE=2][FONT=arial] Next Counter[/FONT][/SIZE]
[SIZE=2][FONT=arial]
[/FONT][/SIZE]
[SIZE=2][FONT=arial]End Sub[/FONT][/SIZE]
The purpose of this code should be obvious. It starts at the bottom of the table and works up. It looks in Cell (Counter, InvoiceColumn) to see if it's blank.
If it is, it cuts the whole row, and pastes it into PasteRow.
It then increases Paste Row by 1 so the next row goes in the right place.
Loop up until 2.
I think the problem is in the characterisation of the Column Tab, which when I debug is called "Invoice" rather than what I would expect, which is 3 (Invoice in this example is found in column c).
So I think this line:
Code:
Set InvoiceColumn = .Range("A1:Z1").Find("Invoice")
Is not not doing the thing I thought it would, which is to return the column reference to the cell which matches "Invoice".
The error by the way is "Runtime Error 1004 - Method Range of Object Global Failed.