Copy range to last row and paste in another worksheet - excel

I need vba code to copy everything starting from A5, everything to the right of it and down to the last row in one workbook and paste it into another Workbook, starting at A5.
This is the code that I have so far, but it's not working.
Windows("Month By Month Income Statment 10.xlsx").Activate
Sheets("Month By Month Income Statmen-A").Select
Range("A5").Select
Dim EC As Long
Dim X As Long
Dim Y As Long
X = Range("A5").End(xlUp).Offset(1, 0).Row
EC = Range("A5").End(xlToLeft).Offset(0, X).Column
Range("A5" & EC).Select
Selection.Copy
Windows("RPG - Apr Mnth acs by co.xlsm").Activate
Sheets("010 - RPL").Select
Range("A5").Select
ActiveSheet.Paste
End Sub

I noticed you had another question to answer, so I had a go at this one too.
I know it wasn't in your original request, but I've added a section that clears the contents of the "RPG" file first (Only from row 5 downwards), so that you won't run in to any problems. This way you will always have a blank page to paste your new data in to and you'll never have data left over from last time if your new data is smaller than your old data. If you don't need this bit feel free to leave it out.
Sub Get_New_Data_From_Other_Workbook()
'
' This macro will copy data from a .xlsx file and paste it back into the .xlsm file
' Any contents in the .xlsm file will first be deleted
' Clear the existing contents of the destination sheet
Windows("RPG - Apr Mnth acs by co.xlsm").Activate ' Make sure the RPG file is selected
Sheets("010 - RPL").Select ' Select the required sheet
Range("A5").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select ' From A5, select every cell until the end of the page (up to where the data stops)
Application.CutCopyMode = False
Selection.ClearContents ' Delete contents
Range("A1").Select ' Select A1 for presentation purposes
' Go to the correct sheet of the other workbook and copy the data
Windows("Month By Month Income Statement 10.xlsx").Activate ' Select the other workbook
Sheets("Month By Month Income Statmen-A").Select ' Select the sheet with the data on
Range("A5").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select ' From A5, select every cell until the end of the page (up to where the data stops)
Selection.Copy ' Copy the data
' Come back to the macro workbook and paste the data in A5 of the required sheet
Windows("RPG - Apr Mnth acs by co.xlsm").Activate ' Select the RPG file
Sheets("010 - RPL").Select ' Select the required sheet
Range("A5").Select ' Select cell A5
ActiveSheet.Paste ' Paste the data
Range("A1").Select ' Select A1 for presentation purposes
End Sub

Related

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

Excel Macro does not display properly

I have a macro that is supposed to copy the format of a row and insert a new row with the same format.
Here is the macro code:
Sub Insertion_ligne_verrouillée()
'
' Insertion_ligne_verrouillée Macro
ActiveSheet.Unprotect
ActiveCell.Offset(-1, 0).EntireRow.Copy
Rows(ActiveCell.Row).Insert Shift:=xlDown
On Error Resume Next
Rows(ActiveCell.Row).SpecialCells(xlCellTypeConstants).ClearContents
ActiveSheet.Unprotect
'Application.CutCopyMode=False
End Sub
Now i am not the one that wrote the macro and honestly my VBA is quite rusty (also not that good in VBA either). The problem i am having is the user is using the macro by selecting a row and using ctrl+L.
It does copy and insert a row with the right format, however some rows afterward seem empty (all blank and no row number) so you have to select the row > right click > display, for it to display properly
Not sure what to look for
The following code makes a new row below the row you want to copy then copies the format of the row and paste into the new row.
Sub Insertion_ligne_verrouillée()
'Make a new row below active cell
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown,
CopyOrigin:=xlFormatFromRightOrAbove
'Copy the active row
ActiveCell.EntireRow.Copy
'paste format into new row
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End Sub

Create Macro to Copy, Paste for multiple Data sheets

Below is just a macro I recorded myself which may or may not be helpful. Basically, I am trying to copy the number, name, and date from one sheet (Worksheet) then paste into a different spreasheet (Data Entry Form).
FYI, the number, name, and date goes from left to right on the ‘Worksheet’, and Each row of data (number, name , and date) needs to go into a separate ‘Data Entry Form’.
So I am looking for a macro that will use the data from “Worksheet” and place it into the ‘Data Entry Form’ while generating additional ‘Data Entry Forms’ (more worksheets) for each separate row of data.
The number of rows in the Worksheet can vary from 10-100 so having a macro would save me ample time of copying and pasting into new forms; even if the macro can only do one row of data at a time.
Sub Popsecform()
'
' Popsecform Macro
'
' Keyboard Shortcut: Ctrl+m
'
Selection.Copy
Windows("Data Entry form.xlsx").Activate
Range("F8").Select
ActiveSheet.Paste
Windows("Worksheet.xlsx").Activate
Range("C2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Data Entry form.xlsx").Activate
Range("F30").Select
ActiveSheet.Paste
Windows("Worksheet.xlsx").Activate
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Data Entry form.xlsx").Activate
Range("F24").Select
ActiveSheet.Paste
End Sub
Edit
I am just trying to create a workbook that will be able to generate my Data Entry Form as an additional sheet to the workbook every time I add another row of Name, Number, and Date to the first Worksheet.
Row A goes to Data Entry Form 1 (Distinct cells)
Row B goes to Data Entry Form 2 (same distinct cells)
and so on

Excel VBA to copy

My task is to create a searchable database within Excel with an entry form. I need a macro to take the data from the entry form move to the database sheet offset the active cell down 1 row and copy the values only(not the formatting)
Every time I try to run the macro I get a run-time error in the code. I have no experience with VB or VBA; please tell me what is wrong with this.
Sheets("Database").Select 'Navigates to Database worksheet
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If 'Clears filters
Sheets("Entry Form").Select 'Navigates back to Entry Form worksheet
Range("E10:L10").Select ' select date, period, and data
Selection.Copy
Sheets("datatable").Select ' navigate to datatable tab
Range("A1").Select
Selection.End(xlDown).Select ' ctrl-down to last occupied row,
ActiveCell.Offset(1, 0).Select ' then one more to first blank row
Selection.PasteSpecial Paste:=xlPasteValues
'Pastes data as values only into the Database worksheet
Sheets("Entry Form").Select 'Navigates to Entry Form worksheet
Application.CutCopyMode = False 'clears copy data from clipboard
Range("E10, L10").Select
Selection.ClearContents 'Clears data from drop down selections
Range("E10").Select 'Returns selection back to Date entry box
It goes the very bottom of the next page and gives a 1004 error.
You need more than just a column label in A1 if you are going to use xlDown. There has to be at least one more value in column A or you will traverse to the bottom of the worksheet. It is usually better to look from the bottom of the worksheet upwards and then offset one row down.
With Sheets("Database") 'Primarily use Database worksheet
If .FilterMode Then .ShowAllData
With .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) 'look from bottom up then down 1 row
'direct value transfer is faster than Copy, Paste Special, Values
.Cells.Resize(1, 8) = Sheets("Entry Form").Range("E10:L10").Value
End With
End With
With Sheets("Entry Form") 'Primarily use Entry Form worksheet
.Range("E10:L10").ClearContents 'Clears data from drop down selections
.Range("E10").Select 'Returns selection back to Date entry box
End With
This makes use of the With ... End With statement to control which worksheet is receiving attention. See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Copy a row from Sheet3, paste into Sheet1, save as file, then go to the next row of Sheet 3

I am trying to find a way to streamline a very repetitive task.
This is the first time I am trying to build a proper macro, so things are confusing for me.
Below is an attempt to make it work.
Sub test()
Dim r As Range, j As Integer
Set r = Range("A2:C500")
Do
Sheets("Sheet1").Range(r.Offset(1, 0)).Select
Selection.Copy
Sheets("Sheet1").Select
Range("D2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("D2:F494")
Range("D2:F494").Select
ActiveWorkbook.SaveAs Filename = j, FileFormat:=xlUnicodeText, _
CreateBackup:=False
If r.Offset(1, 0) = "" Then Exit Do
Loop
End Sub
I am trying to
- copy Row A2:C2 from Sheet 3
- paste it into D2 of Sheet 1
- drag that value all the way down to the end of Sheet 1
- Save Sheet 1 as text file (any file name is fine. I was trying to save as 1, 2, 3, and so on.)
- Then go to the next row of Sheet 3 (A3:C3) and repeat the process until it reaches the last row, A500:C500.
When I recorded the macro for just the first row, it looked as follows:
Sheets("Sheet3").Select
Range("A2:C2").Select
Selection.Copy
Sheets("Sheet1").Select
Range("D2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("D2:F494")
Range("D2:F494").Select
ActiveWorkbook.SaveAs Filename:= _
"D:\Users\XXX\Desktop\XXX. XX\1.txt", FileFormat:=xlUnicodeText, _
CreateBackup:=False
ANY help would be greatly appreciated!
It's not clear what you want to do. Sounds like you are saying you want to copy a row (or some cells in a row) from one sheet to a particular single cell on another sheet and then export that sheet to a text file. This seems strange. Why not just write a macro to get all the data you want and directly write it to file in the first place? Do you just want all the data from all rows of the first three columns of "sheet3" exported to a file? Or is there boilerplate stuff in the other parts of "sheet1" that you want to be in every on of these text files?
Or do you want to create 500 text files, each with a row from sheet3?
You are saving the ActiveWorkbook so it won't save the sheets as individual files. You need to create a new workbook for each set of data using Workbooks.Add, then save and close each of these new workbooks once you've copied the data.
(A Worksheet has a SaveAs method, but it doesn't work - it doesn't save an individual sheet from a workbook.)
You can fill the entire area using Paste or PasteSpecial without having to fill it down:
Worksheets("Sheet3").Range("A2:C2").Copy
Worksheets("Sheet1").Range("D2:F494").PasteSpecial xlValues
Application.CutCopyMode = False
I'm afraid the recorder will only get you so far, and you'll need to study and modify the code that it creates.

Resources