Solved

userform excel 2007, use ADD to update worksheets

Posted on 2014-12-18
17
160 Views
Last Modified: 2014-12-18
I need the correct code to update a worksheet using  cmdADD
0
Comment
Question by:bjfulkerson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
17 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40507563
Sheets("The sheet name").Range("A1") = "blah"
0
 

Author Comment

by:bjfulkerson
ID: 40507574
Can you tell me what is wrong with this.  It won't update
Private Sub cmdAdd_Click()
   Dim lastrow As Long
   lastrow = Sheets("WorkSheets").Range("A2" & Rows.Count).End
   Cells(lastrow + 1, "A").Value = ComboBox1.Text
   Cells(lastrow + 1, "B").Value = TxtDate.Text
   Cells(lastrow + 1, "C").Value = CB2FunctionCode.Text
   Cells(lastrow + 1, "D").Value = TxtTime.Text
   Cells(lastrow + 1, "E").Value = TxtUnits.Text
   Cells(lastrow + 1, "G").Value = TxtFname.Text
   Cells(lastrow + 1, "H").Value = TxtLName.Text
   Cells(lastrow + 1, "I").Value = TxtHDept.Text
 
 End Sub
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40507586
To determine the last available row, replace
lastrow = Sheets("WorkSheets").Range("A2" & Rows.Count).End

Open in new window

with
lastrow = Sheets("WorkSheets").Cells.SpecialCells(xlLastCell).Row

Open in new window


or, if you know the data in column A is contiguous (no empty cells), then you can use:
lastrow = Sheets("WorkSheets").Range("A2").End(xlDown).Row

Open in new window


I presume you have already defined and assigned the values in the objects listed later.

Regards,
-Glenn
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bjfulkerson
ID: 40507596
Not sure what you mean by that.

It still is not updating
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40507669
Instead of "WorkSheets" use the name of a single worksheet like "Sheet1".
0
 

Author Comment

by:bjfulkerson
ID: 40507688
Still no update

Private Sub cmdAdd_Click()
   Dim lastrow As Long
   llastrow = Sheets("Sheet1").Range("A2").End(xlDown).Row
   Cells(lastrow + 1, "A").Value = ComboBox1.Text
   Cells(lastrow + 1, "B").Value = TxtDate.Text
   Cells(lastrow + 1, "C").Value = CB2FunctionCode.Text
   Cells(lastrow + 1, "D").Value = TxtTime.Text
   Cells(lastrow + 1, "E").Value = TxtUnits.Text
   Cells(lastrow + 1, "G").Value = TxtFname.Text
   Cells(lastrow + 1, "H").Value = TxtLName.Text
   Cells(lastrow + 1, "I").Value = TxtHDept.Text
 
 End Sub
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40507703
You have a typo in your code.

llastrow = Sheets("Sheet1").Range("A2").End(xlDown).Row

should be

 lastrow = Sheets("Sheet1").Range("A2").End(xlDown).Row

If that doesn't work then please attach your workbook.
0
 

Author Comment

by:bjfulkerson
ID: 40507709
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40507721
The workbook you provided has no code and no command button that I can find. Please save it as an xlsm file.
0
 

Author Comment

by:bjfulkerson
ID: 40507729
Sorry I sent the wrong one.
STL-Function-Analysis-2015-V2.xlsm
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40507768
Your button on the userform is named "ccmdAdd", it should be "cmdAdd".
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40507774
You have some other problems. Be back in a minute.
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40507795
After correcting the name of the button as I posted in post ID 40507768, use this code.
Private Sub cmdAdd_Click()
   Dim lastrow As Long
   
   With Sheets("Timesheet")
     lastrow = .Range("A1048576").End(xlUp).Row
    'lastrow = Sheets("Timesheet").Range("A2").End(xlDown).Row
    .Cells(lastrow + 1, "A").Value = ComboBox1.Text
    .Cells(lastrow + 1, "B").Value = TxtDate.Text
    .Cells(lastrow + 1, "C").Value = CB2FunctionCode.Text
    .Cells(lastrow + 1, "D").Value = TxtTime.Text
    .Cells(lastrow + 1, "E").Value = TxtUnits.Text
    .Cells(lastrow + 1, "G").Value = TxtFname.Text
    .Cells(lastrow + 1, "H").Value = TxtLName.Text
    .Cells(lastrow + 1, "I").Value = TxtHDept.Text
   End With
  
 End Sub

Open in new window

0
 

Author Comment

by:bjfulkerson
ID: 40507878
Just an FYI.  In my version of Excel 2007, there must be a bug.
the cmdAdd is actually ccmdAdd.
After I changed that it worked.

Do I use UPDATE the same way

cmdUPDATE
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40507905
In my version of Excel 2007, there must be a bug.
the cmdAdd is actually ccmdAdd.
The only way the name of the control became ccmdAdd is if you or someone else typed it that way.

Do I use UPDATE the same way
That's really too broad of a statement to answer exactly, but probably, yes.
0
 

Author Comment

by:bjfulkerson
ID: 40507916
I will ask a new question.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40508106
I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question