Solved

VBA code gives error on the range.resize

Posted on 2016-10-05
3
50 Views
Last Modified: 2016-10-05
I am using this code  and it gives error  Application defined or object defined error runtime error 1004

but if i remove the resize for example the first line FROM wb.Worksheets("PRODUCT").Range("C17").Resize(4, 0).Value = "NT" TO wb.Worksheets("PRODUCT").Range("C17").Value = "NT"  it works.  

i am wondering why using the resize do not work.  any idea?

Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xlsx"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
      Set wb = Workbooks.Open(Filename:=myPath & myFile)
    
    'Ensure Workbook has opened before moving on to next line of code
      DoEvents
    


     wb.Worksheets("PRODUCT").Range("C17").Resize(4, 0).Value = "NT"
   wb.Worksheets("PRODUCT").Range("C21").Resize(3, 0).Value = "ER"
  wb.Worksheets("PRODUCT").Range("B9").Value = "CE"
    wb.Worksheets("PRODUCT").Range("D23").Value = "Y"
   wb.Worksheets("PRODUCT").Range("E29").Resize(0, 1).Value = "N"
    wb.Worksheets("PRODUCT").Range("G29").Value = "B"
   wb.Worksheets("PRODUCT").Range("B33").Value = "C"
    
    'Save and Close Workbook
      wb.Close SaveChanges:=True
      
    'Ensure Workbook has closed before moving on to next line of code
      DoEvents

    'Get next file name
      myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

Open in new window

0
Comment
Question by:excelismagic
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41830177
It should be Resize(4, 1) instead of Resize(4, 0). Resize is different than Offset. A range cannot be resized to the zero column.
0
 
LVL 3

Author Closing Comment

by:excelismagic
ID: 41830202
thank you so much Neeraj. very much appreciated.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41830304
You're welcome. Glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now