In VBA, I want to select a range of rows relative to the active cell. For example, select rows +3 to +5 relative to the active cell. I tried the following without success:
Rows(ActiveCell.Row+3:ActiveCell.Row+5).Select
Sub ActiveCellNewRange()
Dim rngArea As Range
'set new range
Set rngArea = Sheet1.Range("C5").Offset(3).EntireRow.Resize(5)
'Select and write to immeadiate window
rngArea.Select
Debug.Print rngArea.Address
End Sub
Related
This should have been easy, but im struggling.
Looking for a fast code to apply on user selected range.
For each row user has selected set new value in column x.
Try:
Sub Test()
Dim Rng As Range
Set Rng = Selection
For Each rr In Rng.Rows
Range("X" & rr.Row).Value = "new value"
Next
End Sub
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
I would like to create a function that copies certain excel ranges in worksheets and paste these ranges into a "motherfile".
Now, I am trying with this code:
Sub ranges()
Dim month As Variant
Dim months As Variant
months = Array("V01 DEN HAAG", "V02 AMSTERDAM")
Dim destinationRange As Excel.range
Set destinationRange = Sheets("DATASET").range("B3").End(xlDown).Offset(1, 0)
For Each month In months
Dim sourceRange As Excel.range
Set sourceRange = Sheets(month).range("H7", range("H7").End(xlToRight))
Call sourceRange.Copy
Call destinationRange.PasteSpecial
Next month
End Sub
But, I get an Application-defined or object-defined error. Any thoughts on what goes wrong? Thanks!
Adding to mielk's anwser the problem is in the codeline:
Set sourceRange = Sheets(month).range("H7", range("H7").End(xlToRight))
This is because if you are collecting from multiple sheets data and you use range("H7").End(xlToRight it will search for this on the active sheet. Therefor it can only find the correct range if its on the correct sheet.
by using the following code:
Set sourceRange = Sheets(month).Range("H7", Sheets(month).Range("H7").End(xlToRight))
it will work no matter which sheet is active at that moment.
another addition is you can copy and paste in 1 code line:
sourceRange.Copy Destination:=destinationRange
see below the entire code:
Sub ranges()
Dim month As Variant
Dim months As Variant
months = Array("V01 DEN HAAG", "V02 AMSTERDAM")
For Each month In months
Dim sourceRange As Excel.Range
Dim destinationRange As Excel.Range
With Sheets("DATASET")
Set destinationRange = .Cells(.Rows.Count, 2).End(xlUp).Offset(1, 0)
End With
Set sourceRange = Sheets(month).Range("H7", Sheets(month).Range("H7").End(xlToRight))
sourceRange.Copy Destination:=destinationRange
Next month
End Sub
The possibly reason for this error is that you don't have any values in worksheet "DATASET", column B, below 3. row.
Look at this line of code:
Set destinationRange = Sheets("DATASET").range("B3").End(xlDown).Offset(1, 0)
First it takes the range from cell B3 to the last cell in this column (B1048576 in Excel 2007+).
After that it tries to offset this range by one row down (so it tries to create a range having the same number of rows and columns but starting one cell below).
However, it is not possible, because such range would have to start in cell B4 and end in cell B1048577 and Excel has only 1048576 rows.
If you want to assign the first empty row to the variable destinationRange you should replace this code:
Set destinationRange = Sheets("DATASET").range("B3").End(xlDown).Offset(1, 0)
with the below:
With Sheets("DATASET")
Set destinationRange = .Cells(.Rows.Count, 2).End(xlUp).Offset(1, 0)
End With
Both those statements are similar. The difference is that the second one
starts from the last cell in the column B and look for the first non-empty
cell above.
I'm new to programming and need help.
I have an excel sheet that will have 8760 rows (for every hour in a year), in order to distinguish between each new day, I need to insert an empty row every 24 lines. I know I have to use a for loop but not sure what VBA syntax to use and so on.
This is what I have so far, I just don't know where to put the "every 24 lines condition":
For i = 1 to i = 8760
Worksheets("Sheet1").Cells(i, "A").Select
ActiveCell.EntireRow.Insert
Next
Thanks in advance
Just loop over the range of cells and use VBA's mod operator to check to see if you should be inserting a row. You may also want to read Modulo operator on wikipedia.
Public Sub InsertRowOnceEvery24Rows()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet 'adjust this to get the sheet you need
Dim rng As Range
Set rng = ws.Range("A1:A50") 'adjust this to whatever range of cells you need
Dim i As Integer
i = 1
Dim cell As Range
For Each cell In rng
If i Mod 24 = 0 Then
cell.EntireRow.Insert xlShiftDown
End If
i = i + 1
Next
End Sub
Using VBA I am selecting the first empty cell in a column using the following
Range("A3").End(xlDown).Offset(1, 0).Select
How can I set this currently active cell as a range for calling later in the macro.
I tried,
Dim Target as Range
Range("A3").End(xlDown).Offset(1, 0).Select
Target = Cells(Application.ActiveCell)
My apologies if this is a basic question.
If you mean the first empty cell at the end of the column then this is the slight variation required. I would counsel against using the variable name Target because this is also a VBA keyword.
Dim rng As Range
Set rng = Cells(Rows.Count, "A").End(xlUp).offset(1,0)
MsgBox rng.Address
You don't need to select the cell first and as you're assigning an object, you need to use Set like so:
Dim Target As Range
Set Target = Range("A3").End(xlDown).Offset(1,0)
Here it is:
Dim Target as Range
Set Target = Range("A3").End(xlDown).Offset(1, 0)