+ Reply to Thread
Results 1 to 14 of 14

Object Variable or with block variable not set.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Object Variable or with block variable not set.

    I have an error that I just can't figure.

    Option Explicit
    Public Sub CommandButton1_Click()
        Dim CurrentDate As String
        Dim PriorDate As String
        Dim WrkbookPrior As Workbook
        Dim WrkbookCurrent As Workbook
        Dim WrkbookCompare As Workbook
        Dim FilePath1 As String
        Dim FilePath2 As String
        Dim FileNamePrefix1 As String
        Dim FileNamePrefix2 As String
        
        FilePath1 = "XXX:\Users\YYYYY1\Desktop\"
        
        PriorDate = Format(TextBoxPriorDate.Text, " mm-dd-yy")
        CurrentDate = Format(TextBoxCurrentDate.Text, " mm-dd-yy")
        
        FileNamePrefix1 = "Option EPMS Valuation"
        FileNamePrefix2 = "Option EPMS Valuation Tool"
          
        WrkbookPrior = FilePath1 & FileNamePrefix1 & PriorDate & ".xlsx" <--------- Error
        WrkbookCurrent = FilePath1 & FileNamePrefix1 & CurrentDate & ".xlsx"
        WrkbookCompare = FilePath1 & FileNamePrefix2 & ".xlsx"
        
        Workbooks.Open WrkbookPrior
            ThisWorkbook.Sheets.Copy After:=Workbooks("WrkbookCompare.xlsx").Worksheets(Workbooks("WrkbookCompare.xls").Worksheets.Count)
        Workbooks.Open WrkbookCurrent
            ThisWorkbook.Sheets.Copy After:=Workbooks("WrkbookCompare.xlsx").Worksheets(Workbooks("WrkbookCompare.xls").Worksheets.Count)
    
    End Sub
    Last edited by AnthonyWB; 06-22-2011 at 12:32 PM.

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Object Variable or with block variable not set.

    This error is occuring because you are trying to set a workbook type variable to a string value. Try this instead:

    Set WrkbookPrior = Workbooks(FilePath1 & FileNamePrefix1 & PriorDate & ".xlsx").Open
    Instead of
    WrkbookPrior = FilePath1 & FileNamePrefix1 & PriorDate & ".xlsx"
    &
    Workbooks.Open WrkbookPrior
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Object Variable or with block variable not set.

    I am now getting a subscript out of range:

     Set WrkbookPrior = Workbooks(FilePath1 & FileNamePrefix1 & PriorDate & ".xlsx").Open
    Any ideas?

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Object Variable or with block variable not set.

    Sounds like the file is not there. Make sure you have all your slashes.

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Object Variable or with block variable not set.

    well the file name should read as an example:

    Option EPMS Valuation 3-31-11.xlsx

    Perhaps I need to account for the spaces?

  6. #6
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Object Variable or with block variable not set.

    Look at what you have put together in your code. Right now it is:

    XXX:\Users\YYYYY1\Desktop\Option EPMS Valuation 03-31-11.xls.

    You need to change the month to a single character to get rid of the zero in front of the 3.

  7. #7
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Object Variable or with block variable not set.

    Thanks, but still not working. Take a look at what I have done:


        PriorDate = Format(TextBoxPriorDate.Text, "m-dd-yy")
        CurrentDate = Format(TextBoxCurrentDate.Text, "m-dd-yy")
    and then,

    Set WrkbookPrior = Workbooks(FilePath1 & FileNamePrefix1 & PriorDate & ".xlsx").Open
        Set WrkbookCurrent = Workbooks(FilePath1 & FileNamePrefix1 & CurrentDate & ".xlsx").Open

    I am still geting a subscript out of range.

  8. #8
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Object Variable or with block variable not set.

    Use the immediate window to check the file path and name and compare it to the filepath and name in the windows explorer.

  9. #9
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Object Variable or with block variable not set.

    Dave thanks for following me on this one.

    Acording to the Immediate window:

    ? WrkbookPrior

    yields the error: Object Variable or with Block Variable not set. All the other variables pan out exactly the way they should.

  10. #10
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Object Variable or with block variable not set.

    Do this:

    ? FilePath1 & FileNamePrefix1 & PriorDate & ".xlsx"

  11. #11
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Object Variable or with block variable not set.

    Yep its what I thought, there is no space after Valuation and date.

    .......1\Desktop\Option EPMS Valuation12-31-10.xlsx
    we just need to add a space. I have tried the following and it does not correct the issue:

    ........riorDate.Text, " m-dd-yy")
    Note the space in the date format.

  12. #12
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Object Variable or with block variable not set.

    Like this instead:

    Set WrkbookPrior = Workbooks(FilePath1 & FileNamePrefix1 & " " & PriorDate & ".xlsx").Open

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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