vba to select a range that is written in a cell - vba

I have made a formula to updates the range I need, so for instance cell A1 will say A1:B9 (always changing when new data is downloaded).
I need to know how to get my VBA to look at cell A1 and use that as the range, e.g.
ActiveSheet.Range ("a1").select
to show a range of A1:B9 and not just a1 as a range.
Is this possible??

Like this:
Dim rng as Range
With ActiveSheet
Set rng = .Range(.Range("A1").Value)
End With

Related

Autofill One Cell VBA Excel

I am trying to autofill an equation only one column to the right of the active row and am having trouble. For example, there is an equation in I5, I want to have a macro that will auto fill J5 and only J5.
Thanks,
Becca
My deduction from your limited data is that you need the below solution. (Please do reply with details if you need more functionality than this.)
Public Sub FormulaPopulation()
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Sheet1.Range("J2") ' Have assumed over here that the formula that you need autofilled is in "J2" which is an unrelated cell
Set rng2 = Sheet1.Range("J5") ' Then since you only need this cell filled we take this cell.
rng2.Formula = rng1.Formula ' Finally it is formula matched, rather than Autofilling. Much faster than autofill.
End Sub

Vba clear contents of a certain range or cell

How can I clear the column and row starting with a reference cell?
I used
For x = 1 To 20
Sheets(1).Columns(x).ClearContents
Next x
But I want to be able to clear the contents of all rows and columns starting with row A6 as a reference point. I cant use range since the data is dynamic and changes upon insertion of data. The data came from a csv
Since your question states "I want to be able to clear the contents of all rows and columns starting with row A6 as a reference point." Then here is a one liner using SpecialCells(xlLastCell)
ActiveSheet.Range("A6", ActiveCell.SpecialCells(xlLastCell)).Clear
If your range starts at A6 and is continuous with no blanks in Row 6 and no blanks in Column A, you could set the range like this:
'Create variables
Dim wb as Workbook
Dim ws as Worksheet
Dim rng as Range
'Initialize variables
set wb = ActiveWorkbook
set ws = ActiveSheet
lastrow = ws.Range("A6").End(xlDown).Row
lastcol = ws.Range("A6").End(xlToRight).Column
'Set the range
set rng = ws.Range(Cells(6,1),Cells(lastrow,lastcol))
'Clear contents
rng.ClearContents
This uses Range.Endproperty of the Range object. It's basically like clicking on A6 and hitting ctrl+right on your keyboard and then returning the range.
Note: If there are gaps in the range you won't get the correct result.

Adding a single cell to a Range in a formula

I have a Sub that dynamically selects a range of cells and then passes that range in to the Internal Rate of Return Formula. I need to have a single cell appended to the beginning of the range for the formula to work. See below:
Dim calcrange As Range
Set calcrange = Range(Range("B57"), Range("B57").End(xlToRight))
Range("IRR").Formula = "=IRR(" & calcrange.Address & ")"
So ideally, what I'd like to do is add a named range cell "InvestmentOutlay" to be the first cell in the range. The InvestmentOutlay Cell is on a previous sheet, but the order I'd like the formula to run through is
=IRR(InvestmentOutlay,B57,C57, etc)
... Is this possible to do?
Like this, using Union to join two ranges?
Set calcrange = Union(Range("InvestmentOutlay"), Range(Range("B57"), Range("B57").End(xlToRight)))

Excel Database data issue

I have a farily large database of around 2000 people, sheet1 has all of their names and relevant details. Sheet 2 has data pulled on from a site. I would like the data from sheet 2 to auto populate the cells in Sheet 1. Also if the person does not exist in sheet1 to highlight the data it couldnt do. I am so stuck on this.
Sub dup()
Dim cell As Range, cella As Range, rng As Range, srng As Range
Set rng2 = Sheets(2).Range("A2:E2000")
Set rng3 = Sheets(3).Range("A2:E29000")
For Each cell In rng2
For Each cella In rng3
If cella = cell Then
cella.Interior.ColorIndex = 6
' cella.AddComment.Text Text:="duplicate value"
End If
Next cella
Next cell
Set rng2 = Sheets(2).Range("T2:Y2000")
Set rng4 = Sheets(4).Range("A1:F2000")
For Each cell In rng2
For Each cella In rng4
If cella = cell Then
cella.Interior.ColorIndex = 6
' cella.AddComment.Text Text:="duplicate value"
End If
Next cella
Next cell
End Sub
Its hard for me to show as it has a lot of columns not sure how on earth i can show you what im trying to do? :(
Try https://filetea.me/t1sfGPWECvdQqmgVDGtXL4oRQ
Maybe, if you want to do it without vba, you could use the LOOKUP function in the sheet 1's auto populate column. It works like that:
=LOOKUP(sheet1!A2, sheet2!table[a], sheet2!table[b])
This will find the value in the column "b" of the table in sheet2 based on the values of column "a". This will chose the value in the same row were column "a" matches the value in sheet1's A column. Let me know if I wasn't clear enough here.
Then you can use Conditional Formatting rules for the highlight you said. I suggest the COUNTIF function, that will return 0 if no matching value is found in the specified range.
=COUNTIF(A2:A5,A4)
This, for example, cont values in A2:A5 that matches the values in A4.
Also, you will find the conditional formatting tools in the home tab, if you are using excel 2016.
See the link for more information:
Information you may need

VBA Copying cells to another worksheet where cells have the same value

I have two worksheets I'm working with with long serial numbers as the cell values. One sheet (Sheet1) has a list of each individual serial number hyperlinked to a webpage referring to that item. This list ranges from A1:A31.
The second sheet (Sheet2) has a massive list of those same serial numbers, but in range G1:G102. The difference is that this list is not hyperlinked, and the serial numbers sometimes show up multiple times. There are also some areas where a cell is blank, so it splits up the continuous column of data.
I would essentially like to write a macro that takes the first list in Sheet1, and for each cell, it compares it to each cell in Sheet2 column G. Then, if the values match, I would like to copy the hyperlinked cell from Sheet 1 and paste it to that cell with the same value in Sheet2. Therefore, Sheet2 column G now has a fully populated list of hyperlinked serial numbers.
Can anyone help me with this? This is what I have so far...doesn't seem to work:
Sub CopyHyperlinks()
Dim cell As Excel.Range
Dim myRange As Excel.Range
Dim newRange As Excel.Range
Set myRange = Excel.ThisWorkbook.Sheets("Contents").Range("A1:A31")
Set newRange = Excel.ThisWorkbook.Sheets("Sheet1").Range("G1:G102")
For Each cell In myRange
If myRange.Cells.Value = newRange.Cells.Value Then newRange.Cells.Value = myRange.Cells.Value
Next cell
End Sub'
See this little function. Put this:
Function GetHyperLinkAddress(rng As Range) As String
Dim hl As Hyperlink
For Each hl In rng.Parent.Hyperlinks
If hl.Range.Address = rng.Address Then
GetHyperLinkAddress = hl.Address
Exit Function
End If
Next hl
GetHyperLinkAddress = "Not Found"
End Function
in a module. In the Spreadsheet, add
=GetHyperLinkAddress(Cell#)
Next to the cells with the hyperlink. You could then just use a vlookup to match.