I now have a range A2:A10
But I need to select A1, how can I do that?
Thanks in advance!
Edit:
Sorry I should be more specific. It's part of a loop so the range is dynamic.
The following code solved my problem. I needed this to work for any columns not just A2:A10.
Sub test2()
Dim myRange As Range, desiredRange As Range
Set myRange = ThisWorkbook.Worksheets("Sheet1").Range("A2:A10")
Set desiredRange = myRange.Resize(1, 1).Offset(-1)
desiredRange.Select
End Sub
There is no loop shown in your code. If you repeatedly set the variable myRange using a single column then you simply use
myRange.Cells(1,1)
to refer to the first cell
set the range then loop item in range and select first item:
Dim Cell As Range, ws As Worksheet
Set ws = ActiveSheet
For Each Cell In ws.Range("A2:A10")
Cell.Select
exit for
Next Cell
Related
I hope someone can help me with this as it's driving me up the wall!
There are 5 non-contiguous cells in a worksheet that I want to copy to the next empty row on another worksheet whilst retaining the number formatting (which varies). I have this so far but am struggling working out how to retain formatting. Can anyone please help? Thanks I anticipation.
`With wsCalc
For bRun = 1 To 4
bData(bRun) = Application.Choose(bRun, .Range("g2"), .Range("b2"), .Range("R2"), .Range("Q14"))
Next bRun
End With
wSResults.Cells(Rows.Count, "a").End(xlUp).Offset(1).Resize(, 4).Value = bData
`
Here's a possible solution, using your hard-coded cell addresses. You will have to set wsCalc and wsResults to their proper worksheets. Slightly more elegant would be to define a "non-contiguous" range on your wsCalc sheet (select the 1st cell, keep Ctrl pressed and select the next one etc, then type a name in the drop-down box just to the left of the formula bar).
Option Explicit
Sub CopyWithFormat()
Dim wsCalc As Worksheet
Set wsCalc = ActiveSheet 'Or whatever your calc sheet is
Dim rngSource As Range
Set rngSource = wsCalc.[G2,B2,R2,Q14]
Dim wsResults As Worksheet
Set wsResults = ActiveSheet 'Or whatever your result sheet is
Dim clDest As Range
Set clDest = wsResults.Cells(Rows.Count, "a").End(xlUp).Offset(1)
Dim cl As Range
For Each cl In rngSource.Cells
clDest.Value = cl.Value
clDest.NumberFormat = cl.NumberFormat
Set clDest = clDest.Offset(1)
Next cl
End Sub
Instead of using .Value, try .Text. It retains formatting. See below.
Gary's Student is right, text is read only, it should be used for the input not the output.
bData(bRun) = Application.Choose(bRun, .Range("g2").Text, .Range("b2").Text, .Range("R2").Text, .Range("Q14").Text)
I also agree with other answer the entire code could be set up more straight forward.
I have a selected range that I defined as my range. I want to get the Sum of this selection in a specific cell.
The makro shall find "x", select the cell below and put in "Sum" + the range I defined in "myrange"
Sub more_twelve_months()
Dim myrange As Range
Set myrange = Range(Range("F5"), Range("F5").End(xlToRight))
Set more_twelve_months = Range("A1:ZZ10000").Find("x")
more_twelve_months.Select
FormularCell = ActiveCell.Offset(1, 0).Select
Selection.Resize(Selection.Rows.Count, _
Selection.Columns.Count).Select
ActiveCell.Sum (myrange)
I tried several ways to get the sum, ActiveCell.Sum (myrange) is just the last thing I tried.
Any ideas how I can solve this?
Is this what you want?
You should avoid using Select as far as possible.
Sub more_twelve_months()
Dim myrange As Range
Dim more_twelve_months As Range 'declare your variables
Set myrange = Range(Range("F5"), Range("F5").End(xlToRight))
Set more_twelve_months = Range("A1:ZZ10000").Find("x")
If Not more_twelve_months Is Nothing Then 'check you've found something to avoid errors
more_twelve_months.Offset(1).Value = Application.Sum(myrange)
End If
End Sub
So I am trying to use the autofill method in vba right now over a range that is set to a variable. I know that the range of cells you are autofilling from must be included in the destination. So, I do just that. However, and much to my surprise, all the cells in the range are being set to nothing.
Here is the code:
Dim table2Range As Range
Dim table2Range2 As Range
Dim table2Range3 As Range
Dim tableholder As Range
Set table2Range2 = Range("Y54").End(xlToRight).Offset(0, 1)
Set table2Range3 = Range("Y77").End(xlToRight).Offset(0, 1)
Set table2Range = Range(table2Range2, table2Range3)
Set tableholder = Range("y54", table2Range3)
tableholder.Select
table2Range.AutoFill Destination:=Selection 'This is setting all my cells to nothing for some reason
Here is the before & after screenshots:Before, After
Any help is hugely appreciated!
Your AutoFill line of code should be:
SourceRange.AutoFill Destination:FillRange
It seems like your "table2Range " is the source, which overlap with your Fill Range. I.e:
.End(xlToRight).Offset(0, 1) will set range from Y54 to the rightmost cell of the same row.
You can edit the source range with hardcode first (i.e. Range("Y54:Z77") and see if that works for you, then work from there
For example (try on a new workbook):
Sub example()
Set SourceRange = Worksheets("Sheet1").Range("A1:B1")
Set fillRange = Worksheets("Sheet1").Range("A1:G1")
SourceRange.AutoFill Destination:=fillRange
End Sub
Enter "1" in cell(A1) and "2" in Cell(B1) and run the code.
I am having a lot of trouble with this one. I want to replicate this value in VBA:
=MAX(MAX(Named_Range1),MAX(Named_Range2),MAX(Named_Range3))
I am having an impossible time trying to get this done.
If the three ranges are in the same sheet then:
Sub MaxMax()
Dim r As Range
Set r = Union(Range(Named_Range1), Range(Named_Range2), Range(Named_Range3))
MsgBox Application.WorksheetFunction.Max(r)
End Sub
If you don't want to place the formula in one of the cells and calculate the result in VBA code, you can also use the following code:
Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim y As Double
Set rng1 = ThisWorkbook.Names("Named_Range1").RefersToRange
Set rng2 = ThisWorkbook.Names("Named_Range2").RefersToRange
Set rng3 = ThisWorkbook.Names("Named_Range3").RefersToRange
y = WorksheetFunction.Max(rng1, rng2, rng3)
If named ranges are in another workbook, replace ThisWorkbook with Workbooks("workbook name")
For the result to be placed in F9, please try:
Sub Maximal()
Range("F9").Formula = "=MAX(MAX(Named_Range1),MAX(Named_Range2),MAX(Named_Range3))"
End Sub
I'd like to create a macro that selects a rectangular range of cells and sets the name of every one of those cells to the value/contents of the cell.
In terms of what I've thought so far, I get an error though with the cell.Name line.
Public Sub NameCell()
Dim rng As Range
Dim cell As Range
Set rng = Range("A1:D1")
For Each cell In rng
cell.Name = CStr(cell.Value)
Next
End Sub
Is this what you meant?
Sub setVal()
Range("A1:C6").Select
Selection = "value"
End Sub
I believe this may work for you unless I also misunderstood the question.
Dim r As Range
Dim cell As Range
Set r = Sheet1.UsedRange
For Each cell In r
Sheet1.Names.Add Name:=cell.Value, RefersTo:=cell
Next
Keep in mind, though, that you would want to check that the cell.Value is valid (no spaces, etc.) for a named range.
To replace a range of cells with their values (removing any formulas from the range), you would use something like this.
Public Sub NameCell()
Dim rng As Range
Set rng = Range("A1:D1")
rng.Value = rng.Value
End Sub