How can I include a hyperlink to a cell in another sheet? - excel

I would like to make the text displayed to become a hyperlink that will take you to the sheet and cell when the text displayed is selected.
What am I doing incorrectly?
Thank you in advance,
If wb.sheets("Coversheet").Cells(lastRowC, 8) <> WS.Cells(i, 5) Then
wb.sheets("Coversheet").Cells(lastRowC + 1, 2) = "Carrier"
wb.sheets("Coversheet").Cells(lastRowC + 1, 3) = "Employee with a $0 salary found. Unable to calculate salary based benefits. Please rerun Census report"
wb.sheets("Coversheet").Cells(lastRowC + 1, 4) = WS.Cells(i, 4).Hyperlinks(1).Address
End If

I would simply do that in your case to create the hyperlink. It's flexible and easy to manipulate.
Dim hyperlinkLocation As String
hyperlinkLocation = "[ActiveWorkBook.Name"&".xls]1!A1" '<- 1 is the sheet index (important in your case)
ActiveWorkSheet.Hyperlinks.Add Anchor:=.(where the link is), Address:=hyperlinkLockation, _
TextToDisplay:=(name of the link)

Related

Excel formula to find a number within a range of numbers

EDIT, in short:
I have an excel table that looks like this:
tabel
Each cell contains a range of numbers.
I'm looking for a function that can search for a number in the whole table and indicate me the column and row where the range that it belongs to is located.
So, if I search for number 2346, it should function like this:
function (2346) > result (C1, R2)
I have a huge archive of photos (about 300.000 items) in a library.
Photos are stored in boxes, the boxes in shelves.
Each box has a range of inventory numbers of photos.
I want to create a map of the deposit that looks like this:
Shelf 1 - contains boxes 1, 2, 3, etc.
Box 1 - contains photos with inventory numbers between 1257-1321
Box 2 -"- between 2345-2522
Box 3 -"- between 123523-123643
Translated in an excel table, it would look like this:
Column 1 would be Shelf 1, containing the boxes with the images:
cell in column 1 / row 1 (that's box 1) contains the range of numbers: 1257-1321
cell in column 1 / row 2 (box 2), the range: 2345-2522
cell in column 1 / row 3 (box 3), range: 123523-123643
They are not in order, because they entered in more than 100 years in the collection, and they are arranged as they entered and by subject. Not to mention that the deposit has been moved a few times. So, I have a hard time to find one image when I'm looking for it.
But if I have this excel table, this map of the deposit, supposedly I want to enter the inventory number that I'm looking for, let's say "2346", and a formula that search throughout the whole table would indicate me that the item (number I look for, is in a range that is located in column 1, row 2, that means shelf one, box 2 in the deposit).
Actually the concept is very simple, excel is able to do MUCH more difficult tasks, and I'm amazed I can't find a way to do this. I'm a photographer and librarian, so my experience in programming is close to zero.
Thank you very much if you can help!
It's a bit of a lenghty one but not hard to understand, I made some sample data like so:
="C"&SUMPRODUCT(((VALUE(LEFT($A$1:$B$3,SEARCH("-",$A$1:$B$3)-1))<=D3)*(VALUE(RIGHT($A$1:$B$3,LEN($A$1:$B$3)-SEARCH("-",$A$1:$B$3)))>=D3))*COLUMN($A$1:$B$3))&", "&"R"&SUMPRODUCT(((VALUE(LEFT($A$1:$B$3,SEARCH("-",$A$1:$B$3)-1))<=D3)*(VALUE(RIGHT($A$1:$B$3,LEN($A$1:$B$3)-SEARCH("-",$A$1:$B$3)))>=D3))*ROW($A$1:$B$3))
Youll just have to change ranges
If you set up your worksheet like the image below (sheet name "Library"):
You could try the code:
Option Explicit
Sub Painting()
Dim LastRow As Long, i As Long, SearchValue As Long
Dim arrData As Variant
'Let s say that all data appears in sheet called Library
With ThisWorkbook.Worksheets("Library")
'Let s say that we are looking for the value in cell H1
SearchValue = .Range("H1").Value
'Find the Last row of column A sheet Library
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Create as array from Column A row 2 up to Column D row LastRow
arrData = .Range(.Cells(2, 1), .Cells(LastRow, 4)).Value
'Loop Array to find a match
For i = LBound(arrData) To UBound(arrData)
If arrData(i, 3) <= SearchValue And arrData(i, 4) >= SearchValue Then
'Pop up with Shelf & Box name
MsgBox "Search Value " & SearchValue & " included in:" & vbNewLine & "Shelf: " & arrData(i, 1) & vbNewLine & "Box: " & arrData(i, 2)
'Select the line where the Search Value found
.Range("A" & i + 1 & ":D" & i + 1).Select
'Exit the loop
Exit For
End If
Next i
End With
End Sub
Result:

how to add (plus) to a cell using a textbox

i need to add a number to textbox for everytime a specific item that was used to a cell in excel using vb. For example, for everytime the word "Time" is selected from the combobox1, it must add 1 to cell B2. and if "Time" is selected again. it must add 1 again to cell B2. Then cell B2 must have "2" in it
ive tried this, but it doesnt seem to work:
Dim Scrap As String
Scrap = ComboBox1.Text
Select Case Scrap
Case "Aluminium"
worksheet2.Cells(4, 3) = +AmountBox.Text
worksheet2.Cells(4, 4) = +KiloBox.Text
worksheet2.Cells(4, 2) = +1 'adds 1 for each time this type of scrap is used
end select
Dim Temp as Integer = CInt(worksheet2.Cells(4, 2).Value)
Temp += 1
worksheet2.Cells(4, 2).Value = Temp
Try something like this
This as well can do it:
worksheet2.Cells(4, 2) = (worksheet2.Cells(4, 2).Value + 1)

vba how to match cell with range and return value in adjacent cells?

Excuse my lack of coding language, I'm just getting into vba coding.
I have a list of Manufacturers (names) with contact details on one that I like to be populated on my "SPECSHEET" automatically when choosing manufacturer.
Worksheet "SPECSHEET" is fully created from macro.
Worksheet "CONTACTS" has a list of names in column A and phone numbers in column C
Worksheet "DATASHEET" holds information about each luminaire.
I am successful in showing the right manufacturer name:
Worksheets("SPECSHEET").Cells(Company, 5).Value = Worksheets("DATASHEET").Cells(1, 5).Value
But I like the row below to show the phone number:
If Worksheets("SPECSHEET").Cells(3, 4).Value = Worksheets("CONTACT").Range(A:A).Value Then
Worksheets("SPECSHEET").Cells(3, 5).Value = Worksheets("CONTACT").Cells(B "name adjacent to the matched name in column A)
End If
Thanks for any help.
Try this:
Dim t
t = Application.Match(Worksheets("SPECSHEET").Cells(3, 4).Value, Worksheets("CONTACT").Range("A:A"), 0)
If Not IsError(t) Then
Worksheets("SPECSHEET").Cells(3, 5).Value = Worksheets("CONTACT").Range("B" & t)
Else
Worksheets("SPECSHEET").Cells(3, 5).Value = "Not Found"
End If
It uses the worksheet function MATCH() to find the row and then returns the value in Column B of that row. If it is not found then it returns Not Found.

Value of TextBox searched and jump to a cell by clicking a button in the UserForm

I have an Excel VB code in which I made a button in Excel that when clicked, goes to the next sheet and searches for the value based on employee number. I used VLookup in this code. My problem is solved.
The same thing I am doing with UserForm in Excel. Now I have a textbox1 for employee number and textbox2 for employee name, the same is in sheet2 with ColumnA and ColumnB.
Now when I enter the employee number in textbox1, the name is appearing in textbox2 and also a button to change the value of textbox2.
I want to change the value of textbox2 and when the button is clicked, go to that particular employees name field. How can I do this?
Try the below piece of code
Dim i As Long
Dim rno As Integer
i = 0
Do While Sheets(2).Cells(i + 1, 1).Value <> ""
If Sheets(2).Cells(i + 1, 1).Value = TextBox1.Text Then
rno = Sheets(2).Cells(i + 1, 1).Row
GoTo Condition
Else
rno = 0
End If
i = i + 1
Loop
Condition:
If rno <> 0 Then
Sheets(2).Cells(rno, 2).Value = TextBox2.Text
Else
MsgBox ("No Such number is found")
End If

How to display multiple values in a single cell in Excel

I'm am relatively familiar with Excel and its functions, but am very new to VBA (I do however have background in MATLAB and C). Basically what I have is a sheet with a different machine populating each column header and a name of an employee populating the first column. The table contains text values of either "Train", indicating that the person in that row is trained on the equipment in the specified column, or "No", indicating that they are not. What I want to do is to make a separate sheet that has the Equipment in the first column and one column headered as "Trained". Each cell will theoretically be populated with the names of the people who are trained on the equipment for that row. I have a for loop code in VBA that successfully outputs the names into the immediate window
Function Train(Data As Range, Name As Range)
For Counter = 1 To Data.Rows.Count
If Data(Counter, 1).Value = "Train" Then
'Debug.Print Name(Counter, 1)
End If
Next Counter
End Function
but I have been unable in a few hours of searching to figure out how to display these values in a single cell. Is this possible?
Thanks in advance!
You have to choose if you want to do "For each person, for each machine", or "for each machine, for each person" first. Let say you want to go with the second idea, you could go with this pseudo code:
set wsEmployee = Worksheets("EmployeeSheet")
set wsEmployee = Worksheets("MachineSheet")
'Clear MachineSheet and add headers here
xEmployee = 2
yMachine = 2
do while (wsEmployee.Cells(1, xEmployee).Value <> "") 'or your loop way here
yEmployee = 2
trained = ""
do while (wsEmployee.Cells(yEmployee, 1).Value <> "") 'or your loop way here
if (wsEmployee.Cells(yEmployee, xEmployee).Value = "Trained") then
trained = trained & wsEmployee.Cells(yEmployee, 1).Value & ", "
end if
yEmployee = yEmployee + 1
loop
'remove the last , in the trained string
wsMachine.Cells(yMachine, 1).Value = wsEmployee.Cells(1, xEmployee).Value
wsMachine.Cells(yMachine, 2).Value = trained
yMachine = yMachine + 1
xEmployee = xEmployee + 1
loop
That's the basic idea. For better performances, I would do all these operation in some arrays and paste them in one operation.
Use the concatenation operator (&) to assemble the values into a string:
Dim names as String
names = ""
For Counter = 1 To Data.Rows.Count
If Data(Counter, 1).Value = "Train" Then
If counter = 1 Then
names = names & Name(counter, 1)
Else
names = names & "," & Name(counter, 1)
End If
End If
Next Counter
Then just place names in whatever cell you want.

Resources