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")
Related
I've been reading and trying to learn a bit more of VBA, but I can't figure out yet. This script is copying, pasting the source data to another sheet, but not to the first empty row in the existing table on the sheet ("DATA") like it shoud, but to the first row in the table on the sheet overwriting existing data over there.
-What am I missing?
-And how can I put a check(argument?) in this script when a cell in the copied range is empty that should have a value, pasting is not executed and gives a warning to add missing data.
Much regards.
Sub CommandButton1_Click()
Sheets("Invoer Cursisten").Range("B11:K40").Copy
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Sheets("DATA").Activate
Cells(lastrow + 2, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Worksheets("Invoer Cursisten").Range("B12:K41").Clear
End Sub
I'm trying to create a macro to copy cells down an entire column of a table in hopes of acting as a 'refresh' in case those formulas were altered or replaced.
I have multiple tables in the same sheet so I can't select the table name because they constantly change.
What I'm thinking of is having a bottom row with a keyword that VBA can select down until they hit the keyword and select those cells to copy the formulas down.
The thing is that I have multiple tables and they would all have the bottom row of keywords.
When I recorded a macro, I have to Control+Shift+Down multiple times to account for missing rows which I imagine wouldn't always be the case. This is what scares me for this macro since sometimes a table would have no missing data so the xlDown function would select more data than it should.
Here is what I recorded:
Sub Macro9()
'
' Macro9 Macro
'
'
ActiveCell.Offset(3, 2).Range("A1").Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
End Sub
Here is an example of the column I am trying to restore formulas on:
I want the formula below "Total Price" to fill down until it hits the word "Total". Note that formulas are hidden if there is no data elsewhere in the sheet.
There are multiple tables on this sheet so this would have to work in different sections of te same sheet.
If you have actual Tables/Listobjects then you could do something like this:
Sub FillDownFormulas()
Dim lo As ListObject, col As Range
For Each lo In ActiveSheet.ListObjects
For Each col In lo.DataBodyRange.Columns
If col.Cells(1).HasFormula Then 'first cell in column has a formula?
col.Formula = col.Cells(1).Formula 'fill formula to rest of column
End If
Next col 'next column
Next lo 'next table
End Sub
One of the most simple ways (for people that usually don't use VBA) is to get the number of the last row in your table. You can do that by counting values in table or with your own code by using a column that is always filled, like:
last_row = Range("B2").end(xldown).row
With last_row value you can fill your formula in ranges, like:
Range("C2").value = 'Your Formula here
Range("C2").AutoFill Destination:=Range("C2:C" & last_row)
You can do that to every column that you want.
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.
I’m trying to create VBA that allows me to copy the one of a cell and paste over until end of the row in the same column.
But I’m unable to figure out how this works, I have a total of 109 rows with 20 columns and in column “BD” that is an empty column and I want to put today date and fill up rest of the same column until end of row.
Please see the code at the bottom
Sub CopyInsert()
Range("BD2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("BD2").Select
Selection.Copy
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
ActiveSheet.Paste
End Sub
This code is working fine but I’m not getting the right result what these codes do is copy and paste over pass 109 rows mean is paste over up to around 2000 rows
My question is how can I copy and paste over until the end of the row like till row 109! If we have over 200 rows, how to copy and paste until end of the row, row 200 in one column! But not using (BD2:BD), I have tried (BD2:BD) and is paste over 109 rows is not stopped at the end of row.
many thanks.
So you need to find the lastRow based on the column where the actual data is...
Try something like this:
With worksheets("mySheet")
Dim lastRow as Long
lastRow = .Cells(.Rows.Count,1).End(xlUp).Row 'this uses column A for example
.Range("BD2:BD" & lastRow).Formula = "=TODAY()"
End With
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