Refering to Table cell in Macro - vba

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

Related

Saving cell Presets in a dropdown list

At the moment I have created a spreadsheet which takes a bunch of inputs, runs them through a list of formulas, and then spits out the results onto a "report" worksheet.
I've been manually saving each of these reports as separate CSVs but I was hoping for a better method moving forward as it is getting quite tiring to have to open 10 CSVs when i do my monthly reports.
I am looking for a way to start saving all of these reports into a "database". My hope to to have one cell be for an user entry name and for two buttons. One to save the current report under the name entered by the user, and two to remove old records. I would then be able to revisit old entries by selecting them in the dropdown.
I've dabbled with VBA and Macros in the past but this is a little more complicated than what I've dealt with in the past. Looking for some help/direction.
Thanks for your time!
Depending on how your reports need to be used, you might find it satisfactory to simply make your data into one big Excel Table ( Insert Tab > Table ). When you do this, Excel will automatically fill-down any formulas that you enter in a column, and also show the formula using the headers instead of A1-style references.
I use this format, adding Y under Remove from Active List on each line that is already done. Then whenever I save the file or look at it for today's status, I filter out what's old and just look at the new. The other filters enable copy-pasting or printing whatever arrangement I like.
The filters and other things in the table can be referenced in VBA as Sheets("ThisSheet").ListObjects(1), which is an object with a number of useful properties and methods.
For VBA information, read more here: https://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables
This is my code for auto-filtering the table to hide inactive items at time of save. You add it at ThisWorkbook in the VBA editor:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet1").Activate
SelectedCell = ActiveCell.Address 'this saves your screen selection for after the filtering
ActiveSheet.ListObjects(1).Range(1, 1).Select
If ActiveSheet.ListObjects(1).AutoFilter.FilterMode = True Then
ActiveSheet.ListObjects.Item(1).AutoFilter.ShowAllData
End If
A = ActiveSheet.ListObjects(1).Range.Rows(1).Find("Remove from List").Column - _
ActiveSheet.ListObjects(1).Range.Column + 1
ActiveSheet.ListObjects(1).Range.AutoFilter field:=A, Criteria1:="="
Range(SelectedCell).Select
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!

Copy data from one part of a userform to another automatically

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?

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