Error in code, not sure where?

  • Keep getting "Run-time error '91' Object variable or With block variable not set"


    I put in all the comments I could to help explain what Im trying to do here. If anyone needs to see either sheet, ask and I will attach them.


    Thanks.


  • Re: Error in code, not sure where?


    It doesnt even give me that, thus the not sure where. It would really help me out a lot as well if vba would return a line number or something letting me know what the problem seems to be.

  • Re: Error in code, not sure where?


    Not sure you can use this methodology, can you?


    If Workbooks(wb).Sheets(1).Cells(B, i).Value <> "" Then


    I would rewrite to:


    If Workbooks(wb).Sheets(1).Cells(i, 2).Value <> "" Then

  • Re: Error in code, not sure where?


    Upon stepping through, it gives the error right after line 11 which is
    "todate = Format(Today, "mmddyy") 'Format Today() as specified"


    Not sure you can use this methodology, can you?


    If Workbooks(wb).Sheets(1).Cells(B, i).Value <> "" Then


    I would rewrite to:


    If Workbooks(wb).Sheets(1).Cells(i, 2).Value <> "" Then


    Not sure either, need to call upon cell Bi, guessing that would be best method.


    change wb and diswb to string type.


    Would this not negate ability to use wb.save, wb.close, etc?

  • Re: Error in code, not sure where?


    agreed - there are a no. of errors with this that need fixing


    are you trying to save the active workbooks as discrete_date.xls etc or create an entirely new workbook and then save it?

  • Re: Error in code, not sure where?


    Change this:


    todate = Format(Today, "mmddyy") 'Format Today() as specified


    to


    todate = Format(Now, "mmddyy") 'Format Today() as specified

  • Re: Error in code, not sure where?


    First time EVER writing something in VBA, so I expect some probs.


    Sheet attached is used to create a different Discrete for each change in OSI. So upon each change in OSI, current discrete is closed, open blank workbook (too large to attach, sorry), then save, fill in data, repeat process.

  • Re: Error in code, not sure where?



    does this do what you wanted?


    NOTE: before running I suggest you change your loop else you will be looping through 65536 cells (G:G) - might be worth putting a check in there at some point... ie


    Code
    for each c in range("G:G")
    if c.value = "" then exit sub
    'rest of code
    next


    or something to that effect.

  • Re: Error in code, not sure where?


    Close, not inserting any data from the sheet and its closing out the file too soon, Workbooks(wb).Close False must be after else, gives error there? not between the Next's. Saving files to myDocuments rather than on netwrok drive also.

  • Re: Error in code, not sure where?


    reason it's not copying is because of an error in my code - when it does the first check to see if the cell is not blank it's checking the wrong file - should be DisWB and not wb ... wb is the newly created blank file.


    as for the workbook.close thing - when would you want to close the workbook...


    i am not sure that part of the code is structured correctly - depends on when you need to open and save the new DISCRETE file ... and then when you want to close it ... can you elaborate.

  • Re: Error in code, not sure where?


    actually this line is confusing....


    Code
    If Workbooks(wb).Sheets(1).Cells(i, 2).Value <> "" Then 'If line is empty insert there


    should be...


    Code
    If Workbooks(wb).Sheets(1).Cells(i, 2).Value = "" Then 'If line is empty insert there
  • Re: Error in code, not sure where?


    Code
    If IsWorkbookOpen(Foldername & Filename) Then 'Check if Filename is already created
                wb = Filename 'Pointless line, wb already equals Filename
            Else


    If the workbook is open, the OSI has already been hit at least once, so the original file was created. The lines are sorted by OSI, so as soon as all the different lines under that same OSI are added in, close and save that Discrete. Now a new disrete needs to be created for the new OSI number that was reached. The else in the above statement is when the new Discrete is going to be created, thus perfect time to close out the one being previously used.


    Any ideas on why the files are being saved under 'my documents' rather than the V:\ location thats specified?


    Also, does excel support workbooks(wb).sheets(2).print? or something of the sort?


    Thanks for all your help so far, you've really gotten me a long ways.

  • Re: Error in code, not sure where?


    Does this help?

    Boo!:yikes:

  • Re: Error in code, not sure where?


    sorry for delay ... my contributions are sadly sporadic thesedays because of work.


    Ok ....


    1. Being saved to MyDocuments because my code contains typo - "Folder" should be "Foldername" as per your variable.


    2. Still slightly confused about when you open the file... suppose OSI in row 2 is XYZ - you open the template, copy and save the data etc in DISCRETE... if OSI in row3 = XYZ what do you do? Are you ignoring whatever is in Row 3 (given the line if cells(i,2) = "" then....) implies that you do ignore duplicate OSIs... and only create a new file when you reach a new OSI. can you confirm this... simple to remedy.


    3. As for the sheet print code - Workbooks(wb).Sheets(1).PrintOut should work.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!