Modify range of Hyperlinks in Excel - vba

So, I have a spreadsheet that I have sent out to several of our staff asking to update certain fields/columns. I have a Document Link field (Column G) which links them a folder in our F: Drive which they can use to help populate the necessary fields.
The spreadsheets which I want them to complete are also in this F:Drive. However, I noticed each of the document links no longer work, and when I check the link under Edit Hyperlink, I notice instead of the desired "*F:\Procurement..." I have my home drive "+\NSH-HDRIVES1..." as the header,
F:\Procurement\Contracts\Alco...xxx...
\NSH-HDRIVES1\Contracts\Alco...xxx...
I have hundreds of these lines with document links that need "\NSH-HDRIVES1" replaced with "F:".
Is there some kind of VBA Macro that will let me do this? I have little experience and time to learn VBA Macros at this point so any help would be greatly appreciated. Note each link is different and goes to a different folder within our F:Drive, all I need is to change the prefix.
Thanks and sorry for any duplication.

The following code may help, but it is set up to only work on the ActiveSheet. If your hyperlinks are in multiple workbooks and multiple worksheets you will need to loop through them all.
For Each h In ActiveSheet.Hyperlinks
h.Address = Replace(h.Address, "\NSH-HDRIVES1\", "F:\Procurement\")
h.TextToDisplay = Replace(h.TextToDisplay, "\NSH-HDRIVES1\", "F:\Procurement\")
Next

Related

How to find active cells range in excel VBA

I am trying to open multiple webpage tab from my selected cells. I would like to find the selected cell (based on the mouse) starting and ending row and column information for further use of vba macro.
Thanks in advance...
You can do this:
x = ActiveCell.Address
MsgBox (x)
but really, you should try to avoid using selection where possible. The reason for this is because users can (and I've found, will) click in to other spreadsheets as the code is running and so what you have intended as the selection, may no longer be the actual selection. It also affects the longevity of the code because it's much more difficult to fix if something breaks.

Sheet not found via codename

I'm running across and error that appears sporadically. Essentially, a master .xlsm file is used by multiple people to populate data for aggregation. I then use another .xlsm file with macros to pull the data and aggregate it into a single source file.
The code generally works quite well, with one exception:
'define range based on count'
Set rngItemRange = SourceWb.Sheets("Quality").Range(Cells(3, i), Cells((intItemCount + 2), i))
'write concatenated range'
targetwb.Sheets("Raw Data").Cells(pintDest_row, pintCol).Value = concatRange(rngItemRange)
It cannot find the tab "Quality" on some work books (but not all). Okay, maybe someone renamed the tab? checked that, thats not true. One thing that works as a work around: on the open source workbook, when debug throws an error, if i manually click the quality tab, then resume the macro, it will continue.
I also said, okay, well maybe there's some weird character recognition issue, so I started to refer to the sheet by the codename as shown in the vba editor. I experience the same behaviors.
This affects maybe 50% of the workbooks, and I cant find any root cause. I have similar code elsewhere, for different sheets, but this is the only one where i define a range to pass into a function using the "set" command. Again, this only happens sometimes on some workbooks, and i can continue to execute when i manually click on the tab i'm pulling data from.
I'll also add, there is only one workbook open with this sheet name, at any given time, so i dont think it's choking trying to figure which sheet is relevant. Plus sourceWb is a set variable.
Does someone have any clue whats going on? anything to try? solutions? help!
(Forgive any typos, I'm working on a broken thumb right now.)

Excel VBA - Extract/Open a dynamic hyperlink URL/Address

Greeting!
I am attempting to use excel vba to extract a dynamic hyperlink from OR use excel vba to open the dynamic hyperlink in a worksheet. I am able to click the hyperlink and it will open up correctly. So far, I am not able to find any relevant information pertaining to how to extract the URL the hyperlink is pointing to or simply open the hyperlink.
Here is the formula that is used to build the dynamic hyperlink based on a model selection.
=IFERROR(HYPERLINK(VLOOKUP(S.O.P.!$D$3,Models!$C$2:$G$296,4,FALSE), "Click Here to Open Box Label"), "Unable to retrieve part information")
So based on a selection the user makes this formula goes and finds the respective link that will be used to create the hyperlink.
What I want: Using a button - open the file(PDF) to allow the users to easily print the files they need from one button. I am trying to minimize human touches to eliminate the possibility for human errors.
I have also attempted to record a macro to open up the link, but it only takes me to the worksheet that the hyperlink is placed in.
Here are links to other post that were not able to resolve my issue, but provided good information...
Excel VBA Get hyperlink address of specific cell
Extract URL From Excel Hyperlink Formula
Thank you, I look forward to some helpful responses c:
let me know if I need to elaborate or clarify anything!
Any reason you cant run the vlookup in VBA. Then you will have the link address as a string which you can then do what you want with:
Sub GetLink()
Dim ReturnedAddress As String
ReturnedAddress = Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("S.O.P.").Range("D3"), ThisWorkbook.Sheets("Models").Range("C2:G296"), 4, 0)
end sub

Excel VBA: Resetting spreadsheet count

I have a excel VBA macro that dynamically generates and deletes spreadsheets based on user input. However, when I open the VBA IDE, it seems that although I am naming my spreadsheets in the subs that create/delete them, the overall count is still increasing.
For example, depending on how far into execution my program is, under the "Microsoft Excel Objects" folder in my current project, the spreadsheets in the current workbook could look something like
Sheet101(Sheet3)
Sheet103(Sheet2)
Sheet104(Sheet1)
Or
Sheet81(Inputs)
Sheet83(Date Adjustment Interpolation)
Sheet84(Pricing)
Sheet85(Comparison)
No matter if I delete the rest of them and add one, it still picks up where the last highest one left off.
I don't know how many times this macro will be run and I'd feel a lot better about putting it out there if I could reset this annoying tally on the number of spreadsheets that have ever been generated, since I don't know for sure where excel will cut me off. Plus it's just annoying.
My Question:
I would like to know how to alter that spreadsheet number, or at least what the relevant object is for doing so.
Thanks!
Thanks to #dijkay s suggestion on code names, I've found some code to accomplish this.
ThisWorkbook.VBProject.VBComponents("Sheet1").name = "test"
Will change the code name of Sheet1 to test, so in the Excel Objects folder, it will appear as test(Sheet1) for example.
This option, however, requires messing around with some trust/security settings in each individual excel client running the macro, which is unsuitable for my purposes, unfortunately. You can also change the value manually by changing the (Name) property directly in the IDE through the properties window.
here are some ideas you can try...
Sheets(x).Name = "Sheet" & x
or (assuming in this example, 'Sheet3' doesn't already exist:
Set Sheet3 = sheets.Add
Sheet3.name = "Sheet3"
This is more cleanup than re-setting
cheers,
Micéal

Edit multiple Excel workbooks in folder

I have a couple hundred CSV files I need to do the same edits to.
-I need to change one column from scientific notation to a number without and decimals
-If a cell in specific column begins with '2014', delete entire row
-Save as new file name, from 'filename.csv' to 'filename_new.csv'
I tried using Kutools for Excel but couldn't figure out if I could edit multiple books
Could someone help me out with this? I appreciate the help. Thanks.
Workbooks.Open "YourFilePath" will let you open a single file (don't forget that you'll probably need to store the workbook in a variable so you can better address it).
You can then do whatever edit you like, for example your YourSheet.Cells(x,y).EntireRow.Delete under an If condition.
You can then .Close your Workbook and specify a filename to save.
Matt, I'm sorry but I can just provide you some ideas. Writing the code won't help you learn :)