Copy data from one part of a userform to another automatically - vba

I am trying to work with an Excel form using VBA that I have customized (I found the original on the web).
I have two tabs: one is called Planning-Deleted, the other is called Planning-Deleted Data. All the data that I enter in the first tab is copied to the second tab when I press a command button called Add to Database.
Is it possible to skip the command button altogether and record the data entered in cell D5 and D6 directly in the second tab and clear these cells for the next records? I have no knowledge of programming and I would appreciate as much clarity and specificity as possible.
If more details are needed, please let me know.

You'll need some trigger to cause the copy operation to happen, for example:
Using the 'Change' event, you can trigger some action any time some value is entered into a sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
Sheet2.Range(Target.Address).Value = Target.Value
End Sub
This code will copy any data entered into the first sheet into the same cell on the second sheet. 'Target' in this case is the cell where data was entered.
I'm not sure if this what you want though, since you also want to clear the cells after the copy. Can you describe when the copy-and-erase should occur, if not from clicking a button?
Also, your question doesn't seem to match your question's title. Can you clarify a bit what you are trying to accomplish?

Related

Refering to Table cell in Macro

So, I am relatively new to VBA and Programming as a whole,
I know in Excel Formulae, Rather than refer directly to a Cell ($B$27) you can refer to the Column name instead, (#[Condition]).
Now I am aware that this only works on "linear" tables, the formulae will only work on and affect the row its in, but for the table i am working on this is fine.
My question is, is there a way to refer to this inside VBA?
I am trying to create a function that takes the value of a "Condition" Cell and if this is "Other" it will open a custom User form with 3 buttons that allow the user to select the reason why the Condition is "other"
Depending on the button click, the Value of another Cell in the same table (Works Completed) will be changed to what ever the reason they click.
(E.G, user clicks "To be Repaired", the value of "Works Complete" will be changed to "For Repairs"
This currently only works when referred to a Cell directly ($B$27)
Whenever you have a question, concerning the translation of a manual action in Excel to VBA the best answer is:
Start the macro recorder, do the manual action and check yourself.
This is a sample of a recorded macro, selecting Table1, Column3:
Sub Macro1()
Range("Table1[[#Headers],[Column3]]").Select
ActiveCell.FormulaR1C1 = "Testing"
Range("C2").Select
End Sub

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

Is it possible to have a cell that has a formula and accepts entry at same time in excel?

Example:
A B
1 =vlookup(XX)
2
3
in cell A1 there is a Vlookup formula, Is it possible to enable user entry in this cell and override the formula then later restore the formula automatically when sheet is open again?
Even through VBA
Short, boring answer: nope.
A cell only ever has a keyed-in value, or a calculated formula. Can't have both.
Longer answer: maybe.
Shift everything 1 row down, and use row 1 to store your "original" formula - then hide that row (and pray the user isn't going to mess with it).
When the sheet is opened again sounds like you're confusing "workbook" and "worksheet" - you need to handle Workbook_Open if you want to run code when a workbook opens. Workbooks contain worksheets - it's the workbook that opens, not the sheets (sheets activate, but I doubt you would want to put that logic in there).
So, in the handler for Workbook_Open, write code that takes the formula in the hidden row and overwrites whatever is under it.
Another solution can be to hard-code the formula in the VBA code.
One possibility would be to store your Workbook as a template. Normally when a user opens the workbook by double-clicking, it will open whole new workbook based on the template, and they can modify it to their heart's content, save it, mail it to Grandma, etc.
The next person who comes along will double-click the template file and get the formula again, just as you designed it.
Short answer: Kind of, sort of
Long answer:
Save your workbook as a template. Every time someone will use it you'll see the orignal with formula, then if someone write over the formula, when using save your original will be kept intact.
What You need to do is:
press Alt + F11
select ThisWorkbook and paste this code:
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("A11").Value = "asdf"
End Sub
Every time the workbook is opened, this script will run.
Instead of "Sheet1" you can write the name of the sheet you want to apply the script.
Inside the Range, You can define the cells you want to modify, You can use even multiple cells. Check this for more information about this.
After Value You can write what You want to be written inside the cell. You can write "=vlookup(XX)" and it will work.

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.

Show Cell Range on UserForm; then update

I've been using a crude method to help the user update some cells - by having them in a sheet. Is there any way I can display the various ranges in a userform, one by one, then have the user update them, click a button and move onto the next one?
Essentially, can I have Excel automatically generate an input form based on a range? The process of updating and saving back to the sheet I can do; it's the production of the correct form that I can't.
It's possible to do this, but the only way I can think of is to make a userform that automatically populates itself based on a range passed in. This way you could have different macros in Excel that call the form to populate based on different ranges. I built a proof of concept Excel file for trying this, and it seems to work, the only issue I can think of being that you need to figure out a way to tell the user what input field is what.
I think what needs to be done is to add controls programmatically to a userform (I name the textboxes as the cell address it's going to populate) then when the form is closed loop through all the textboxes and populate the cells with the textbox values.
You can see what I did at:
https://my.syncplicity.com/share/uicgbs3rl0/InputForm.xls
I think all that would need to be done is for you to work out how to add labels for the textboxes, and make sure the form is resized based on the controls you add...
I am not quite shure what you are looking for, but you could insert a second sheet and use it as a "form". An other way could be a dialog box with an input field.
Either way, you present the cells you want the user to change one by one, using a vba-function. You implement a "previous field" and a "next field" button, so the user can step through the range of cells. If the user hits "next field", you save his input and take the next cell from a previous defined range of cells.
You could have a "config field" in which you define the range of cells you want to change.
This is pretty rough and old-fashioned but if you have the data in standard list format - i.e. column headers in the first row of your range and then one record of data in each row below - then selecting a cell within the range and going Data > Form will give you a crude input form with roughly the functionality you need.
You can also do this in VBA by calling the ShowDataForm method of the appropriate worksheet. Just select a cell within whichever range you need first. The macro will remain paused until the user closes the data form