Error 1004 - Method Range of Object Worksheet failed

Razor_Rob

Board Regular
Joined
Aug 18, 2022
Messages
65
Office Version
  1. 365
Platform
  1. Windows
Im getting the Error 1004 Method Range of Object Worksheet failed, not sure what Im missing here...

VBA Code:
Option Explicit

Sub CleanUp()

Dim wbImport As Workbook
Dim wsImport As Worksheet
Dim cell As Range
Dim fileNameAndPath As Variant
Dim lDestLastRow As Long

    'Open a workbook
    fileNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.csv), *.csv", Title:="Select File To Be Opened")
    If fileNameAndPath = False Then Exit Sub
    'Workbooks.Open Filename:=fileNameAndPath
    Set wbImport = Workbooks.Open(Filename:=fileNameAndPath, ReadOnly:=True)
    Set wsImport = wbImport.Worksheets(1)
    
Application.EnableEvents = False

    'Add Zero in front of the mobile number
    With wsImport
        .Range("Q3:Q" & lDestLastRow).NumberFormat = "@"            'format range as text
       
        For Each cell In .Range("Q3:Q" & lDestLastRow)
             cell.Value = Format(cell * 1, "0000000000")      'Convert each cell
        Next cell
    End With
    
    'Remove spaces from the mobile number
    With wsImport
        .Range("Q3:Q" & lDestLastRow).Replace " ", vbNullString, xlPart
    End With
    
    'Clear contents of existing data range
    wsImport.Range("AO3:AY,BB3:BF " & lDestLastRow).ClearContents
    
    'Close the Source Workbook
    wbImport.Close SaveChanges:=False
    
    MsgBox "Added Zeros in front of the Mobile Number and Deleted Employer Info not required", vbOKOnly
    
    Application.EnableEvents = True
    
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
When does lDestLastRow get set?
 
Upvote 0
Solution

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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