Replacing file path in excel with another - excel

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

Related

Copy arrays from different location in a worksheet and use it to paste it into any other application without additional spaces

My excel sheet creates three proper sentences at three different locations in the worksheet based on the data in the tables but has double spaces between some of the words. I am trying to find a code where I can click a button that copies information from these different arrays in the worksheet that I can then paste into a word file without any double spaces or tab spaces and add some additional generic lines.
Currently, we are manually copying information from these arrays into a notepad to remove the table, the cells and their formating. Then manually replace all the double space and tab spaces with a single space, add some additional generic lines and then copying this information into a Word file.
Trying to write a code that will allow me to do this by clicking the button and then pasting it anywhere I want. What code can I use for this? Thanks.
Here are some ideas, untested so you will probably have to experiment to get them to work. If you get stuck, please use google to research the problem, then post your code and issue in a new question, so folk have more to go on. I'm assuming you have found the VBA editor, the alt-f11 shortcut may work.
Use the Names Manager to designate each of the three sentence locations as a "named range", e.g. first_sentence, second_sentence and third_sentence
Choose a shape and add it to your workbook. The shape will serve as the button. If you right click on the shape, at the bottom of the list of options, is one to connect a macro (which does not exist yet)
In the VBA code editor, make a new module, then create the macro by typing what is below. After creating it, you can connect it to your button, and that will allow you to test it.
Option explicit
' the macro "transfer_text" controls the export of text from the Excel sheet to the Word document
sub transfer_text()
' this is where clean-up code and everything goes
end sub
You need to tell your transfer_text macro where the cells of interest are, perhaps by pasting the following into the body of the macro:
dim range_first as range, range_second as range, range_third as range, clean_first as string, clean_second as string, clean_third as string
' set the ranges so the macro knows where the data is
with thisworkbook.worksheets("Sheet1")
set range_first = .range("first_sentence")
set range_second = .range("second_sentence")
set range_third = .range("third_sentence")
end with
' update the data (without changing the original) using the clean_text function (see below)
clean_first = clean_text(range_first)
clean_second = clean_text(range_second)
clean_third = clean_text(range_third)
' this is where we connect Word
I would use a function to clean the text in each of the ranges, like this, stored outside the main macro, though in the same module for convenience. You can add text editing operations to it later:
Function clean_text(string_text as string) as string
' the text from the range is passed in
' various operations performed on the text
' the result is sent back to where the function was called
clean_text = trim(string_text)
end function
That is rather alot already, and we have not even connected to Word. To connect to Word is really better done as a separate topic. You will need to set a specific reference to word, using the Tools-References menu in the VBA project. Scroll down the list until you find Microsoft Word, and tick the box.
That will give you access to the object library for Word, so you can place your text using bookmarks in the Word document.
You will also need to deal with the file object - opening the Word document. Another topic for you to research!
That should get you started!
Some notes on debugging. Set a breakpoint at the start of the With statement by clicking the mouse in the left hand margin of the code editor. Then click you button... the code will stop at the breakpoint. Use the mouse-over and the Immediate Window (you may need to unhide it) to check variable values. Press F8 to step forward one line of code. In the Immediate Window, type a ? followed by the variable you want to check.

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

changing a wildcard in an excel hyperlink with vba

I would like some help with removing/replacing a wildcard character in an excel hyperlink.
Logically it seems very easy but it's beyond my abilities.
I have a spreadsheet with hyperlinks to PDF documents. The hyperlinks contain the "#" character and that stops the file path from working. In the hyperlink I simply need to change the "#" to "%23" and the link works. I don't want to do this manually because of the amount of links. Is there any way of achieving this with VBA. It seems easy enough to change a file path but searching a hyperlink and changing the # doesn't seem to be possible.
All the hyperlinks are in column A.
Excel treats text to the left of the # as the .Address and to the right as the .SubAddress - as it suggests an anchor type link. You'd need to repair this on each link like so:
For Each lk In Sheets("YourSheetName").Range("A:A").Hyperlinks
If lk.SubAddress <> "" Then
lk.Address = lk.Address & "%23" & lk.SubAddress
lk.SubAddress = ""
End If
Next

VBA Excel: How to edit links using user cell inputs?

I'm trying to make a macro that, among other things, updates external file links in several cells, where the file location currently in the cells and that which I want to change it to are specified by the user in another tab.
I've tried to do this via find/replace; if I try to do this with the text specified in the code:
Range("b3").Formula = Replace(Range("B3").Formula, "\\folder\file", "\\folder\newfile")
Then it'll replace this text and the links update correctly. If I change the locations with inputs, say oldlocation and newlocation:
oldlocation= "\\folder\file"
newlocation= "\\folder\newfile"
Range("b3").Formula = Replace(Range("B3").Formula, oldlocation, newlocation)
This also works ok. But if I change the definitions of the locations (e.g. B3: "\folder\file"):
oldlocation= Range("b3").text
newlocation= Range("b4").text
It no longer works - passes over the line in the code with no change or error. I've made a quick check and Range("b3").text & "\folder\file" both seem to be text strings; after that I'm stumped. I've tried a few different find/replace formats I've found, but all with the same result. What am I missing?
The problem is when you define the string to be replaced as
oldlocation= Range("b3").text
the oldlocation variable will have the value of the cell (the data you see in the cell) not the formula of the cell that contains the current reference, so the replace function does not find the string to be replaced.
You have to extract the location that you want to replace from the Range("b3").formula string and work with that.

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

Resources