1

I have a "My Quote" template workbook which has a quote number in cell "I1" of the "Quote" sheet.

Upon opening that workbook I want to go to another (unopened) "Quote List" workbook, find the last row and look at the last quote number in column A of the "Quotes" sheet.

I then wish to add 1 to it and enter it in cell "I1" of the already open "My Quote" workbook (I.E. find the last quote number, say 3001 in the "Quotes" sheet of the "Quote List" workbook, and enter 3002 in the "Quote" sheet of the "My Quote" workbook).

Below errors on

Set wb1 = Workbooks("C:\Quotes\Quote List.xlsx")

The path and spreadsheet name / type are correct.

I am using Excel from Office 2019.

Sub Workbook_Open()
Dim wb1 As Workbook 'Quote List
Dim wb2 As Workbook 'My Quote
Dim ws1 As Worksheet 'Quotes
Dim ws2 As Worksheet 'Quote

Set wb1 = Workbooks("C:\Quotes\Quote List.xlsx")
Set ws1 = wb1.Sheets("Quotes")
Set wb2 = ActiveWorkbook
SourcePath = "C:\Quotes"
Set ws2 = wb2.Sheets("Quote")
lastrow = ws1.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws2.Range("I1").Value = ws1.Range(lastrow).Value + 1

End Sub
1
  • Try Set wb1 = Workbooks.Open("C:\Quotes\Quote List.xlsx"). Commented Jul 7, 2024 at 8:16

2 Answers 2

0
  • Set wb1 = Workbooks("C:\Quotes\Quote List.xlsx") doesn't open the file.
  • ws1.Range(lastrow).Value has syntax error. The column name is missing. eg. ws1.Range("A" & lastrow).Value

Try:

Sub Workbook_Open()
    Dim wb1 As Workbook 'Quote List
    Dim wb2 As Workbook 'My Quote
    Dim ws1 As Worksheet 'Quotes
    Dim ws2 As Worksheet 'Quote

    Set wb2 = ThisWorkbook
    Set ws2 = wb2.Sheets("Quote")
    On Error Resume Next
    Set wb1 = Workbooks("Quote List.xlsx")
    On Error GoTo 0
    If wb1 Is Nothing Then
        Set wb1 = Workbooks.Open("C:\Quotes\Quote List.xlsx")
    End If
    Set ws1 = wb1.Sheets("Quotes")
    ws2.Range("I1").Value = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Value + 1
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this as looked much simpler and closer to what I had but it errored out on the last line. It opened the source spreadsheet ok but just stopped and gave a yellow line on the last one.
I've updated the code to correct the typo. Pls try again.
0

A Workbook Open: Retrieve Value from Closed Workbook

Option Explicit

Sub Workbook_Open()
    
    ' Define constants.
    Const SRC_FOLDER_PATH As String = "C:\Quotes\"
    Const SRC_FILE_NAME As String = "Quote List.xlsx"
    
    ' Attempt to reference the source workbook.
    Dim swb As Workbook:
    On Error Resume Next
        Set swb = Workbooks(SRC_FILE_NAME)
    On Error GoTo 0
    
    Dim sFilePath As String, WasSourceWorkbookOpen As Boolean
    
    ' If it's not open, open it.
    If swb Is Nothing Then ' source file is not open
        sFilePath = SRC_FOLDER_PATH & SRC_FILE_NAME
        Set swb = Workbooks.Open(sFilePath)
    Else ' source file is open
        WasSourceWorkbookOpen = True
    End If
    
    ' Reference the source worksheet.
    Dim sws As Worksheet: Set sws = swb.Sheets("Quotes")
    ' Reference the last source cell in column 'A'.
    Dim scell As Range:
    Set scell = sws.Cells(sws.Rows.Count, "A").End(xlUp)
    
    ' Reference the destination workbook, the workbook with the code.
    Dim dwb As Workbook: Set dwb = Me ' or 'Set dwb = ThisWorkbook'
    ' Reference the destination worksheet.
    Dim dws As Worksheet: Set dws = dwb.Sheets("Quote")
    ' Reference the destination cell.
    Dim dcell As Range: Set dcell = dws.Range("I1")
    
    ' Copy integer increased by 1 from source to destination.
    dcell.Value = scell.Value + 1
    
    ' Close the source workbook.
    If Not WasSourceWorkbookOpen Then ' was not open; close it
        swb.Close SaveChanges:=False
    'Else ' was open; do nothing???
    End If

    ' Inform.    
    MsgBox "Number retrieved.", vbInformation

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.