Open multiple hyperlinks in default browser [closed] - vba

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Good afternoon, I am asking this because I didn't get any of the codes I got from the web working. I have a sheet that has a lot of links and I want to be able to open at least 10 of them at a time, in tabs. I previously had some code (that I lost) that opened all of them(in chrome), which was a big problem.
I would like 1 of 2 things:
Option 1: Open all the hyperlinks I select from a column in my default browser(chrome) (the hyperlinks are there with the following formula "=hyperlink(leftcell;"OPEN")
Option 2: I paste either the hyperlink or text in another sheet and the 10 first rows are opened.
I would greatly appreciate the help.

You can use a loop to call the ActiveWorkbook.FollowHyperlink function, and use the links you already have in your worksheet.
Assuming you have the links in the form bellow:
Links in worksheet
You now have to loop in these links and open them one by one. In the bellow code the column A is hard coded, but you can easily change that by another input of yours, like a user selection, for example.
Sub test_link()
Dim current_row As Integer
Dim last_row As Integer
Dim current_sht As Worksheet
current_row = 1
Set current_sht = ActiveWorkbook.ActiveSheet
last_row = current_sht.Cells(current_sht.Rows.Count, "A").End(xlUp).Row
While current_row <= last_row
ActiveWorkbook.FollowHyperlink Address:=current_sht.Range("A" & current_row) 'Open link
current_row = current_row + 1
Wend
End Sub

Related

Receiving run-time error '9' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have just started to learn how to program using Python about a month ago and I am trying to learn a bit about VBA as well. I have an excel document with 3 sheets, the first being an inventory with columns A through W and several thousand rows. The second sheet is the assets that are in question, the third is the destination for the results.
This is the pseudo code for the macro:
Make the second sheet active
Create Loop to go through contents of column A and highlight each
Copy contents of each row to variable one by one
Make the first sheet active
Loop through the contents of Column C and D for aforementioned variable
If found highlight the active row
Copy active row to sheet 3 in next available row starting at A
I have researched how to solve this problem for the last several days, finding code for searching, looping through rows, selecting the appropriate row, going between sheets for the copy command. With all of this I have written what I believe should work for the intended purpose. I have included comments for each line to give my thought process behind it.
The error I'm receiving currently: Run-time error '9': Subscript out of range
Error location: Line 12 - where I set the targetsheet to Sheet(0).
Thank you so much for any help!
Sub SpecialCopy()
Dim targetSh, destinationSh, invSh As Worksheet
Set targetSh = Sheets(1) 'Setting initial value to Page 2 which contains assets being searched for
Set destinationSh = Sheets(2) 'Using a second one for use in the final copy statement to the destination sheet
Dim i As Long
Dim g As Long
Dim asset As String 'Using string as asset row may contain all numbers or numbers and letters
For g = 1 To Cells(Rows.Count, "A").End(xlUp).row 'Using loop to loop through values in column containing assets being searched for
Set targetSh = Sheets(1)
asset = Cells(g, 1).Value 'Setting asset to next value in Sheet 2
Set targetSh = Sheets(0) 'Not sure if I should initialize a third worksheet to use as the worksheet containing inventory, or if setting it twice in the loop would work.
For i = 1 To Cells(Rows.Count, "F").End(xlUp).row 'Looping through values in inventory to find asset
If Cells(i, 3).Value = asset Then
Range(Cells(i, 1), Cells(i, 23)).Copy Destination:=destinationSh.Range("A" & targetSh.Cells(Rows.Count, "A").End(xlUp).row + 1) 'Trying to copy the found asset, including all rows from A to W from Sheet(0) to Sheet(2)
End If
Next i
Next g
End Sub
The code can be found on Github here: https://github.com/cookchelsea/Find_and_Paste/blob/master/Master
The starting point for sheets is Sheet(1), so pointing to Sheet(0) is getting you Run-time error 9, which in this case is because you a referencing a non-existent collection (there is no Sheet(0)). More on that error code here.

Excel VBA How to multiply range by one number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am very new to VBA and I am seeking help to solve the following problem. I need to multiply a range by a single number. All of the cells with in that range need to be multiplied by that one number.
Thank you!
As what I comment just now.
1.Enter the formula at the cells (e.g. G1)
2.Press Enter key and drag the formula
keong has already shown you one method but unfortunately that method requires one to do the calculation in another cell/range. If that is what you want then go with keong's answer but if you want to do the calculation in the same range then continue reading below.
Here is another method which doesn't use formulas or VBA.
Let's say the range is A1:A10 and you want to multiply the entire range by 5
Simply type 5 in any blank cell. You can delete that later.
Copy that cell
Select Range A1:A10
Right click on it
Click on Paste Special | Values - Multiply as shown below and you are done.
Before
After
Followup from comments
In case you do not want to use a temp cell to write 5 then you can directly set 5 in the clipboard and use it like this.
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim MyData As New DataObject
Dim rng As Range
Set ws = Sheet1
Set rng = ws.Range("A1:A5")
'~~> Put the number 5 in clipboard
MyData.SetText 5
MyData.PutInClipboard
'~~> Get the data from clipboard
MyData.GetFromClipboard
rng.Formula = Application.Evaluate("=" & _
rng.Address & _
"*" & _
Val(MyData.GetText))
End Sub
Like I mentioned, you don't need VBA for this but if you still want to use VBA then you can use this instead of copying the data to the clipboard.
rng.Formula = Application.Evaluate("=" & rng.Address & "*" & MYNUMBER)
Where MYNUMBER is the variable which has the number that you want to multiply the range with.

(Excel VBA) How can I get a button on Excel to display another random cell's data from a list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have no prior experience with VBA/Visual Basic/Macros and all that - but I need to create a button that cause a cell to display another random cell's data.
I have a word list of several hundred cells and need a button that will choose one of them to be displayed in a cell at the top of my worksheet. The button needs to be able to be pressed multiple times and still bring up a random word. It doesn't need to have a fail-safe that prevents the same cell being displayed twice in a row.
I don't care how it is accomplished - it just needs to be explained a bit more as I have no idea how VBA works.
I'll give you a starter to assist your learning - (it is Christmas!) and you have the job of researching how to modify it and apply it. To get you going put this code in a standard code module, adjust the various variables to suit your display/data requirements, add a shape to the spreadsheet and assign this macro to that shape. My word list is on worksheet "Sheet1"
Sub showRandomWord()
Dim ws As Worksheet
Dim stRow As Long, endRow As Long, dataCol As Long
Dim dispRow As Long, dispCol As Long
Set ws = Sheets("Sheet1")
stRow = 2
dataCol = 3
dispRow = 2
dispCol = 4
With ws
endRow = .Cells(.Rows.Count, dataCol).End(xlUp).Row
.Cells(dispRow, dispCol).Value = _
.Cells(Application.RandBetween(stRow, endRow), dataCol).Value
End With
End Sub
Depending on your exact needs you might not even need VBA,
say you assign the name "data" to the range with the list
in the cell where you want to show the random value put the formula
=INDEX(data,INT(RAND()*COUNTA(data))+1)
Then a new random value will be shown each time the sheet is recalculated, you can do this manualy by pressing F9 key
If an actual button is required then use VBA , in the buttons event :
dim l as long
l=Sheet1.Range("data").count
Sheet1.Range("result")= Sheet1.Range("data").cells(fix((rnd*l)+1),1)
here i assume the list is in a range named "data", and the cell where you want the result is named "result"

How to search for an item and select it in VBA [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to search for a value in a cell in a spreadsheet and select it. Here is some pseudocode for what I want to do:
search for the unique value "Justin123" in spreadsheet
select the cell that contains the value "Justin123"
offset 2 cells to the left of the searched value and replace with new value "John123"
I tried to google a solution but all I found were recommendations on using the record macro feature to search and replace an item. However, what I need to do is slightly different since I need to search and replace the item two units to the left of the searched cell.
See if you understand this code (hope it gets you interested in learning VBA):
Sub TestReplace()
ActiveSheet.Cells.Find(What:="Justin123", LookAt:=xlPart).Offset(0,-2).Value = "John123"
End Sub
The Macro Recorder is a great tool for beginners. Please do use it for simple cells operations.
Basically, what you have to do is initialise a RANGE object (which represents a cell in excel). Then you set the range object to the output of the function .Find(), which returns a range object from the sheet that matches the search inputs. If there is no match, it sets the range object to Nothing. You can then use the method .Offset() that range objects have to traverse across the other range objects in the sheet.
Sub Main()
Dim rng As Range
Set rng = ActiveSheet.Cells.Find(What:= "Justin123", LookAt:= xlPart)
' Check if rng is not Nothing [ie a match was made using .Find()]
If Not rng Is Nothing Then
rng.Offset(0, -2).Value = "John123"
End If
End Sub

Using a Dashboard to copy particular rows in a particular columns and pasting them into a different excel document [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my excel spreadsheet I have a master which basically controls everything. In this master I have a page which allows me to select an xlsx file, then in a drop down it allows me to select which sheet in that chosen file that I want to use. [This part is working perfectly however...]
What i am struggling with is the following, The user must be able to stipulate which row the data starts and which row the data end and what column this data is in
example:
Row in which data starts 7
Row in which data ends 25
Column of the data G
Column of the Data code D
Using this information, i need excel to extract that data and copy it to another spreadsheet that the user has selected and merge them together
Column to input data H
Title of column (the code must name it with the input of the user) TITLE
Column of the data code E
When merging the data it must match the data to the "Data Code"
Thanks in advance
It sound as though you are asking us to design your program.
The user must be able to stipulate which row the data starts and which row the data end and what column this data is in. Example: "Row in which data starts 7 Row in which data ends 25 Column of the data G Column of the Data code D.
Another spreadsheet (workbook?) that the user has selected.
Only you know what your users will find convenient and what will match your existing code. Below I show one method of selecting a range that you might like.
Option Explicit
Sub Test()
Dim CopyRange As Range
Dim reply As Long
Do While True
Err.Clear
On Error Resume Next
Set CopyRange = Application.InputBox(Prompt:="Select ranges to be copied", _
Type:=8)
On Error GoTo 0
If CopyRange Is Nothing Then
reply = MsgBox(Prompt:="Do you wish to exit without copying a range?", _
Buttons:=vbYesNo)
If reply = vbYes Then
' User wants to exit
Exit Sub
End If
' Loop for another go
Else
' User had entered a valid range
Exit Do
End If
Loop
Debug.Print CopyRange.Address
End Sub
You tell us that you have already opened another workbook so you know how to work across multiple workbooks.
The easiest command, in my view, for copying data is:
SourceRange.Copy Destination:=TopLeftCellOfDestinationRange
The above should give you a start on the next sections of your macro. If you have problems come back with specific questions about code that does not work as you require. Please don't provide a list of vague requirements.