Solved

userform excel 2007, use ADD to update worksheets

Posted on 2014-12-18
17
158 Views
Last Modified: 2014-12-18
I need the correct code to update a worksheet using  cmdADD
0
Comment
Question by:bjfulkerson
  • 9
  • 7
17 Comments
 
LVL 46

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

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

It still is not updating
0
 
LVL 46

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 46

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 46

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 46

Expert Comment

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

Expert Comment

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

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 46

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 46

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

856 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