Solved

userform excel 2007, use ADD to update worksheets

Posted on 2014-12-18
17
156 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now