Microsoft Excel VBA code range based on cell contents - excel

I have some VBA code, and I have a problem with a range.
In my excel sheet, I want the range to be based on the contents of cell C2.
Here is the VBA code,
Sub Repeat()
'
' Repeat Macro
'
'
Range("A1:A3").Select
Selection.AutoFill Destination:=Range("A1:A19"), Type:=xlFillDefault
Range("A1:A19").Select
Range("B1").Select
End Sub
However, instead of A19 I would ideally like to use the AX with X being the value in cell C2, if cell C2 contains 24 I would like it to say A24 instead of A19 any ideas?

Try
Range("A1:A" & Range("C2").value)
But you should qualify with the sheet name as well.
For example:
With Worksheets("Sheet1")
.Range("A1:A3").AutoFill Destination:= .Range("A1:A" & .Range("C2").value) , Type:=xlFillDefault
End With

Related

Autofill across and above a dynamic range

I'd like to autofill cells across and above a dynamic range.
I have a line of numbers in row 3 and would like to put the word "Customer No." in the cell above each one.
I do this by copying A2 and pasting into C2 then dragging across
Via VBA macro recorder the code I get looks like this
Selection.Copy
Range("C2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("C2:E2"), Type:=xlFillDefault
Range("C2:E2").Select
I was wondering if there's a way to create an autofill across a dynamic range as the number of cells in row 3 will change from time to time?
Thanks
You could do it like this:
With ActiveSheet
.Range("C3", .Cells(3, Columns.Count).End(xlToLeft)).Offset(-1).Value = .Range("A2").Value
End With

VBA Autofill Range Adjustment

I have recorded a macro in Excel, but when I run it with updated data, it only applies to the number of rows I had when I first had the macro recorded. I dove into the macro code and I think these are the problems:
Selection.AutoFill Destination:=Range("L2:R2"), Type:=xlFillDefault
Range("L2:R2").Select
Selection.AutoFill Destination:=Range("L2:R242")
Range("L2:R242").Select
How can I adjust the macro to have the range set up as the size of the worksheet?
Save the last row of your data into a variable and then modifiy the line to always fill the formula to that value like this:
Dim Last_Row as Long
Last_Row = Application.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("L2:R2").AutoFill Destination:=Range("L2:R" & Last_Row), Type:=xlFillDefault
(Rows.Count, 1) this 1 represents the Column A. I supposed your data is here

Excel Copy Cell Data from one sheet to another using VBA

I am new to VBA, I have to copy cell value from one sheet to another. The existing code was
'go to the team sheet and select col 3-5 on last row and copy
Sheets(strname).Activate
ActiveCell.Offset(0, -10).Select
Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.Column + 2)).Select
Selection.Copy
DoEvents
'select the col 2 on team line and paste
Sheets("dtl overview").Select
ActiveCell.Offset(0, -6).Select
ActiveSheet.paste Link:=True
DoEvents
The problem is , I have added one more column in the 'team' sheet. So the above copy script has to read one cell backward.
Say for example, the above code is reading the data from D,E & F cells. I dont know how...
I am looking for to change the above code to read the value from C,D&E.
Inputs are Welcome & Highly appreciable!
I don't know how you consistently copy from columns D:F using that code either.
What your code does is:
'Activate sheet indicated in the "strname" variable.
'"strName" must be set elsewhere in the code?
Sheets(strname).Activate
'When the sheet is activated a cell will already be selected on there.
'This will be whatever cell was active when the sheet was previously looked at.
'This could easily change if a user selects another cell.
'The "OFFSET" command looks at the same row and ten columns to the left of the ActiveCell.
'If the ActiveCell is not in at least column J (11th column) then this
'will throw an "Application defined or Object Defined error" as it will try and select
'a column before column A.
'The offset cell is then selected - hopefully it will be column D.
ActiveCell.Offset(0, -10).Select
'This will select a range from the ActiveCell plus 2 columns on the same row.
'Hopefully columns D:F
Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.Column + 2)).Select
'Copy the selection.
Selection.Copy
'Don't need this line unless other code you haven't included needs it.
DoEvents
'Select the "dtl overview" sheet.
Sheets("dtl overview").Select
'Again, whichever cell was last active on "dtl overview" and select the cell 6 columns to the left.
ActiveCell.Offset(0, -6).Select
'Paste a link to the original cells.
'So if you copied D4:F4 on the original sheet (which I'll call "Sheet1") then this will paste
'=Sheet1!D4 , =Sheet1!E4 and =Sheet1!F4
ActiveSheet.Paste Link:=True
'Definitely shouldn't need this now.
DoEvents
At the moment your code looks 10 columns to the left of whichever cell is currently active - so depends which cell you have selected when you run the code.
You don't say which row you want copying, so this code copies row 1 and pastes to cell D1.
Sub Test()
Dim strName As String
strName = "Sheet1"
'ThisWorkbook means the file containing this code.
Dim wrkSht As Worksheet
Set wrkSht = ThisWorkbook.Worksheets(strName)
'Cells(1,4) is row 1, column 4.
'Range(Cells, Cells) shows a start & end cell for the range.
With wrkSht
.Range(.Cells(1, 4), .Cells(1, 6)).Copy _
Destination:=ThisWorkbook.Worksheets("dtl overview").Cells(1, 4)
End With
End Sub
Further reading: With

VBA - How to Autofill all the way to the bottom until the last B cell with data

I recorded a macro and I tried to autofill column C with the COUNTIF function all the way down until B column has their last cell value. It doesn't work with new data so I tried to edit the macro and replace it with Range("D2:D") but that doesn't work.
Sub COUNTIF()
'
' COUNTIF Macro
'
'
Range("D2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-3],RC[-3])>1"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D")
End Sub
Better to find the last cell. Also avoid using select as it slows down your code.
Sub COUNTIF()
Dim LastRowB As Long
LastRowB = Range("B" & Rows.Count).End(xlUp).Row
Range("D2").FormulaR1C1 = "=COUNTIF(C[-3],RC[-3])>1"
Range("D2: " & "D" & LastRowB).FillDown
End Sub
Now change your "=COUNTIF(C[-3],RC[-3])>1" formula to whatever it is you really want to fill down.

Recorded Excel macro with countIf function with lastrow

My problem is following. I am recording macro for for sheet that´s range is dynamic.
The formula is =CountIF(A:A;A2) and recorded it:
Range("M2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-12],RC[-12])"
Range("M2").Select
Selection.AutoFill Destination:=Range("M2:M4333"), Type:=xlFillDefault
Range("M2:M4333").Select
Since the rows are dynamic, I want to end every formula to the lastrow. And I searched here for answer and copied this fuction to the beginning of the macro.
Function GetLastRow(sht As Worksheet, col As String) As Integer
GetLastRow = sht.Range(col & CStr(sht.Rows.Count)).End(xlUp).row
Now I am trying to call this to the orginal recorded macro
Range("M2").Select
Range(Selection, Selection.End(xlDown)).Select
Formula1 = "=COUNTIF(A:A" & GetLastRow(Sheets("Sheet1"), "A") & ",A2))"
I changed it looking like the formula that is copied in worksheet and not like it´s recorded. I would appreciate some help to this. It would be better if the lastrow function is used with recorded formula since I have lots other formulas to come with same problem.
You want to fill column M from row 2 down to the row in column M that meets the last populated cell in column A. Use the Range.Offset property and Range.Address property to get the correct address to use in the COUNTIF function.
With Worksheets("Sheet1") 'you should know what worksheet you are on!
With .Range("M2:M" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.FormulaR1C1 = "=COUNTIF(" & .Offset(0, -12).Address(ReferenceStyle:=xlR1C1) & ", RC1)"
End With
End With
        

Resources