Avoiding Select with Copy and Paste VBA - excel

I am trying to reduce my usage of select. I have been referencing the article below but I am not entirely sure how to implement it in this scenario. To practice I have the working set of code below that I use to copy the value in B1 and paste it down as many rows that are filled in column A. If someone could coach me through how to avoid this bad habit in order to write more effective and efficient code, I would greatly appreciate it!
How to avoid using Select in Excel VBA
Range("B1").Select
Selection.Copy
ActiveCell.Offset(0, -1).Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Range(Selection, "B1").Select
ActiveSheet.Paste

Others have provided direct solutions to your code. But to your question on being "Coached" on how to avoid Select, this is what is happening...
Consider .Select as storing a Reference to whatever it's attached to.
Range("B1").Select
is saying store a reference to the Range: B1.
Selection.Copy
is saying use that reference, and call the function Copy
Instead of using the reference, you can access it directly.
Range("B1").Copy
Now since your copying and pasting you have two different references, your source and your destination. So you can apply the value directly from source to destination, without invoking copy and paste functions.
Range(<DestinationRange>).value = Range(<SourceRange>).value
The offset functions are simply saying Start at the Range specified, and move over the specified number of column(s) or row(s), and use this cell as your reference.

All of that code can be translated to this:
Range("B1:B" & Cells(Rows.Count, "A").End(xlUp).Row).Value = Range("B1").Value
We're making the Range of cells B1 through B and the last row of cells in column A (which we get by using Cells(Rows.Count, "A").End(xlUp).Row) equal to the value in cell B1.

You could do this:
Sub copyPaste()
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Sheet1").Range("B1:B" & lastRow).Value = Sheets("Sheet1").Range("A1:A" & lastRow).Value
End Sub

Related

cannot copy and paste when area size is not the same

Dim lastrow&, lastCol&, myarray As Range
lastrow = Range("A1").End(xlDown).Row
lastCol = Range("XX1").End(xlToLeft).Column
Set myarray = Range("A1").Resize(lastrow, lastCol)
Range("A1", myarray).Select
Selection.Copy
So basically, i am trying to get select an array which could vary, I know it starts at A1, but I'm unsure which row and column it will end at. Code above works fine to help copy this array.
Application.CutCopyMode = False
Selection.Copy
Application.WindowState = xlNormal
Windows("macrofile.xlsm").Activate
Sheets("MRG").Select
'has to find the last row by itself
Range("A" & Rows.Count).End(xlUp).Offset(2, 0).Select
ActiveCell.PasteSpecial (xlPasteAll)
I am getting an error on the last line ActiveCell.PasteSpecial (xlPasteAll).
Error 1004, can't paste because copy area and paste area aren't the same
I have tried different variations including activesheet.paste and xlpastevalues to no avail.
Range("A" & Rows.Count).End(xlUp).Offset(2, 0).Select selects a single cell in column A to find the last used row and offsets it by 2 rows so I can paste below the existing data. Not sure why error 1004 comes up because replicating selecting an array and pasting it into a single cell in excel runs no errors.
Any help is much appreciated; I am really new to VBA and most of this code came from different sources online.
As long as the source data has no blank rows or columns you can do this:
ActiveSheet.Range("A1").Currentregion.Copy _
Workbooks("macrofile.xlsm").Sheets("MRG").Cells(Rows.Count, "A").End(xlUp).Offset(2,0)
Assuming there's room for the pasted data.

Unable to copy all data in column to another sheet - excel VBA

I am unable to copy all data in a column. I would only want to copy data that does not include the first row, (headers), and paste it to another sheet. My current code gives me a partial result as there are some blanks in between the data.
Please help me out as I am new to VBA. Thanks!
Below are two codes that I have tried. One is through the xldown method and the other is the lastrow. I found that using the lastrow, I am not even able to copy anything at all.
This is the xldown method, which gives me partial data (wsRawT and wsDetI are my defined worksheets):
wsRawT.Select
range("AI1").Select
ActiveCell.Offset(1, 0).range("A1").Select
range(Selection, Selection.End(xlDown)).Select
Selection.copy
wsDetI.Select
range("B1").Select
ActiveCell.Offset(1, -1).range("B1").Select
ActiveSheet.Paste
This is the lastrow method, which does not even allow me to copy anything:
Dim lastRowTD As Long
lastRowTD = wsRawT.Cells(Rows.Count, "A").End(xlUp).Row
wsRawT.range("AU2" & lastRowRD).copy
wsDetI.range("A2").PasteSpecial xlPasteValues
wsDetS.range("A2").PasteSpecial xlPasteValues
There are only small mistakes in your code, try this:
Dim lastRowTD As Long
lastRowTD = wsRawT.Cells(Rows.Count, "AU").End(xlUp).Row
wsRawT.Range("AU2:AU" & lastRowTD).Copy
wsDetI.Range("A2").PasteSpecial xlPasteValues
To explain: This code checks how large the dataset in column A of your sheet wsRawT is. Then it copys everything from cell A2 down to the last row with data. Then all the values are pasted to column A of sheet wsDetI. If you don't specifically need the values only, you could also use this simpler version of .Copy:
wsRawT.Range("AU2:AU" & lastRowTD).Copy wsDetI.Range("A2")

How to Dynamically Autofill Cells

I would like to dynamically fill a column. However, with the VBA code I have now, the formula fills all the way to the max row count (1.04 million). I would like it to stop at the max rows found in the column to its left (same as manual auto fill)
Sub Vlookup()
Range("C2").Select
ActiveCell.FormulaR1C1 =
"=IFERROR(VLOOKUP(RC[-2],'C:\Test.xlsx'!R2C6:R1000C15,10,FALSE),"""")"
Selection.AutoFill Destination:=Range("C2", Range("C2").End(xlDown))
End Sub
I tried to use
Selection.Autofill Destination :=
Range("C2",Selection.Range("C2").End(xldown))
But that didnt seem to work either
You always want to use Long when finding the last row. Integers only go to 32,767 and there are over 1 million rows. You should also avoid using Select.
Dim lastRow as Long
lastRow=Cells(Rows.Count,2).End(xlup).Row
Range("C2").AutoFill Destination:=Range("C2:C" & lastRow)
first you can get the last row coming from column B and use that as reference for auto fill. see my example below.
Dim lastRow As Integer
Range("B1").Select
Selection.End(xlDown).Select
lastRow = Selection.Row
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C" & lastRow)

How can I use VBA to create an =AVERAGE formula in a dynamic cell with a dynamic range?

I'm trying to write a Sub that will allow me to replace a formula for every time the macro is run.
I'm able to select the range I want using:
Range("A3").Select
Selection.End(xlDown).Select
Range("B5", ActiveCell.Offset(-1, 1)).Select
And I'm able to find the cell in which I want the formula using:
Range("A3").Select
Selection.End(xlDown).Select
ActiveCell.Offset(2, 1).Activate
Is there a way I can make a formula that says =AVERAGE([selected range])?
Note: I do not want to just have the value in the cell. I need to have it so there is an active formula showing the results.
It would seem you are trying to do something like this:
Dim lrows As Long
lrows = Range("A3").End(xlDown).Row - 1
Range("A" & lrows + 1).Formula = "=AVERAGE(B5:B" & lrows & ")"
You should avoid using SELECTing and ACTIVATEing cells in your code, it slows it down and makes it less reliable.
You can also use the formula. worksheetfunction.Average("Select range")

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