In VBA how do I check if a hyperlink is sending data? - vba

I have a excel document that is pulling data from many other excel files using hyperlinks. Certain clients enter their data into template files and my file pulls that data automatically through hyperlinks. For example
='http://hyperlink[filename.xlsm]Data'!E50
Now not everyone fills in all the fields therefor I am trying to write a macro that will go through the document and find either the cells that do not contain data or find the cells whose hyperlinks do not return values to be cleared and replaced with N/A.
Private Sub CommandButton21_Click()
Dim rng As Range, cell As Range
Set rng = Range("E47:H234")
For Each cell In rng
If (IsEmpty(cell)) Then
cell.Value = "N/A"
End If
Next cell
End Sub

Related

Counting Hyperlinks

I am having a tiny problem with Inserted hyperlinks. If I start with a blank sheet and insert a hyperlink and then copy it to two other cells using two individual Copy/Pastes:
Sub hyper1()
Cells.Clear
With ActiveSheet
.Hyperlinks.Add Anchor:=Range("A1"), Address:="http://www.cnn.com/", TextToDisplay:="News"
Range("A1").Copy Range("A2")
Range("A1").Copy Range("A3")
MsgBox .Hyperlinks.Count
End With
End Sub
Three cells get filled and Excel correctly reports the number of hyperlinks as 3.
However if I fill the second two cells with a single Copy/Paste:
Sub hyper2()
Cells.Clear
With ActiveSheet
.Hyperlinks.Add Anchor:=Range("A1"), Address:="http://www.cnn.com/", TextToDisplay:="News"
Range("A1").Copy Range("A2:A3")
MsgBox ActiveSheet.Hyperlinks.Count
End With
End Sub
The same three cells get filled, but Excel reports the number of hyperlinks as 2!!
Which is broken, Excel 2007 or my computer ??
Sub hyper3()
Cells.Clear
With ActiveSheet
.Hyperlinks.Add Anchor:=Range("A1"), Address:="http://www.cnn.com/", TextToDisplay:="News"
Range("A1").Copy Range("A2,A3")
MsgBox ActiveSheet.Hyperlinks.Count
End With
End Sub
The comma separation splits them into two ranges instead of one
I suspect its because in the second macro, the hyperlink is copied as a single range object as opposed to two individual ranges (like the first one).
Excel will treat this as a Range object (that represents the range the specified hyperlink is attached to). It will therefore count this as one hyperlink instead of two.
You can see this if you click either of cells A2 or A3 after the macro has run (they will both change colour at the same time indicating that the hyperlink has been selected).

Excel cover sheet that is used to populate cells across the whole workbook

I am trying to create a cover sheet for an excel workbook, the user will populate these cells then a macro will paste them across all worksheets inside the workbook.
The cover sheet asks for information in cells from column D and column I which could be common across the whole workbook. I am trying to make filling out this workbook faster and easier.
I have a basic understanding of Excel VBA but require help to write this, thank you.
You code use something like this
Sub Main
Dim cellsToCopy As Range
Dim shtNames as Variant, shtName As Variant
Set cellsToCopy = Worksheets("CoverSheet").Range("D5, D11, I3, I12") '<--| change both "CoverSheet" name and its cells address list to fit your needs
shtNames = Array("firstSheet", "secondSheet", "thirdSheet") '<--| change sheets names to paste cellsToCopy values into
For Each shtName in shtNames
Worksheets(shtName).Range(cellsToCopy.Address).Value = cellsToCopy.Value
Next shtName
End Sub

Excel: Copy Dynamic Range from one worksheet to another

I am trying to copy a dynamic range (B12:Lxx)from one worksheet to another. I need the range being copied to stop at the first empty row (there is additional data further down the sheet which I don't want copied).
I am a very basic VBA user so if you could explicitly set out your instructions that would be handy.
Source: Worksheet "MyKPIs" with the dynamic range B12:Lxx (column L is set, row numbers are variable BUT must end at the first empty row)
Target: Worksheet "Month Template", cell B5
Trigger would be a command button
I have trawled through other articles but have failed to find anything that I could use.
Thanks,
Hayley
this will work. insert a command button on your worksheet. double click the button. paste in this code between sub and end sub.
Worksheets("MyKPIs").Range("b12").CurrentRegion.Copy Worksheets("Month Template").Range("b5")
it should look like this when you are through. then go to your worksheet on developer tab toggle off design mode then click the button.
Private Sub CommandButton1_Click()
Worksheets("MyKPIs").Range("b12").CurrentRegion.Copy Worksheets("Month Template").Range("b5")
End Sub
for those inexperienced with currentregion please look at the 2 samples below that have blank cells but the region is selected and you can easily see the beginning and ending points in the range and how an entire blank row or column forms the range.
Place a command button from the Forms toolbar on your "MyKPIs" sheet.
Then add this code:
Sub Button1_Click()
Dim myrange
Set myrange = Sheets("MyKPIs").Range("B12:L12")
myrange.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Worksheets("Month Template").Range("B5")
End Sub
Considering all cells in column B have data, this will copy all the cells in the range. It will stop at the first empty cell in column B. This should help you to start.

VBA (highlight Hardcode cell (i.e.1234) in Excel) after model is built

I'm building a financial model and i'm trying to highlight all the cells after the model is complete. I need to identify which one are hardcoded after the model is completely without searching for each input one by one.
it would be great, If you can help with a vba for the whole excel tab and a selected range on a sheet. thank you.
I believe what you are asking is to look through a range, and then highlight any values within that range that don't contain a formula. So first find the range you want to highlight and then find the ranges within that range that contain a formula. For my example we'll say that your model is from cells A1 to A100
Public Sub hightlightNoFormulas()
Dim yourRange as Range, rangeNoFormula as Range
Set yourRange = Range("A1:A100")
Set rangeNoFormula = yourRange.SpecialCells xlCellTypeFormulas
Then loop through your range, excluding any values that have formulas
Dim rng as Range
For Each rng in yourRange
If Intersect(rng,rangeNoFormula) Is Nothing Then
rng.interior.Color = 65535
End If
Next rng
Exit Sub

VB Compare two range of cells in multiple workbooks and if match copy to different cell

I am trying to figure this out all day. This is what I need to accomplish:
Using GetFile select a number of workbooks without opening them.
Compare Cell A2 of selected workbooks indiviually in loop to a reference workbook.
If Cell A2 of the selected workbook equals the numerical value of the numerical value within the reference workbook contained within column A then copy/past column B's text in the corresponding cell of the matched reference workbook cell to M2 of the selected workbook.
Sounds complicated but let me demonstrate. The letters represent column names.
Selected Workbook
A2=12 M2= ""
Reference Workbook
A2=12 B2=milk, protein
Desired result:
Selected Workbook
A2=12 M2=milk, protein
So far this is the code I have:
Sub Click()
Dim rCell As Range, vVal1, vVal2
Dim wbCheck As Workbook
For Each rCell In Workbooks("2.xls").Worksheets(1).Range("A1:C100")
vVal1 = rCell
vVal2 = ThisWorkbook.Worksheets(1).Range(rCell.Address)
If IsNumeric(vVal1) And IsNumeric(vVal1) Then
If vVal1 = vVal2 Then
rCell.Interior.ColorIndex = 3
ElseIf vVal1 < > vVal2 Then
End If
End If
Next rCell
End Sub
Why do you need a VBA code for this?
This can be achieved using an Excel Formula.
Paste this in M2 and copy it down
=IF(A2='C:\[Ref.xlsx]Sheet1'!A2,'C:\[Ref.xlsx]Sheet1'!B2,"")
My Assumptions (Please change as applicable)
The reference file name is Ref.xlsx
You are pulling data from Sheet1 of reference file
The Ref.xlsx is in C:
EDIT
Even if you want to use VBA, you can also do this.
Sub Sample()
Application.Calculation = xlCalculationManual
ThisWorkbook.Sheets("Sheet1").Range("M2").Formula = _
"=IF(A2='C:\[Sample.xlsx]Sheet1'!A2,'C:\[Sample.xlsx]Sheet1'!B2,"""")"
Application.Calculation = xlCalculationAutomatic
End Sub
The above formula or code is beneficial becuase of what you mentioned in 1st point Using GetFile select a number of workbooks without opening them. If you do not want to open the file then the formula or formula in VBA is the way to go :)
I solved my own question. The answer was to use VLookup.
=VLOOKUP(I2, 'C:\Desktop\Merge[ISReference.xlsx]Reference'!B2:G1923, 6, FALSE)
I am surprised no one suggested this to me. Given its simplicity I was able to build this function into some code I built to run through a FSO filedialog picker on a form in access and link external workbooks with Vlookup.
Thanks Siddarth for giving the proper direction of embedding a function instead of crazy iterated loop.