+ Reply to Thread
Results 1 to 9 of 9

Link VBA Code to Button to transfer data (Run time error)

  1. #1
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    127

    Link VBA Code to Button to transfer data (Run time error)

    Hi

    I hope someone can help as I am new to VBA.

    I need help to link VBA code to a button that will transfer data from a specific row in the "DB" sheet back to the "Input" sheet when you click the "Loan Input" button.

    I have the following VBA code in "module3" but I get an error when I link it to the button (I have also attached the workbook) :

    Sub RoundedRectangle1_Click()
    Application.ScreenUpdating = False
    If Target.Column = 1 Then
    Sheets("Input Sheet1").Cells(4, 3) = Sheets("DataBase").Cells(Target.Row, 2)
    Sheets("Input Sheet1").Cells(6, 3) = Sheets("DataBase").Cells(Target.Row, 3)
    Sheets("Input Sheet1").Cells(10, 3) = Sheets("DataBase").Cells(Target.Row, 5)
    Sheets("Input Sheet1").Cells(12, 3) = Sheets("DataBase").Cells(Target.Row, 6)
    Sheets("DataBase").Rows(Target.Row & ":" & Target.Row).Delete
    'Application.Calculate
    Sheets("Input").Activate
    End If
    End Sub

    When I do link it to the "Loan Input" button I get an Run time error '424': object required

    Thank you in anticipation
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    11-12-2014
    Location
    Nottingham
    MS-Off Ver
    2013
    Posts
    383

    Re: Link VBA Code to Button to transfer data (Run time error)

    If you want to use "Target" as a variable then you need to define it. The best way to do this is by putting into into a sheets VBA and using the Worksheet_SelectionChange() Sub.

    If not then you need to use Activecell instead.

  3. #3
    Forum Contributor LokeshKumar's Avatar
    Join Date
    03-31-2015
    Location
    India
    MS-Off Ver
    All, mostly 2010 now..
    Posts
    471

    Re: Link VBA Code to Button to transfer data (Run time error)

    Hey
    You can try the following code: this would be much easier to understand....

    Please Login or Register  to view this content.
    I am attaching sheet for your reference

    Database Example (module3).xlsm

    If this resolved your issue pls mark the thread as "Solved" and may reputation as well.
    Lokesh Kumar
    Stay Hungry.. Stay Foolish..
    _________________________________________________________
    Please Click STAR to Add Reputation if my/someone's answer helped!

  4. #4
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    127

    Re: Link VBA Code to Button to transfer data (Run time error)

    Hi ARowbot

    Thanks for the reply, I trust the following code what you are talking about?

    If so, how do I run this code by linking it to a button?

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 1 Then
    Sheets("Input").Cells(4, 3) = Sheets("DB").Cells(Target.Row, 2)
    Sheets("Input").Cells(6, 3) = Sheets("DB").Cells(Target.Row, 3)
    Sheets("Input").Cells(10, 3) = Sheets("DB").Cells(Target.Row, 5)
    Sheets("Input").Cells(12, 3) = Sheets("DB").Cells(Target.Row, 6)
    Sheets("DB").Rows(Target.Row & ":" & Target.Row).Delete
    Application.Calculate
    Sheets("Input").Activate
    End If

    End Sub

  5. #5
    Valued Forum Contributor
    Join Date
    11-12-2014
    Location
    Nottingham
    MS-Off Ver
    2013
    Posts
    383

    Re: Link VBA Code to Button to transfer data (Run time error)

    That particular code will not be linked to a button. It will run whenever you change a value in the worksheet in column 1 (A). If you want to link it to the button use:

    Please Login or Register  to view this content.
    To assign this to the button, right click the button and click Assign Macro.

    Also, In future make sure your code on the forum is put into code tags like above

  6. #6
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    127

    Re: Link VBA Code to Button to transfer data (Run time error)

    Hi LokeshKumar,

    Thanks for the quick reply, however your code moves the data from the input sheet to the DB sheet but I need it to move in the reverse (the line selected in the DB sheet table must repopulate the corresponding cells in the Input sheet) when you click the "Load Data" button

  7. #7
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    127

    Re: Link VBA Code to Button to transfer data (Run time error)

    I created a module and inserted your code into it and then linked the "load Data" button in the "BD" sheet to the code.

    On the "DB" I selected a cell in column A of the table to select the row data that needs to transferred to the input sheet, but when I click on the button to transfer the data I now get a message:
    Run-time error '9': Subscript out of range. The error occurs on line "Sheets("Input Sheet1").Cells(4, 3) = Sheets("DataBase").Cells(ActiveCell.Row, 2)"

    PS. How do I get the code into code tags like you suggested I just started using the forum a little while back

  8. #8
    Valued Forum Contributor
    Join Date
    11-12-2014
    Location
    Nottingham
    MS-Off Ver
    2013
    Posts
    383

    Re: Link VBA Code to Button to transfer data (Run time error)

    I noticed when using your spreadsheet that your sheetnames were inconsistent, make sure the sheets are called Input Sheet1 and Database exactly or adjust your code accordingly. You can put them into code tags by pressing the button that looks like #.

  9. #9
    Forum Contributor
    Join Date
    07-13-2014
    Location
    Three Rivers
    MS-Off Ver
    365
    Posts
    127

    Re: Link VBA Code to Button to transfer data (Run time error)

    I'm so sorry, My bad! I used the code from another workbook I was trying to figure this problem out and forgot to that I changed the sheet names in this workbook.

    The code now works like a charm!

    Thank you so much for the assistance!

+ 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. Link a print and Save As Code to one button?
    By countryred in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-03-2014, 02:29 AM
  2. [SOLVED] Run time error in code that extracts data from several sheets into master sheet
    By Hello23 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-23-2012, 10:11 PM
  3. Auto. Transfer error code to next sheet (Excel 2007)
    By Excelnoub in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-26-2012, 07:37 AM
  4. First timer command button link - compile error expected end sub
    By MONSIRET in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2012, 08:34 AM
  5. How does this code link to the main report (at the press of a button)
    By slay0r in forum Access Tables & Databases
    Replies: 1
    Last Post: 03-28-2012, 11:45 AM

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