I do have a document with some formulas retrieving data from other tables. Some of the users were overwriting these formulas with more accurate data. I would like to delete now all the data retrieved from the formulas (so basically I would like to delete the formulas) and only keep the manual entered data.
How could this be done? I just know the other way around :)
Select the whole range, Home / Find and Select / Go To Special, check Formulas, OK, hit Del button on keyboard.
If you need the VBA code, this is it:
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
Of course you can omit the Select thing, and do whatever you want with it, e.g. clear it:
Selection.SpecialCells(xlCellTypeFormulas, 23).Clear
Or on the whole worksheet:
Cells.SpecialCells(xlCellTypeFormulas, 23).Clear
Related
I want to insert the content of a cell or a row of cells into a macro to avoid having to update the macro code.
This is so that people at my office can simply update a list in a spreadsheet instead of having to go change the VBA code.
Is it possible to simply ask Excel to insert text from a cell into VBA and use it to execute the macro?
Thank you!
I'm not positive I understand your question, however to read data from a cell you would use
Worksheet(SheetIndex).Cells(x,y).Value
If you want to loop through rows to get the data doing a for loop would be the easiest way around it.
For i to j
'Do Stuff
Next
If you want to loop through all rows in a sheet find the total number of rows by getting number of used cells.
Worksheet(SheetIndex).UsedRange.Rows.Count
Honestly, I would probably have a button to actually trigger the start of this to increase usability. Code should always be written with scalability in mind so why would they need to change the macro code?
I want that when the table is created there will be a fully functioning excel formula. If I try with "... each "=[#[Prog.]]" there will insert only the text not the formula.
P.S.: I'm new to this so, excuse me if this is basic.
From what I understand, you can create Excel formulas in Power Query and pass those to your Excel worksheet but the worksheet won't automatically recalculate. This earlier post addressed the same thing.
I tried this out by creating a table with two columns: "Column1" and "Column2", with the numbers 1 & 2 in each, respectively. Then, I loaded the table into Power Query and created a new column, "Custom", with the formula, "=" & Text.From([Column1]) & "+" & Text.From([Column2]). (To be clear, the = here is not the = that is already populated when the new column dialog box pops up, it's additional.) Anyhow, I got this table from that formula:
Then, when I clicked "Close and Load," my Excel worksheet was loaded with this:
Notice it looks just like text, but in the formula bar, it looks like this:
Notice there isn't a ' before the =, so it's a formula. If it were just text, it would be '=1+2 instead of =1+2.
Since the cell doesn't automatically recalculate, I had to click the cell I wanted to update, and then click in the formula bar and then press enter. That gave me this:
I tried to use F9 to manually recalculate, but it did not work.
Also... Every time I refreshed my query, my worksheet was set back to just the text-looking formula (i.e., =1+2) and I had to re-recalculate manually. That would be a real pain to have to do that for a lot of cells each time you refresh your query.
#Umut K posted a VBA-based workaround to trigger refresh all in the earlier post, but I think what you are asking to do might be more trouble than you're looking for.
The above answers are correct that the formulas do not automatically recalculate, even though they are in the cell correctly. A workaround/trick that I found was to use -Find/Replace to trigger a recalculate on every cell in one step. Go to one of the cells with the formula; to copy the entire formula. to open the Find/Replace dialog. in both the Find What and Replace With boxes in the dialog. Click on Replace All. All the cells except the one you are in will recalculate. Then, exit the cell you are in, and it will also recalculate.
It seems stupid, but it works.
The way I have been using excel to transform data calculated via formulas into static values was to select each cell, press F2 to edit it and then press F9to replace the content of that cell with the result of the formula in it.
I was wondering if there is a convenient way to do this process to a group of selected cells. It would come in handy if I wanted to do this to hundreds of cells at a time.
Could anybody please tell me if that is possible? And if so, how?
If a VBA based solution is viable, then this short routine should suffice.
sub Values_Only()
with selection
.value = .value
end with
end sub
Just select a group of cells and tap Alt+F8 then Run the macro. Optionally, use Options in the Macros dialog to set a hot key combination.
You may also find the Quick Access Toolbar (aka QAT) of use to assign the Paste Values command to a hotkey. Mine is set to Alt+2. With any group of cells selected, Ctrl+C then Alt+2 is sufficient to revert formulas to their returned values. See this for more information.
Highlight all of the desired cells. Copy them. Then right click the highlighted area and select the 123 paste option.
I am working on the following sheet, called Raw_Data:
In a new sheet, I want to copy the registration_date column by reference. This means that if I change the registration_date on the Raw_Data, the changed value should be reflected in the new sheet.
To implement this, I have entered the following =Raw_Data!C2. So far it works fine as you can see below:
But the problem is that when I double click on the little green square here, it doesn't automatically populate the entire column.
I don't want to manually drag-and-drop because there are several thousand rows. Does anyone know how I could automatically populate the column by reference?
While there are some automation things you could do, I think a lot of that would be overkill for what could be just an input issue with how you are choosing to copy in Excel.
If you find that you need to copy a large block of data, rather than dragging the corner of your cell like that, try one of these alternate methods:
While selected on the cell, press CNTRL + C. Then in the 'Name Box' (where it shows the address of the cell you are on), type in the cell where you want to go (A17000); then press SHIFT + ENTER. This will jump you to that cell, and will highlight all cells inbetween where you were and where you are going. Then press CNTRL + V.
Another method of moving around a large data block in Excel is to hold CNTRL and press an arrow key. This will move you as far down the data block as possible. Note that this will not work on a blank sheet, as there is no data and therefore Excel doesn't know when to stop.
Again - some automation would be possible here, but moving around an Excel worksheet is something you will be doing too frequently in too many different ways to want to automate what might be 5 keystrokes once a week.
In excel the "double click to fill" feature is a heuristic based feature that fills the cells that appear to be relevant with the selected formula (+ reference corrections).
In your case there is no hint the heuristic can use to tell what to fill so nothing is being done.
Regarding dragging "several thousand rows", that's not a real problem. If you only do it once, there no reason to even trying anything "smart" or complicated.
You can also copy the source cell, select all the cells you want to fill and then paste. You can select cells in any way you like, not just dragging.
You could instead of all the dragging/copying/filling approach simply copy directly from the source, and paste by reference.
As a last resort, you can always go for a VBA solution to do that for you.
If I manually enter a formula into a column in an Excel table (i.e. ListObject), AutoCorrect applies this formula to the whole column.
Is there any way to control the this behavior via VBA, i.e. can I somehow modify/delete/add this formula?
I know I can simply change the formula of the ListObject.ListColumns(1).DataBodyRange object - but this will overwrite any manually values entered before - while changing the formula in the UI will leave this untouched...
Thanks to Doug's and bonCodigos comments/answers, I found the simple answer:
ListObject.ListColumns("Column name").DataBodyRange.FormulaR1C1 = "new formula"
This will overwrite any manual value (just as the normal behavior with AutoCorrect).
Best would be if you could show us a screen shot of your sheet. Based on that we would have articulated the answer.
Here is with the general assumption. That you have one list object to dump some data to a column and rest of the cells in that column are manually interacted.
You could try the following manually first and see if it works for you. Still the manual one is being overtaken by the code level, then you do this in code level. :)
The main action here is to Stop automatic corrections
Go To -> Tools menu -> Click AutoCorrect Options ->
In the AutoCorrect Tab ->
1- To prevent ALL automatic corrections
Clear the check box for Replace Text as you type
2- To prevent SPECIFIC corrections
Clear the corresponding check box for the option. I believe you are more interested in the latter - specific data range that you just dump via a listobject.
Now here is the code for disabling this feature at code level.
When working with Excel Tables (ListObjects) there are two AutoCorrect options to consider: You can read about those two in details.
* Apply new rows and columns in table
(VBA AutoCorrect.AutoExpandListRange Property)
* Fill formulas in tables to create calculated columns
(VBA AutoCorrect.AutoFillFormulasInLists Property)
Code you may want to use at the top of our listobject is,
Application.AutoCorrect.AutoFillFormulasInLists = False
And totally agree that it would be so much more useful if Application.AutoCorrect. AutoFillFormulasInLists controlled on a table by table basis at the ListObject level. So here is a workaround.
So one way is to clear your table data each time. And when you clear data you can make sure for TABLE TO FORGET forumulae and formatting. So it will clear the contents of the data body range before deleting table contents.
Sub forgetMe()
With Sheet1.ListObjects("myTable")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.ClearContents
.DataBodyRange.Delete
End If
End With
End Sub
When you input the data, start with auto stuff off.