The purpose of my code is to:
Clean the two destination column
At source worksheet jump to last cell(1048576) in a specific column from bottom, jump to the last cell with data, and select from there to the top cell
copy, paste, and remove duplicates (the removeduplicates is a working part)
I have to select the range from bottom because of embedded empty cells that prevents excel to select further cells with data.
The line that performs selection seems correct after multiple checks, and also tried to
- use string form ("AC") for parameter
- number form (13)
- put the cell number in a Range()
Despite all my efforts the line gives an "Run-time error '1004' : Method 'Range' of object'_Global' failed" error.
I removed even all other params except for the, single cell reference, tried to rearrange my code and solve by some other way.
The code part
Sheets("Data").Cells(Rows.Count, "AC").End(xlUp).Row
returns the value(row number) of the first cell with data from bottom. That would be the end of the selection. I know that there are some parts that doesn't match the description, but they are also irrelevant in terms of the error. (for example at line 17-18, it selects the entire column, but I can fix that later.)
Here is my code, I put a comment next to the problematic part.
Sub CopyUniqueProcList()
Dim ContainWord As String
Dim SrcSheet As Worksheet
Dim DestSheet As Worksheet
Dim TypeRng As Range
Dim TypeRngDest As Range
Dim GrundRng As Range
Dim GrundRngDest As Range
Dim TRD_E As Integer
Dim GRD_E As Integer
Set SrcSheet = Worksheets("Data")
Set DestSheet = Worksheets("lTopTenHelper")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set TypeRng = SrcSheet.Range("AC4")
Set TypeRngDest = DestSheet.Range("A1")
Set TRD_E = DestSheet.Cells(Rows.Count, "A")
Set GrundRng = SrcSheet.Range("AE4")
Set GrundRngDest = DestSheet.Range("D1")
Set GRD_E = DestSheet.Cells(Rows.Count, "D")
TRD_E = DestSheet.Cells(DestSheet.Rows.Count, "A").End(xlUp).Row
GRD_E = DestSheet.Cells(DestSheet.Rows.Count, "D").End(xlUp).Row
Sheets("lTopTenHelper").Range(TypeRngDest, "A" & TRD_E).Clear
Sheets("lTopTenHelper").Range(GrundRngDest, "D" & GRD_E).Clear
If TypeRng.Value <> 0 Then
Range(TypeRng, "AC" & Sheets("Data").Cells(Rows.Count, "AC").End(xlUp).Row).Copy 'Error thrown here
Sheets("lTopTenHelper").Cells(Rows.Count, "A").End(xlUp).Offset(0).PasteSpecial Paste:=xlPasteValues
End If
Sheets("lTopTenHelper").Range(TypeRngDest, "AC" & Sheets("Data").Cells(Rows.Count, "AC").End(xlUp).Row).RemoveDuplicates Columns:=Array(1), Header:=xlNo
If GrundRng.Value <> 0 Then
Range(GrundRng, "AE" & Sheets("Data").Cells(Rows.Count, "AE").End(xlUp).Row).Copy
Sheets("lTopTenHelper").Cells(Rows.Count, "D").End(xlUp).Offset(0).PasteSpecial Paste:=xlPasteValues
End If '6?
Sheets("lTopTenHelper").Range(GrundRngDest, "AE" & Sheets("Data").Cells(Rows.Count, "AE").End(xlUp).Row).RemoveDuplicates Columns:=Array(1), Header:=xlNo
Application.Calculation = xlCalculationAutomatic
Exit Sub
End Sub
Any help/suggestion/other solution of task is welcome. Thanks in advance.