Including text with quotes within a formula in VBA - excel

I have a button coded to reset a spreadsheet with the following line in VBA:-
Range("F6") = "=IF(AND(vtype=TRUE,U41>0,vatable=FALSE),vatrate*100&"% VAT",IF(AND(vtype=TRUE,matchnew>1,vatable=TRUE),"VAT Exempt",""))"
When this button is clicked an error appears, it says that the % sign is an illegal character.
Please advise what is incorrect.

Try:
Range("F6") = "=IF(AND(vtype=TRUE,U41>0,vatable=FALSE),vatrate*100&""% VAT"",IF(AND(vtype=TRUE,matchnew>1,vatable=TRUE),""VAT Exempt"",""""))"
Excel is not objecting to % but to " without being escaped (with ").

As a test, I named a cell 'vatrate', then put =vatrate*100&"%V VAT" in another cell and it evaluated with no problem.
Put your cursor in the cell where this formula is
Click on 'Formulas' on the ribbon
In the 'Formula Auditing' section of the ribbon, click on 'Evaluate Formula'
That will allow you to walk through your formula one evaluation at a time so you can see exactly what Excel's seeing as it's evaluating it. That will tell you where it's going wrong.

Related

Batch add a formula in Excel 2016

I have a spreadsheet with hundreds of cells containing formulas like =('Pricing Master'!$E135*'Pricing Master'!$L$29). I would like to batch add the ROUNDUP formula, so that they all read, for example, =ROUNDUP('Pricing Master'!$E135*'Pricing Master'!$L$29,0). A simple Replace All will not work, as it requires both the function call preceding as well as the Number argument following. Not providing both at the same time produces an error. This creates an issue with batch editing using Replace All.
I am sure that there is a way to do this with the Paste Special function, although if there is another way I would be glad to hear it.
My approach is this and I do this often with large sheets with many formulae:
One : select the row(s) or column(s) you want to work with,
Then edit/replace “=(“ with “xyxy(” (I use xyxy as it just doesn’t come up...
Now all replace operations will be quicker as there is no re-calc happening...
So now do edit/replace “xyxy” with “xyxyroundup(“ and “)” with “,0)”
Then just replace “xyxy” with “=“
And wait for it to finish its calculations...
loop through each cell and add formula to the original formula. Like this
Sub updateFor()
Dim r As Range
For Each r In Selection.SpecialCells(xlCellTypeFormulas)''only cells with formula in
r.Formula = "=ROUNDUP(" & Mid(r.Formula, 2) & ",0)"
Next r
End Sub
just replace the selection with the range you want to edit
if you arnt familiar with vba heres a quick guide
press ALT + F11 to show the vba editor
from insert menu select module
module1 should appear in the project window in the top left,
click on this
a large window should open on the right paste the above code in
go back to your worksheet and select the cells you want to roundup
goto to the view ribbon click the macro button on the far right
select the macro 'updateFor' and press run

Replacing file path in excel with another

I have an excel file with cells that retrieve data from a file stored on another computer. I need to update the file path in all of these formulas but it is extremely tedious as each time I update the formula, an open file window comes up. The location that I am changing this to is also not on my computer.
Is there a quick way to update a file path formula without having this dialog window open up?
I need to change my path from
='\\clusfs001nas\
To:
='R:\
Under the "Data" Tab, click "Edit Links" - this should show you the files you have linked to, and you can "Change Source" to update it. Alternatively, you could do a simple Find/Replace (CTRL+F, then click "replace" and type the path you need to replace and then in the replace area, put the new path).
You can change formulas using VBA.
But first, I suggest you have it in only one cell, and in the formulas, you add this cell, not a real path. This way, next time you need to change it, you change only one cell.
Now the coding, an example to change A2 and B5:
Sub Change()
Range("A2").Formula = type the formula here between quotes
Range("B5").Formula = type the formula here between quotes
end sub
If you have lots of cells in a column, you could do a loop:
For i = 1 to 20 'say you have cells from row 1 to 20
Cells(i,3).Formula = type the formula here between quotes
'the number 3 above is the third column: C
next
This is late, but one option that can avoid the annoying dialog when you click Replace All is to insert some random characters at the beginning of the path which will prevent Excel from thinking that the string is an actual link. Then you should be able to edit the portions of that path that need to be changed, and once that is complete, do another Replace ALL, basically removing the random characters that were inserted.
You need to be careful of course, because if you choose a random character that happens to be within other formulas the Replace ALL may have some unexpected results
You can write on your code:
Application.DisplayAlerts = False
Application.AskToUpdate = False

Create a hyperlink that is dynamic

In Excel 2010
On the same worksheet
I want to be able to click on Cell # A3
that click has it jump to # A8 (in the same worksheet).
and then be able to insert a row at # A6,
and still have A3 automatically jump linked to #A8
My apologies, my searches do not seem to point me to a solution. If it is there just point me there and I will see what my limited Excel skills will allow me to learn. The hardest part is learning the terminology to discover what my solution will be. FYI - I have never done a "VBA' solution in my past so that part will be a little difficult for me to understand, but I can try.
I was hoping the 'Edit Hyperlink' window's 'Type the cell reference' field would have allowed for $ character to keep the 'mapped' cell as dynamic.
You can use 'DEFINED NAME' in hyperlink. Use name box to create the defined name for your 'to be' hyperlinked cell and use that defined name to link. More step by step details is available here
In cell A3 enter the formula:
=HYPERLINK("#A8","A8")
and as Tim Williams points out, that is where it will always go.
If, however, you wanted the destination to "move" as A8 moves, you would assign a Name to the destination
Well I have a friend that found my answer that worked
One liner:
The technique is 'define the name' for the cell to be linked and use that 'defined name' in hyper link.
Ref:
http://sivajankan.blogspot.com/2015/04/excel-hyperlink-tying-to-cell.html
Before going on step by step, please find what is 'Name Box'.
Name box:
The left most cell in 'Formula bar' is the Name Box. When you hover the Name Box cell, it shows 'Enter a name for a cell range, or select a named range from the list' [in MSOffice 2013]
Step by Step:
Create the Defined Name:
Select the cell you want to be hyperlinked (to be jumped to). In the 'Name Box' [check the image above], enter a 'reference' name and press return key. [Assumed 'Reference1' for our example case]
Use the Defined Name in Hyperlink
Go to the cell where you want to place the hyperlink (to be jumped from), right click and click on 'Hyperlink' from the menu
In the 'Insert Hyperlink' popup window, in 'Anchor' ['Link to' for older version] section, click on 'locate'; which opens another popup window
In the 'Select Place in Document' popup, expand the 'Defined Names' by clicking the small triangle [or + icon] next to it [make the triangle point down, or change + to -, by clicking], and select the reference you made in step 2. [Our example 'Reference1']
Now your hyperlink pointed to the reference; so inserting or deleting any rows or columns won't affect your hyperlink pointing cell location.

Adding Apostrophe in every field in particular column for excel

How can you add an apostrophe in every field in an Excel spreadsheet without individually typing it in? I have got like 5k fields
I'm going to suggest the non-obvious. There is a fantastic (and often under-used) tool called the Immediate Window in Visual Basic Editor. Basically, you can write out commands in VBA and execute them on the spot, sort of like command prompt. It's perfect for cases like this.
Press ALT+F11 to open VBE, then Control+G to open the Immediate Window.
Type the following and hit enter:
for each v in range("K2:K5000") : v.value = "'" & v.value : next
And boom! You are all done. No need to create a macro, declare variables, no need to drag and copy, etc. Close the window and get back to work. The only downfall is to undo it, you need to do it via code since VBA will destroy your undo stack (but that's simple).
The way I'd do this is:
In Cell L2, enter the formula ="'"&K2
Use the fill handle or Ctrl+D to fill it down to the length of Column K's values.
Select the whole of Column L's values and copy them to the clipboard
Select the same range in Column K, right-click to select 'Paste Special' and choose 'Values'
i use concantenate. works for me.
fill j2-j14 with '(appostrophe)
enter L2 with formula =concantenate(j2,k2)
copy L2 to L3-L14
More universal can be:
for each v Selection : v.value = "'" & v.value : next
and selecting range of cells before execution

Force Refresh of "Last" Cell of the Worksheet

Pressing Ctrl+End in Excel takes you to the bottom-right-most cell of the worksheet.
If you delete the last rows or columns and save the workbook, this last cell gets updated, as well as the scrollbars.
I remember there was a one line VBA command that you could run that would do the update without having to save the workbook, but I can't remember what the command is - do you have any ideas?
I’ve found something that consistently works to delete those blank rows. You can tell when the “used range” excel is using is too big and is going to add extra blank rows when you use the scroll bar to the right and it goes beyond the last row of actual data when you scroll to the bottom. This will caused extra blank records to be added to the table when it is imported into SQL.
To get rid of them:
Select the entire first row under the last row of data. Hit Ctrl + Shift + Down Arrow to select all the blank rows.
From the Home Tab Select Clear and then Clear All from the Edit menu (picture of a whitish-grey eraser).
Right-click and select Delete.
Goto cell A1 first and then Save.
Press Ctrl + Home and then Ctrl + End (This should take you the correct last cell in the used range (above blank rows).
Click Save again.
Here is the answer:
Sub x()
ActiveSheet.UsedRange
End Sub
Run this and the last cell will be reset.
When none of the above works try this.
Select the unused rows and change the row height.
Now delete the rows and save.
Bingo!
Here's what I did... since none of the above worked (this time that is, which is sad cause this code was running beautifully then all the sudden xlCellTypeLastCell totally failed me.) This will only work if you hardcode the first cell of the region you wanna grab the last cell of... for example I was pasting data tables into a sheet of 12 - 40 columns and 60-90 rows... but since it was a paste, it always started in cell A79...
Worksheets("Data_Import").Activate
Range("A79").CurrentRegion.Select
A = Selection.Rows.Count - 1
B = Selection.Columns.Count - 1
Selection.Offset(A, B).Resize(1, 1).Select
Set DataEnd = Selection
I feel sad to NOT use the cool special cells thing, but alas, if it doesn't work! then I just can't use it. :C
p.s. - you could also throw in a
ActiveSheet.Cells(Rows.Count, 1).End(xlUp).CurrentRegion.Select
This solution works for Excel 2013, but may also work for most recent versions of Excel:
Choose the worksheet where you want to change the last cell, and delete any unused rows and columns
Click on File - Options - Customize Ribbon
Under Main Tabs, check the box next to "Developer", then click OK
On the Developer ribbon that now appears, click Visual Basic
In the upper-left corner, under Microsoft Excel Objects, click on the Sheet Name where you want to force a refresh of the worksheet's last cell
In the menu, click on Run - Refresh, then close the Visual Basic Window
When you hit Ctrl + End, your last cell should now be refreshed.
Check out http://dmcritchie.mvps.org/excel/lastcell.htm#MakeLastCell, found the link from a similar question.
Far from the forgotten one liner, but did solved the problem for me.
Alternatively;
Turn on manual calculation (to preserve references).
create new sheet.
Copy cells and Name of old sheet.
For some reason the code ActiveSheet.UsedRange alone did not work for me on Excel 2016 64-bit to force Excel to reset the last used cell.
This does work for me. Hope this helps.
Rows(25:415).Clear 'deletes contents and formatting
ActiveSheet.UsedRange 'resets last used cell
The solution to change row height to zero and saving worked.
Reopen, set row height to 12 and notice that End Home is no longer at the very bottom right of the worksheet.
THANK YOU. I have been working on this for over two years.
Jim Champaigne
Elkhart, Indiana
I'm not sure why everyone is making it so complicated. Just press Ctrl + PgDn.

Resources