Changing values of a specific column within a range - vba

I want to set the values of a column within the current selected range. For example, the current selected range (which could vary) is A5:D10, I want the values in column B of the range to be "Something". I'm guessing it would be something like:
ActiveCell.Columns("B").Value="Something"
Thanks.

You could also use the Intersect() function...
Sub Intersection_Example()
Dim rngB As Range
Dim rngResult As Range
Set rngB = Columns("B")
Set rngResult = Intersect(Selection, rngB)
rngResult.Value = "Something"
End Sub

Related

Assigning two ranges from separate sheets to a variable

I have 3 worksheets and I am trying to assign a range to a variable on one of the sheets, based on ranges in two other different worksheets. Here is my code:
Sub Combine()
Dim range1 As Range
Dim range2 As Range
Dim ID As Range
Set range1 = Worksheets(3).Range("A2:A")
Set range2 = Worksheets(4).Range("A2:A")
Set newRng = Worksheets(6).Range(range1, range2)
End Sub
I'm getting back a
Run-time error '1004'
Any suggestions?
A range can't span multiple worksheets.
This may work, depending on what you ultimately need to do:
Set newRng = Worksheets(6).Range(range1.Address, range1.Address)
But, since these ranges have the same address in your example, I think what you want is not a Range object combining them, but some other data structure, like an array, collection, or dictionary.
NOTE Your ranges are not valid to begin with, Range("A2:A") is not valid, so you'll need to fix that. See here for reliable ways to find the "last" cell in a range. I've modified it to bring in the entire column A (except A1) but you will probably want to fine-tune that.
newRange will have to be a different data type for this to work without raising a Mismatch error, for example a Collection:
Sub Combine()
Dim coll as New Collection
Dim range1 As Range
Dim range2 As Range
Dim ID As Range
coll.Add Worksheets(3).Range("A2:A" & Rows.Count)
coll.Add Worksheets(4).Range("A2:A" & Rows.Count)
Set newRng = coll
End Sub
Or as an array of range:
Sub combine()
Dim newRange(1) As Range
Set r1 = Worksheets(3).Range("A2:A" & Rows.Count)
Set r2 = Worksheets(4).Range("A2:A" & Rows.Count)
Set newRange(0) = r1
Set newRange(1) = r2
End Sub
Using the array example above, you can then assign the values to another location, modify as needed:
Worksheets(4).Range("B1").Value = newRange(0).Value
Worksheets(4).Range("B2").Value = newRange(1).Value

Reference a Range from a Range Object

I'm trying to find a way to refer a range from another range for example,
A range that holds the cells "A5:A10", 6 cells are in that range. What is needed is the range next to it which is "B5:B10".How can I refer it when there is already a range object ("A5:A10" in this case") to the range next it.
Dim R As Range
Dim A As Range
Set R = R("A5:A10").Select
Set R =
'Code to refer to next column is here
Sorry this could be the wrong syntax to start off with , it's been a while since I coded in vba, it's just to clarify what's is needed solve this.
Try this:
Sub setRanges()
Dim ws As Worksheet
Dim rngA As Range
Dim rngB As Range
'set the worksheet -- Adjust the worksheet name as required
Set ws = ThisWorkbook.Worksheets("Sheet1")
'set the first range to a range in the worksheet
Set rngA = ws.Range("A5:A10")
' set the second range to an offest of the first range
' in this case, use an offset of one column, with the same row
' ... remember the offset command takes rows in the first parameter
' ... and the second parameter is for the columns
Set rngB = rngA.Offset(0, 1)
' so, zero row offset, i.e. stay in the same row
' and 1 column offset to get the rngB for one column to the right of rngA
rngB.Select
' don't use Select in your code. This is just to demo.
End Sub

search for multiple values - loop

I think this can only be done in VBA. I tried VLOOKUP, but no luck.
I want to search a whole sheet for certain values. These values come from a row in another sheet. Once each value is found, it looks to the top most row and pulls that value.
I might need to loop through each value in the row and search the sheet from there?
my thought/example:
Sheet1!A:A
1234
5325
6346
6342
look in sheet 2 for 1234. lets say its found in cell G5, it will then look at the value in G1 and input that into cell A1 on sheet 2. I'm sorry for making this really confusing.
Here's what I have starting out:
Sub FindValues()
Dim SearchRow As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchRow = Sheets("sheet2").Range("B:B")
getting error on the last line. run-time error '13': type mismatch
You need to Set a Range object. You cannot assign it to a string unless you are looking for a property that is a string like the Address property.
Dim SearchRow As String
Dim SearchRange As Range
SearchRow = Sheets("sheet2").Range("B:B").address
Set SearchRange = Sheets("sheet2").Range("B:B")
Please consider the following code for your requirements:
Sub FindValues()
Dim SearchRow As Range
Dim SearchRange As Range
Dim Cell As Range
Dim FirstFound As Range
' Set Search value
Set SearchRow = Sheets("Sheet1").Range("B:B")
Set SearchRange = Sheets("Sheet2").Range("A:K")
For Each Cell In SearchRow
Set FirstFound = SearchRange.Find(Cell.Value2)
If Not FirstFound Is Nothing Then
Cell.Offset(0, 1).Value2 = SearchRange.Cells(1, FirstFound.Column).Value2
Else
Cell.Offset(0, 1).Value2 = "No Value Found"
End If
Next
End Sub
Note that as per your description I assumed that the SearchRange was more than just 1 column.
Basically the script loops through each Cell in the SearchRow and looks for the value in the SearchRange. If it finds the value and returns a subrange representing cells with the search value and sets the value to the right of each Cell to the value of the top cell of the column where the value was found. Hope this serves your needs. Cheers.

Find cells with same value within one column and return values from separate column of same row

I want to find all the cells in Column L with a particular value and return the values in Column D of the same row as those cells found.
So far, I am only able to return one result, which would be the top most result in my list, but I want to find all the rest as well, which I don't know the code to use.
Just to further explain: Value in cell D11 is the value I want to find in Column L of sheet "Master List". Supposedly I find the value in cells L13, L15 and L20, I want to return the value in cell D13, D15 and D20 into cells "C37:C39" of ws. Note: no. of cells that have the value may vary so the values returned will just appear from C37 downwards (something like automatic multiple selection, copy and paste)
Here's a little something to start the ball rolling:
Sub FindRelatedProducts()
Dim cell As Excel.Range
Dim D11Value As Variant
Dim D11Row As Variant
Dim ws As Worksheet: Set ws = Sheets("RShip")
Set cell = ws.Range("D11")
D11Value = cell.Value
With Sheets("Master List")
D11Row = Application.Match(D11Value, .Range("L:L"), 0)
If Not IsError(D11Row) Then
ws.Range("C37") = .Range("D" & D11Row).Value
End If
End With
End Sub
Here's an example using range variables.
You'll want to define a range for the input data range and a range for the output data. Then in the VBA you will want to change the wrk, inRng and outRng variables to be the named ranges you defined and change the column indexes in the for and if blocks to match the column index of the data you are looking for.
Option Explicit
Option Base 1
Sub FindValues()
Dim wrk As Worksheet
Dim inRng As Range
Dim outRng As Range
Dim cntr As Long
Dim outCntr As Long
Dim findVal As Double
Set wrk = Worksheets("Data")
Set inRng = wrk.Range("LookupRange")
Set outRng = wrk.Range("OutputRange")
' Clear the output range in case you have fewer values on this run than on the previous one
outRng.ClearContents
' Set the value you are looking for
findVal = 1
' Iterate through the rows in the input range. If you find the result you want then write it to the output range
For cntr = 1 To inRng.Rows.Count
If inRng(cntr, 1) = findVal Then ' Assumes the value you are finding is in column 1 of the input range
outRng(outCntr, 1) = inRng(cntr, 2) ' Assumes the values you are exporting is in column 2 of the input range
outCntr = outCntr + 1
End If
Next cntr
End Sub

Use VLOOKUP in a dynamic range using VBA

I'm trying to set up VB so I can do a VLOOKUP on a dynamic range of values. It'll always start at the same place, but it may end further down based on whatever value is in H4.
EDIT: Here's the code and it works.
Thank you Alex!
Public Sub State()
Dim refRng As Range, ref As Range, dataRng As Range
Dim i As Variant
Dim count As Integer
i = Sheet2.Range("H1").Value
i = i + 3 'offset of when to start
Set refRng = Sheet2.Range("D8:" & Cells(8, i).Address) '//horizontal range of look up values
Set dataRng = Sheet13.Range("A:C") '//data block you want to look up value in
For Each ref In refRng
ref.Offset(1, 0) = Application.WorksheetFunction.VLookup(refRng, dataRng, 2, True)
Next ref
End Sub
This may help:
Sub LookUp()
Dim refRng As Range, ref As Range, dataRng As Range
Set refRng = Worksheets(1).Range("D8:F8") //horizontal range of look up values
Set dataRng = Worksheets(2).Range("A1:B4") //data block you want to look up value in
For Each ref In refRng
ref.Offset(1, 0) = WorksheetFunction.VLookup(ref, dataRng, 2, 0)
Next ref
End Sub
Here you set up references to your lookup values and the data you want to query. Then just iterate over the values in the horizontal range and look up the value.