My goal is to cut columns G through I (without header) and paste the data starting on the next available line under columns D through F. Basically, I want to append the columns that are adjacent to be beneath, instead. Then I need to copy columns A through C and paste the duplicate data right beneath itself.
This code actually worked for 4 files of mine, but it's now giving me an error, saying that I should paste to the same size rectangle or a single cell. I've tried soooo much code that I've found on boards and nothing has fixed this error. There are no merged cells.
All help is greatly appreciated!
Dim lastRow As String
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Range("G2:I" & LastR).Select
Selection.Copy
Range("D" & lastRow).PasteSpecial xlPasteValues
Range("A2:C" & LastR).Select
Selection.Copy
Range("A" & lastRow).PasteSpecial xlPasteValues
Try this comment below your second line activesheet.cells.unmerge
It will unmerge all cell in sheet.
Also dont forget to check value of last row and Lastr.
Smt likes this will work.
If lastrow<2 then
lastrow=2
End if
Related
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 am trying to merge 2 columns (Product 1 and Product 2) into a new column. However, I want to keep some data (Customer ID and location) from the original rows and copy them into the new rows. In my actual data, there are multiple "Product" rows which are not all located next to one another. Also, I need to copy the columns and rows into a brand new merged dataset from columns A-C to columns G to I, rather than just editing the old column to incorporate the new rows.
Sub CopySecondaryProduct()
Range("A:D").Copy Range("G:J")
Range("J:J").Select
Selection.Copy
Range("I:I").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial
Range("G:H").Select
Selection.Copy
Range("G:H").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial
End Sub
This is the code I used, but it returned the error that the copy and paste areas aren't the same size. Also, there would be a problem of the headings also copying into the new column. For this code the headings are in the first row because I deleted the "What I have" and "What I need".
Thank you
The error is caused by the fact that you copy the whole columns. Try this instead. It is not that much robust but it should work in this case:
Sub CopySecondaryProduct()
Dim LastRow As Long
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Range("A:C").Copy Range("G:G")
Range("A3:A" & LastRow).Copy Range("G" & LastRow + 1)
Range("B3:B" & LastRow).Copy Range("H" & LastRow + 1)
Range("D3:D" & LastRow).Copy Range("I" & LastRow + 1)
End Sub
I have spent the day trying to understand what is going on with my excel.. I am running some code which worked fine before, I modified part of it early up (but which still works fine), and now a Selection.Copy later on in the code has stopped working. Here it is :
Range("AE3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
I used break points to find the problem. After the first two lines of code, it is the data in column AE which is selected. When I move on to the last line (Selection.Copy), it is not the data in AE which is selected but the columns AA and AB. I have tried literally everything I can think of to try and fix this but can't find anything..
If I run the code up to this point of the code and do the selection and copying manually, it also copies the wrong cells (it copies AA and AB like when it's done with vba)
I would post screenshots of it but you can't put photos here it seems.
Thanks for your help!
Resolved:
I went through the code step by step and noticed that previously in the code I copied the data from columns AA and AB to lower columns. To do so I had selected the columns and then copied them. I changed that so that I selected only the data in the columns and not the columns themselves and copied the data. This change has made my code work. I'm not sure why this was effecting the later Selection.Copy, but it was in some way. Thank you everyone for their help!
As I've suggested in my comment, avoid using .Select & Selection, is usually bad practice and almost everything can be done in VBA without the need to use them. I understand those are a result of the recorder (which is a good place to start learning how to do certain things in VBA), just need to learn as well how to use the code generated by the recorder.
See if this helps (see comments in code as well):
Sub copyRange()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1") 'use a variable for the sheet you want to use
Dim lRow As Long
lRow = ws.Cells(Rows.Count, "AE").End(xlUp).Row 'get the last row at the desired column
With ws
.Range("AE3:AE" & lRow).Copy _
Destination:=.Range("AE3:AE" & lRow).Offset(0, -10) 'destination offset 10 columns to the left
'or alternatively specify the destination
'Destination:=.Range("U3:U" & lRow)
End With
'ALTERNATIVE to the above - copy values only
With ws.Range("AE3:AE" & lRow)
.Offset(0, -10).Value = .Value 'destination offset 10 columns to the left
'or alternatively specify the destination
'ws.Range("U3:U" & lRow).Value = .Value
End With
'2nd ALTERNATIVE to the above - copy values only
With ws.Range(ws.Cells(3, 31), ws.Cells(lRow, 31))
.Offset(0, -10).Value = .Value 'destination offset 10 columns to the left
'or alternatively specify the destination
ws.Range(ws.Cells(3, 21), ws.Cells(lRow, 21)).Value = .Value
End With
End Sub
Note the use of With statement, .Range(...) is not the same as Range(...).
In case you want to copy all in column AE try this:
Range("EA3:EA" & Range("EA" & Rows.Count).End(xlUp).Row)).Copy
And to paste you could use:
Range("U3").PasteSpecial (xlPasteValues)
Also, I strongly suggest you to read:
How to avoid using Select in Excel VBA
You could try:
Option Explicit
Sub test()
Dim LastRow As Long
'Create a with statement refer to the sheet where your data are
With ThisWorkbook.Worksheets("Sheet1")
'Find the LastRow of column AE
LastRow = .Cells(.Rows.Count, "AE").End(xlUp).Row
'Refer to the range starting from AE3 and ends at Lastrow
.Range("AE3" & ":AE" & LastRow).Copy
End With
End Sub
Results:
Right now I am doing this:
xlwb.Sheets(curSheet).Cells.Copy
xlwb.Sheets(curSheet).Cells.PasteSpecial (xlValues)
This lets me copy/paste the entire sheet in-place as-values.
However I technically only want to do this for all columns between row 1 and some row near the bottom, call it finalRow. There is some content after that I wish to leave as formulas.
How can I copy/paste the rows from 1 to finalRow as-value? I'm technically running this code from Access VBA but this code is for Excel.
You can use the following syntax
xlwb.Worksheets(curSheet).Rows(1 & ":" & lastRow).Copy
xlwb.Worksheets(curSheet).Range("A1").PasteSpecial xlValues
Application.CutCopyMode = False
Change the paste range as required.
Skip the clipboard when copying only values:
With xlwb.Sheets(curSheet).Rows("1:" & lastrow)
.Value = .Value
End With
Good afternoon,
After too many hours of researching the proper code for what I am trying to do, I am finally having to ask the experts here. I am terribly new to VBA (but now hooked on conquering it!).
I am trying to copy a range of 3 cells of data I enter daily (I enter the data into columns J:L), then paste it 7 times, every 7th row below (so, I am copy/pasting Monday data to the next 7 Mondays, in their respective rows below). Each day changes, and so will the paste location (Tuesday data will be copied, then pasted to the next 7 Tuesday rows below, etc.). The data I enter will always be columns J:L.
Then, once the data is pasted, I have a button in place that uses that data, and clears it. So, columns J:L are always clear, until I add the data to the next row of 3 cells.
Any help is appreciated, as I am simply stumped. I tried several variations of "lastrow", but haven't found the proper coding that works for me (from piecing together info from here, but failing to make it work).
I tried to add a snapshot here, but apparently, I'm too noob for that even :/
The idea seems simple, so hopefully there will be a simple solution.
Thank you for your awesomeness!
Edited (sorry for making my first run at this site so difficult :/ )
Sub CopyPaste()
'
' CopyPaste Macro
' copies and pastes range for 8 total weeks
'
Dim lastrow As Long
lastrow = Range("J" & Rows.Count).End(xlUp).Row
'
Selection.Copy
Range("J27").Select
ActiveSheet.Paste
Range("J34").Select
ActiveSheet.Paste
Range("J41").Select
ActiveSheet.Paste
Range("J48").Select
ActiveSheet.Paste
Range("J55").Select
ActiveSheet.Paste
Range("J62").Select
ActiveSheet.Paste
Range("J69").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
See if this helps. I foresee a problem though in that once you have copied values down the worksheet for say Monday, the last used row will be row 70 or something like that.
Sub x()
Dim i As Long, lastrow As Long
lastrow = Range("J" & Rows.Count).End(xlUp).Row
For i = 1 To 7
Cells(lastrow, "J").Resize(, 3).Offset(7 * i).Value = Cells(lastrow, "J").Resize(, 3).Value
Next i
End Sub