Solved

userform excel 2007, use ADD to update worksheets

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

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 45

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 45

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 45

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 45

Expert Comment

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

Expert Comment

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

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 45

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 45

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

757 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

18 Experts available now in Live!

Get 1:1 Help Now