1

Can someone please help me?

Basically, I have three textboxes in my Excel spreadsheet, and I have a button that I want to save the details into a table on another sheet.

Each row as an 'x' in it, so that the program can know where to save the next data.

When I click the save button, an error comes up saying 'Object required.'

I'm god awful at VBA, seriously I have no clue, so maybe try to keep the answer in stupid-man terms so I don't mess up? I'm probably doing something minor wrong but I'm not sure. Here's my code:

Public Function findrow(texttofind As String) As Integer

Dim i As Integer
Dim firstTime As Integer
Dim bNotFound As Boolean

i = 5
bNotFound = True
Do While bNotFound
  If Cells(i, 1).Value = texttofind Then
   firstTime = i
   bNotFound = False
  End If
  i = i + 1
Loop
findrow = firstTime
End Function

Sub SaveCustomer()
 Dim rowno As Integer
 Worksheets("Customers").Select
 rowno = findrow("x")
 Range("C" & rowno).Value = Invoice.TextBox21.Text
 Range("D" & rowno).Value = Invoice.TextBox22.Text
 Range("E" & rowno).Value = Invoice.TextBox23.Text
 Worksheets("Invoice").Select
End Sub

I'm really bad at VBA, and my teacher isn't great, I just need to fix this one thing so I can pass my assignment. I just want to save the information from 3 textboxes into a table in another sheet. Thanks for any help received!

I'm sorry if this is a duplicate, but because I'm so bad at VBA, answers to other questions don't really help me at all.

Community
  • 1
  • 1
  • What is the Invoice object? Where does the error occur? – Marek Stejskal Feb 01 '15 at 22:05
  • I have 3 text boxes. I type in random characters in the boxes, I click a save button (Which has a macro assigned which is the 'SaveCustomer' code and after I click the button, an error comes up saying 'Object Required.' and it shows the sheet with the table on. (The table I want the textbox stuff to save to) but nothing is highlighted. – YouCan'tSeeMe Feb 01 '15 at 22:13
  • Invoice is the name of my sheet with the textboxes on it – YouCan'tSeeMe Feb 01 '15 at 22:14
  • Try replacing Invoice with Worksheets("Invoice"). If en error comes up, click debug and check what line causes the error – Marek Stejskal Feb 01 '15 at 22:17
  • Wow it worked. I can't believe it. I literally signed up for an account just to fix this, and all the problem was, was that. Listen man, I really appreciate that. You've just helped a kid somewhere in the world pass his assignment and get a good grade. Thank you! – YouCan'tSeeMe Feb 01 '15 at 22:24
  • How can I upvote you? or give you reputation? – YouCan'tSeeMe Feb 01 '15 at 22:26
  • Glad to have helped! To give me rep accept my answer below as solution. – Marek Stejskal Feb 01 '15 at 22:28
  • As a note, 'selecting' worksheets isn't generally good practice. [Here is a good question/answer](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) on this subject. – tospig Feb 01 '15 at 22:33
  • This is my first ever use of VBA code, and I'll most likely never use it again. However, I do code in other languages, and I'll definitely take a look at that because the chances are, it'll help me. Thank you! – YouCan'tSeeMe Feb 01 '15 at 22:46
  • 1
    Also, in your `Do While` loop, if the `texttofind` is never found, you will end up in an infinite loop. You may need another method for exiting, or use a `for` loop that iterates over the rows in the column. – tospig Feb 01 '15 at 22:56

1 Answers1

2

Replace

 Range("C" & rowno).Value = Invoice.TextBox21.Text
 Range("D" & rowno).Value = Invoice.TextBox22.Text
 Range("E" & rowno).Value = Invoice.TextBox23.Text

with

 Range("C" & rowno).Value = Worksheets("Invoice").TextBox21.Text
 Range("D" & rowno).Value = Worksheets("Invoice").TextBox22.Text
 Range("E" & rowno).Value = Worksheets("Invoice").TextBox23.Text
Marek Stejskal
  • 2,698
  • 1
  • 20
  • 30