Excel, VB form with Text boxes. On button click. Create new row in worksheet with data from text boxes - vba

Ive been looking around EVERYWHERE! And nothing works. ever.
I would like to be able to have a form that you fill in ( Lets say with two textboxes2 )
When you click the button it should then add a new row below all other populated rows and put the data of the text boxes into the cells of that row.
Nothing I do works :(
Does anyone have any ideas?
My form has:
Textbox1
Textbox2
Worksheet is called Sheet1
( Ive left everything default while testing )
Any help would be epic..
Once I have this bit sorted, i can then modify to do what i want to achieve in the long run. but this basic " Add a row with some data" is just killing me.. It cant be that hard right?
Thanks
G

I believe you could follow a logic similar to that : (After the click)
Find the last populated row
Insert an empty row under it
Grab the data from the textboxes and copy them to their proper column
Does that help? If not, what part are you having trouble? I'll be happy to provide more help.

Something like this could get you started. This assumes your first text box will fill in column A and second text box fills in column B.
Sub InsertText()
Range("A65000").End(xlUp).Offset(1, 0).Select
ActiveCell.Value = TextBox1.Value
ActiveCell.Offset(0, 1).Value = TextBox2.Value
End Sub
I don't think you need to add another row if you're just filling in below the last populated cell. If so
ActiveCell.EntireRow.Insert

Related

VBA Sort with Spinners

I am working on an excel spreadsheet that contains several rows worth of data that I need to sort based on the values in Column A. Column B contains values that are controlled by Spinners also located in Column B.
What I am trying to do is create buttons that when pressed will sort all of the rows based on Column A but without breaking the spinners.
What I have tried so far:
When I just use a simple sort function, it moves the rows as required along with the spinners but it doesn't update the Cell Links on the spinners so they remain linked to the cells in the old questions.
I tried adding in a line in the VBA for the spinners that would update the spinners Cell Link when the spinner was pressed. The problem here is that excel would first run the increment portion of the spinner before updating the link, resulting in it incrementing/decrementing the old cell before updating the link.
I tried adding in a line in the VBA for the SORT Macros that would run a For loop to update all of the spinner Cell Links based on their new topleftcell value. This works for the Cell Link but it also updates the Cell Value based on what seems to be the Cell Value of the last spinner moved. Needless to say this is also a problem.
I'm not sure what else to try as I am still quite inexperienced with VBA.
If anyone has any suggestions for sorting cells that contain spinners without breaking the spinner's values or links I would be very appreciative!
Thanks in advance and please let me know if further information is required. I can include snippets of the code I've used so far but I wasn't sure if it owuld help much.
Try something like this.
Sub SORT()
Range("A2:B4").SORT. Key1:=Range("A2"), Order:=xlAscending
Call SpinnerControl
End Sub
Sub SpinnerControl()
Dim spn As Spinner
Dim rng As Range
For Each spn In ActiveSheet.Spinners
Set rng = spn.TopLeftCell
spn.LinkedCell = rng.Address(external:=False)
Next spn
End Sub

Pasting same cell on different rows as value is replaced

This sort of follows up from my previous few questions on the same workbook.
I have two sheets, the first being Car Search, which contains a form (NOT a VBA form, just a normal table that appears like a form) to fill in. The second sheet is Raw Data, and will contain all the information entered in the Car Search sheet. It will be displayed row by row (see 2nd image).
In the Raw Data sheet, I am using the formula =""&'Car Search'!B3 to copy the contents of cell B3 in the Car Search spreadsheet.
My question is: If I had a new Car ID value, how can that automatically be entered into the row below?
Essentially, I am trying to use the form to capture all data for new cars coming in, and then I would like all that data to appear in the second sheet in their respective rows/columns.
Any help much appreciated! :)
EDIT:
Good news!
You need to use VBA:
Sub Range_Copy_Examples()
Worksheets("Car Search").Range("B3").Copy Worksheets("Raw Data").Range("=OFFSET(Sheet3!B1,0,0,COUNTA(B1:B300)+1,1)")
End Sub
However there is a small bug where it keeps pasting across past values, so I have suggested an alternative macro below
Re-edit:
A more mechanical way to make macro work (hopefully someone can improve on it) looks like this:
Sub Macro1()
Sheets("Car Search").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Raw Data").Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Worksheets("Car Search").Activate
End Sub
This works as follows:
in Car Search, have cell B3 selected the whole time
in Raw Data, select the cell with the last value in the column where you want you Car Search!B3 data pasted (you only have to do this once)
press Run on the macro (easy enough to just record one yourself)
go back to Car Search and change the value of B3, press Ctrl-Enter to keep the same cell selected after changing value, then press the same macro button without changing a thing.
If someone could add so that when pressing Ctrl-Enter on Car Search!B3 the Raw Data gets automatically added without having to manually run the macro, it would be fully automated!

How to move one column to the end in Pivot table with VBA?

I create a pirvot table, but the colunm order is not what I wanted. I want to move one column to right end. I can do it in Excel, but don't know how to do it with VBA.
In Excel, can do in this way--active the column header which you want to move, click right-hand button, choose Move, Move "--" to end.
Record Macro--when record Macro, it just shows the exact position, just like,
ActiveSheet.PivotTables("PivotTable16").PivotFields("wk").PivotItems("#VALUE!") _
.Position = 12
But I want to use the Macro in other files, maybe the end position is not 12. Then the Macro can't be used.
Hope can get help here. How to do it to move the colunm to end by VBA? Thanks.
I hope I get you right.
You can get the end position by
ActiveSheet.PivotTables("PivotTable1").PivotFields("whatever").PivotItems.count
Basically it returns the number of items for a label
Edited
So you could do like
Dim total as Integer
total = ActiveSheet.PivotTables("PivotTable1").PivotFields("whatever").PivotItems.count
ActiveSheet.PivotTables("PivotTable1").PivotFields("whatever").PivotItems("whatever").position = total
Just a random note to anyone that comes across this and runs into the same issue I did. If in your macro you're hiding fields, it's going to give you an error. Don't use this code as the last thing you do when setting up your pivot table. Use this code, let it move to the bottom position, THEN have your code to hide your fields. If you hide fields first, it'll count the hidden fields then try to move it outside your pivot table and cause an error.

When the value in a cell changes change the filter on a pivot table

I have been reading around for a bit and reached the ask SO point. I have a drop down selector in excel that lets me change names and when the name is changed, the value in "C3" is changed (c3 references another cell on another sheet if that matters) and when the value in "C3" changes I want a pivot table on another sheet (sheet6 for now) to update its filter on territory id to reflect the new value of "C3".
It sounds like an event to me, so I went digging around and found out VBA has events (yay!), but I have been unable to get my event to actually do anything. The code I have there works when I run it as a macro, but I would really like it to automatically run everytime the value in cell "C3" changes.
Here is what I am trying so far:
Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Sheets("Current Status").Range("C3")) Is Nothing Then
Sheets("Sheet6").PivotTables("PivotTable5").PivotFields("territory_id"). _
ClearAllFilters
Sheets("Sheet6").PivotTables("PivotTable5").PivotFields("territory_id").CurrentPage _
= Sheets("Current Status").Range("C3").Value
End If
End Sub
Update: I have found that if I put the above code in the sheet section (sheet 2) instead of in a new module I can get it to run if I physically enter the code and then hit enter. Now I am wondering if there is a way to make it do it without me having to manually enter the value and hit enter. I just want to be able to use my drop down menu to select a new name and when the value in c3 changes due to the drop down selector update the pivot table.
Thank you as always SO.
The problem is that C3 is not actually changing, because it's just formula reference that is updating. Is the "drop down selector" on a form or based on data validation and in a cell?
If it's based on a cell, set your target to be the target cell, not C3 -> because C3 is just a
formula reference, and your drop down cell is the one actually changing.
If it's based on a form, but code in the on_change event of the form control.

Excel macro for automatic data entry

I need a macro to help me with data entry in Excel. Basically I want sheet 1 for data entry, and sheet 2 for the data that are entered. Sheet 1 will only have one row for data entry, and once that row is filled you hit enter. The row is automatically added to the table in sheet 2, and the row is cleared on sheet 1. Now you are ready to enter another entry in sheet 2.
So to summarize sheet 2 will have multiple rows for the data entered, and sheet 1 will only have 1 row because it automatically clears it's row after each entry.
Is something like this possible?? If you guys can post some code for me it would really help, and keep in mind I have never programmed in VBA before. Thanks in advance!!
Place a button on your sheet 1, then put this macro into a regular code module and attach it to your button. Assuming you are entering values across row2 (row1 being titles or such), then this would transfer row2 data to the next empty row on sheet2:
Sub Transfer()
Rows(2).Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
Rows(2).ClearContents
End Sub
I'm not totally sure of the use-case, but have you considered using Excel UserForms instead.
The input would be a form that actually appears in front of the user which would collect input. When they hit the enter button, you could have underlying code to update the main sheet, Sheet 2.
http://www.excel-vba-easy.com/vba-userform-excel-vba.html
You could also use the database form. set up your database sheet then Data > Form (in 2003)
In order not to have to "click" on a button, I would suggest to look into this function:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
This macro will run every time something in your sheet has changed.
It might be that you have to check if there is data in cell A1, as this VBA might be triggered both after hitting enter and after the data has been transported to the other sheet.