+ Reply to Thread
Results 1 to 4 of 4

Runtime Error 1004 in VBA code

  1. #1
    Registered User
    Join Date
    01-21-2020
    Location
    india
    MS-Off Ver
    2016
    Posts
    12

    Lightbulb Runtime Error 1004 in VBA code

    HI Team,

    I am getting this error when running the code for the task raw data file. Below is the error.
    VBA error.PNG

    Below bold line item in the code causing this error. Please help me to resolve this error.


    Public Sub InitializeTaskRawData(functionalDemandbyStudyReportFileName As String)
    Dim startTime As Double
    startTime = Timer

    Dim Wksht As Worksheet
    Set Wksht = brainsWorkbook.Sheets("Task Summary")

    Dim calculatedDateString As String
    calculatedDateString = Left(Right(functionalDemandbyStudyReportFileName, 24), 19)



    TaskrawDataExportFilename = "Functional Demand Task Raw Data-" & calculatedDateString & ".xlsx"


    Dim taskrawDataWksht As Worksheet
    Set taskrawDataWksht = Application.Workbooks.Add.Sheets(1)
    taskrawDataWksht.Name = "Task Raw Data"

    taskrawDataWksht.SaveAs mainExportFolder & TaskrawDataExportFilename




    Dim inputReportWorkbook As Workbook
    Set inputReportWorkbook = Application.Workbooks.Open(functionalDemandbyStudyReportFileName)

    'Validate_InputFile

    Dim rawDataRow As Long
    rawDataRow = 2

    Dim startColumn As Integer
    startColumn = 0


    Dim startRow As Integer
    startRow = 13

    Dim lastRow As Long
    lastRow = Wksht.Cells.Find(What:="*", _
    After:=Range("A1"), _
    LookAt:=xlPart, _
    LookIn:=xlFormulas, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, _
    MatchCase:=False).row

    Dim lastCol As Long
    ' lastCol = inputReportWorkbook.Sheets(1).Cells.Find(What:="*", _
    ' After:=Range("A1"), _
    ' LookAt:=xlPart, _
    ' LookIn:=xlFormulas, _
    ' SearchOrder:=xlByColumns, _
    ' SearchDirection:=xlPrevious, _
    ' MatchCase:=False).Column
    lastCol = startColumn + numberOfMonths


    taskrawDataWksht.Cells(1, startColumn + 1).Value = "Study"
    taskrawDataWksht.Cells(1, startColumn + 2).Value = "Resource"
    taskrawDataWksht.Cells(1, startColumn + 3).Value = "Region"
    taskrawDataWksht.Cells(1, startColumn + 4).Value = "Task"
    taskrawDataWksht.Cells(1, startColumn + 5).Value = "Travel"


    startDate = DateValue(UserForm.TextBoxStartDate.Value)
    stopDate = DateValue(UserForm.TextBoxStopDate.Value)

    'numberOfMonths = 144 + 24 + 120
    numberOfMonths = 1 + date_diff_to_months(startDate, stopDate)

    taskrawDataWksht.Activate
    Range("F1").Activate
    'Populate the dates for as many months has have been selected
    Dim j As Integer
    For j = 0 To numberOfMonths - 2
    ActiveCell.offset(0, j).Value = DateAdd("m", j, startDate)
    ActiveCell.offset(0, j).NumberFormat = "m/d/yyyy"
    Next j

    Dim taskrawDataWorkbook As Workbook

    If (IsWorkBookOpen(mainExportFolder & TaskrawDataExportFilename)) Then
    Set taskrawDataWorkbook = Application.Workbooks(TaskrawDataExportFilename)
    Else
    Set taskrawDataWorkbook = Application.Workbooks.Open(mainExportFolder & TaskrawDataExportFilename)
    End If



    'taskrawDataWorkbook.Close True

    End Sub


    Thanks

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Runtime Error 1004 in VBA code

    That's usually caused by one of following.

    1. Folder path does not exist. You have to create folder prior to saving in it, if it does not.
    2. You don't have appropriate permission in the folder.
    3. file name lacks extension.

    According to error message most likely cause is #1.

    Oh and please nest your code in code tag using "#" button.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    01-21-2020
    Location
    india
    MS-Off Ver
    2016
    Posts
    12

    Re: Runtime Error 1004 in VBA code

    Thanks CK, I have created the path for it. Can you suggest what file name it should be?

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Runtime Error 1004 in VBA code

    ? What ever the file name you want to save it as (remember to exclude standard illegal characters like \/:*?"<>|) followed by file type (.xlsx, .xlsb, .xlsm etc).

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] runtime error 1004 in copy and close workbook vba code
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2018, 03:23 PM
  2. [SOLVED] Vba code Runtime error worksheet failed 1004
    By sintek in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 01-03-2016, 07:04 AM
  3. Problem with VBA code - runtime error 1004
    By ExcelFailure in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2015, 02:22 AM
  4. [SOLVED] Runtime error 1004 with my saveas code
    By Anto_BT in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-26-2015, 11:49 AM
  5. [SOLVED] 'Runtime Error 1004' when trying to Rename Worksheet using vba code
    By Raporter65 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-28-2013, 07:43 PM
  6. [SOLVED] Range error in code, runs alone but not inside my full program, giving runtime error 1004
    By charizzardd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-23-2012, 03:34 PM
  7. Runtime Error Code 1004
    By laks in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-19-2007, 07:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1