+ Reply to Thread
Results 1 to 11 of 11

Reference to External Workbook Objects

Hybrid View

  1. #1
    Registered User
    Join Date
    10-22-2009
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    80

    Reference to External Workbook Objects

    Hi all

    I'm having trouble working this around, i use the following code in one of my Workbook sheets.

    Private Sub CommandButton1_Click()
    
    Dim WsTarget As Worksheet
    Dim WsSource As Worksheet
    Dim CurRng As Range
    
    
    Windows("Arrival Form.xls").Activate
    Set WsSource = Worksheets("Arrival Form")
    Windows("Main.xlsm").Activate
    Set WsTarget = Worksheets("Bookings")
    Set CurRng = WsTarget.Range("A1000").End(xlUp).Offset(1, 0)
    
    
    CurRng.Offset(0, 0) = TextBox1.Value 'House
    CurRng.Offset(0, 2) = Calendar2.Value 'Date of Departure
    CurRng.Offset(0, 3) = TextBox2.Value 'Cleaning
    CurRng.Offset(0, 8) = ComboBox13.Value 'Supplier
    CurRng.Offset(0, 1) = Calendar1.Value 'Date of Arrival
    CurRng.Offset(0, 19) = TextBox3.Value 'Special Requests
    CurRng.Offset(0, 11) = TextBox4.Value 'Name
    CurRng.Offset(0, 12) = TextBox5.Value 'Phone Number
    CurRng.Offset(0, 13) = TextBox6.Value 'Mobile Number
    CurRng.Offset(0, 14) = TextBox7.Value 'Email Address
    
    'Cleaning'
    If TextBox2.Value = "" Then
    CurRng.Offset(0, 3) = "?"
    End If
    
    'Food'
    If ComboBox1.Value = "Yes" Then
        CurRng.Offset(0, 15) = "Sim"
    ElseIf ComboBox1 = "No" Then
        CurRng.Offset(0, 15) = "Não"
    End If
    
    'High Chair'
    If ComboBox2.Value = "Yes" Then
        CurRng.Offset(0, 18) = "Sim"
    ElseIf ComboBox2.Value = "No" Then
        CurRng.Offset(0, 18) = "Não"
    End If
    
    'Pool Heating'
    If ComboBox3.Value = "Yes" Then
        CurRng.Offset(0, 17) = "Sim"
    ElseIf ComboBox3.Value = "No" Then
        CurRng.Offset(0, 17) = "Não"
    End If
    
    'Cot'
    If ComboBox4.Value = "Yes" Then
        CurRng.Offset(0, 16) = "Sim"
    ElseIf ComboBox4.Value = "No" Then
        CurRng.Offset(0, 16) = "Não"
    End If
    
    'Entered on Calendar'
    If ComboBox10.Value = "Yes" Then
        CurRng.Offset(0, 10) = "Sim"
    ElseIf ComboBox10.Value = "No" Then
        CurRng.Offset(0, 10) = "Não"
    End If
    
    'Clean on arrival'
    If ComboBox6.Value = "Yes" Then
      CurRng.Offset(0, 5) = "Sim"
    ElseIf ComboBox6.Value = "No" Then
      CurRng.Offset(0, 5) = "Não"
    End If
    
    'Clean on Departure'
    If ComboBox7.Value = "Yes" Then
      CurRng.Offset(0, 6) = "Sim"
    ElseIf ComboBox7.Value = "No" Then
      CurRng.Offset(0, 6) = "Não"
    End If
    
    'Wash Laundry on Departure'
    If ComboBox9.Value = "Yes" Then
      CurRng.Offset(0, 7) = "Sim"
    ElseIf ComboBox7.Value = "No" Then
      CurRng.Offset(0, 7) = "Não"
    End If
    
    'Owner, Client, PHD'
    If CheckBox7 = True And CheckBox8 = False And CheckBox9 = False Then
        CurRng.Offset(0, 4) = "Cliente"
    ElseIf CheckBox7 = False And CheckBox8 = True And CheckBox9 = False Then
        CurRng.Offset(0, 4) = "Dono"
    ElseIf CheckBox7 = False And CheckBox8 = False And CheckBox9 = True Then
        CurRng.Offset(0, 4) = "PHD"
    End If
    
    
    
    End Sub
    Private Sub Calendar1_Change()
    
    End Sub
    Private Sub Calendar2_Change()
    
    End Sub
    
    Private Sub CheckBox1_Click()
    
    End Sub
    Private Sub CheckBox2_Click()
    
    End Sub
    Private Sub CheckBox3_Click()
    
    End Sub
    
    Private Sub TextBox1_Change()
    
    End Sub
    
    Private Sub TextBox2_Change()
    
    End Sub
    
    Private Sub TextBox3_Change()
    
    End Sub
    
    Private Sub ComboBox1_Change()
    
    End Sub

    Explanation: I dont want the code to go on the Workbook to my clients, i want to have it on my main Workbook and when i press Button1 it will just copy the fields from "Arrival Form.xls" Workbook to my "Main.xlsm" Workbook

    What happens after several trys its gives me object error and range error

    Any help would be welcome, thanks in advance
    Last edited by Rage; 12-21-2009 at 08:49 AM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Copy from other Workbook

    Hi Rage what happens if you save the xlsm as xls?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Registered User
    Join Date
    10-22-2009
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: Copy from other Workbook

    Well the file format doesnt have anything to do with it, i can use xls or xlsm and still works out the same.

    Want i want it to do is, with that code on my main it gets the objects that i refer but those objects are in a diferent workbook.


    Main(Workbook) = Code

    Copy from ->

    Arrival Form(Workbook) = Objects


    This way i can prevent users changing my code.

  4. #4
    Registered User
    Join Date
    10-22-2009
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: Copy from other Workbook

    Up up and away

  5. #5
    Registered User
    Join Date
    10-22-2009
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: Copy from other Workbook

    Please can i get some help over here ????

  6. #6
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Copy from other Workbook

    I'm a little lost as why the code is in the clients workbook
    Why dont you place the code in a module?
    can you please explain a little more?

  7. #7
    Registered User
    Join Date
    10-22-2009
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: Copy from other Workbook

    Hi Pike,

    Well since im new to VBA i started to build this like you saw it.
    And now as you may notice i want to change the way i started.

    I think my explanation of what i want may not be the best.

    I want the main workbook to reference the objects on the clientes workbook

    The coding i had is the one i posted and yeah it was working on the clients workbook

    I think this is the way to put it, the problem is that i dont know how to make the code refer to another workbook objects.

    All the help would be welcome

  8. #8
    Forum Contributor
    Join Date
    09-22-2009
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Office 2003 / Office 2007
    Posts
    114

    Re: Reference to External Workbook Objects

    This is not to difficult to do:

    Set ClientsBook = Workbooks.Open(path)
    This will open the book and set a reference to it. Then you can do things like:

    ClientsBook.Sheet1.Range("A1") = Stuff

  9. #9
    Registered User
    Join Date
    10-22-2009
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: Reference to External Workbook Objects

    But how do i use that reference as u say with a Range on this


    For example
    CurRng.Offset(0, 0) = TextBox1.Value 'House
    Can i use ?

    Set CurRng = ClientsBook.Sheet1.Range("A1000").End(xlUp).Offset(1, 0)
    Last edited by Rage; 12-21-2009 at 10:49 AM.

  10. #10
    Forum Contributor
    Join Date
    09-22-2009
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Office 2003 / Office 2007
    Posts
    114

    Re: Reference to External Workbook Objects

    Whenever you do something in VBA, by default it references whatever the active book / sheet is. So when you do something like this:

    Range("A1") = 7
    You are really doing this:

    ActiveWorkbook.ActiveSheet.Range("A1") = 7
    If instead you want to refer to a book that is not the active book, you can refer to it directly:

    Workbooks("MyTest.xls").Sheets("Sheet1").Range("A1") = 7
    This defines precisely which workbook and worksheet you would like to work with, and will work with any currently open workbook. This gets a bit cumbersome so you usually set a reference to that.

    Set MyShortcut = Workbooks("MyTest.xls").Sheets("Sheet1")
    Now you could use the following instead of typing all of that out:

    MyShortcut.Range("A1") = 7
    In this example, we have set MyShortcut to refer specifically to the sheet named "Sheet1" in the workbook MyTest.xls.

+ 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