Copy range until empty row and paste into new sheet - vba

I'm trying to write a VBA macro for Excel 2013. It's purpose is to merge two worksheets into a combined worksheet. (I tried to find a built in feature to do this but was unable to find what I needed).
What the macro needs to do is this:
Activate "Sheet3" and clear all rows starting with row 3 and down
Go into "Sheet1" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Go into "Sheet3" and paste those rows starting at A3.
Go into "Sheet2" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Go into "Sheet3" and past those rows starting at the first empty cell in column A.
I'm a novice at VBA but I've managed to find the following code and I'm trying to make it work to accomplish the above requirements.
Sub CreateCombinedSheet()
lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("A3", ActiveSheet.Cells(lastRow, 12)).Copy
End Sub
I'm trying to write parts of it and test it as I go but I'm already getting a 1004 error with this:
Application-defined or object-defined error
Any thoughts on how I should work this?
Thanks

Activate "Sheet3" and clear all rows starting with row 3 and down
You do not need to activate a sheet to clear the rows. You may want to see THIS You can directly say
Sheets("BlahBlah").Rows("3:200").ClearContents
I have hardcoded 200 as an example. To find the end of rows, see the below point.
Go into "Sheet1" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Same for this. You do not need to go to that sheet. You need to first find the last row. I see that you are hardcoing the rows in your code. You don't need to do that. xl2007+ now has 1048576 rows. Use .Rows.Count Please see THIS
Go into "Sheet3" and paste those rows starting at A3.
To paste, you again don't need to go to that sheet. You can directly say
rng.Copy Sheet("BlahBlah").Rows(3)
Your point 4 and 5 are just variations of the above. I am sure you can now take it from here :). In case you still face any difficulty, simply post back.

Related

Copy relevant rows and paste to new worksheet

I have multiple sheets which I am looking to copy and paste the relevant cells from rows into a new worksheet. The defining factor is whether the rows are "Live" which is determined by whether today falls between the "start date" and "end date" columns. For all rows that are "live" certain cells on that row should then be copy pasted to the bottom of the list on the new worksheet. I would then repeat these steps for the other worksheets so that the new worksheet essentially contains a summary of all "live" lines from the other sheets.
I then ultimately need to create a button so I can re-run this at the end of each day.
Hoping you can help!
You've defined and explained what you want to do nicely. So the next step would be to start researching the actions you want the code to do:
Start with how to copy rows:
http://www.bluepecantraining.com/copying-moving-rows-columns-excel-vba/
Next is how to find the last used row in a worksheet:
https://powerspreadsheets.com/excel-vba-last-row/
Next is to apply your condition on what rows to copy:
Using VBA to check if a date is between two dates
Next is how to create a for loop that runs the code the desired number of times:
https://www.excel-easy.com/vba/loop.html (a tip is to use the LastRow that you defined via the second link i posted. eg: "For x = 1 To LastRow")
And lastly is how to assign a macro to a button or shape:
https://wellsr.com/vba/2017/excel/how-to-assign-a-macro-to-a-shape-in-excel/
Try to build a code with the help of these links. If you get stuck, come back and show us what you've created and where the problem is. I (aswell as and many others) would love to help you on from there.

VBA Excel - copy, paste loop with offset

I am new to VBA and need some help - if anyone could help me.
I need to copy a range, say A1:F1, and paste it in the row directly under, so A2:F2, on an automatic loop.
The top-most row contains data from a different sheet, therefore I will be copying the formulas of that row which links to the secondary sheet. I would like the loop to end once the other sheet has been exhausted and the macro hits an empty row from the sheet.
Any ideas?
You can use a do until loop to find the length of your list. Then u can use this index to use range, push down the data and push your new data on the now empty top row.
I understand, that u want to push data always in the first row and everything else down, so you add always one new row.

Problems with paste as values in VBA

I have a problem in paste as values a multiple selection, this is what I have to do:
I have a spreadsheet, every cell has a formula in it and I want to create a macro which copy each cell and paste on itself as value (in order to remove every formula). The problem is that there are some cells which are locked, so if I try to copy and paste them excel returns an error and it stops the procedure.
Now I have two possibilities:
case 1: copy and past as values each cell individually, but I have a lot of cells and I have to do this procedure very often.
case 2: create a big selection which contains only the unlocked cells and then copy and paste them all togheter.
case 2 seems to be the better choice, but with excel I can't copy and paste multiple selection...does anyone have a tip for me?
I though to take the big fragmentary selection and copy and paste every block of the selection one by one, but I don't know if is it possible :(
Loop throught every cell in selection and check if they are locked like this:
For Each Z In Selection
if Not Z.Locked Then
'do copy paste here
End If
Next Z

Find a row with a string and paste rows above it

I'm sorry I have no code - I did some research and couldn't find what I wanted.
I have a table that I want to add rows to with the click of a button.
I created a cell at the bottom which says "End".
I want VBA to copy cells from N15:S16 (two rows), and insert two rows above the "End" cell, along with the pasted data.
How would I go about that in a click function?
Just curious whether you realise you may not need any code (or formula) for what appears to be your objective (but wanted to post an image so 'as an answer' regardless).
{In my case using Excel 2007}
Ensure N14:S15 is a Table. (Select N14:S15, Insert > Tables > Table, do not check My table has headers {unless required} and OK).
In Sheet Before I added Table labels and a Total row, in which I added "End" {there is no need for "End"} and added formulae to sum Qty and Value.
With Before as a starting point:
After is the result of selecting N14:S15 and pasting that into N16.
#John Bustos helped me solve it. I recorded a few macros, and this is the code I have now:
Sub Insert_Row_Click()
Range("T15:AK16").Select
Selection.Copy
Application.Goto Reference:="EndCell2"
Selection.Insert Shift:=xlDown
End Sub
Have a nice day everyone!

Excel VBA loop through columns in range and copy columns

I am new to Excel VBA and need some help writing a macro.
From Worksheet1 of Workbook1 I need to copy each column of range D1:Z100, one at a time, and pasteValue it to cells B1:B100.
This triggers a calculation in Worksheet2 of Workbook1. Here I need to copy cells A1:B200 into a new workbook.
This new workbook is to be renamed with the text string in Worksheet1, cell B1. The new workbook is to be saved into the same folder as Workbook1.
The loop is then to continue with the next column from Worksheet1, and continue until all columns in the range have been treated this way.
I have used two days searching the net to find an answer without any luck....
Start the way most of us did:
Record new macro.
Manually perform all the steps you want to automate.
Stop recording.
Go to the Developer tab and inspect the auto-generated code.
Write your own VBA based on what you learn in Step 4.
You will have to figure out a few things on your own, or come back here and ask specific questions.