Home » excel » excel – How to overcome Error 1004 range of object _global failed

excel – How to overcome Error 1004 range of object _global failed

Posted by: admin May 14, 2020 Leave a comment

Questions:

Here i am trying to copy data from one sheet to another, where i need to exclude one particular column from copy sheet

Like :
Think i have 2 sheets( with names as “Combined”,”HSR”) from HSR i need to copy all the data to Combined sheet excluding column “D” values

Dim ws As Worksheet
Dim ColumnLetter As Variant
    Set ws = Sheets("HSR")
    Worksheets("HSR").Select
    lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
    lr2 = Sheets("Combined").Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
    ColumnLetter = Split(ws.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Cells.Address(1, 0), "$")(0)
    Range("A2:C" & lr).Copy Sheets("Combined").Range("A" & lr2 + 1)
    Range("E2:ColumnLetter" & lr).Copy Sheets("Combined").Range("D" & lr2 + 1)
End Sub

I expect the output as, in sheet Combined i shouldn’t get the values that are in column D from HSR sheet

How to&Answers:

ColumnLetter is a variable. You are using it as a String.

Change

Range("E2:ColumnLetter" & lr).Copy Sheets("Combined").Range("D" & lr2 + 1)

to

Range("E2:" & ColumnLetter & lr).Copy Sheets("Combined").Range("D" & lr2 + 1)

Also few tips

  1. lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row and lr2 = Sheets("Combined").Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row will give you error if .Find doesn’t return anyting. Handle it as shown in .Find and .FindNext

  2. Avoid the use of .Select. You may want to see How to avoid using Select in Excel VBA