Find a row with a string and paste rows above it - vba

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!

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.

Selecting columns based on specific text strings

I'm trying to put together a macro which will select certain columns and paste them into a new sheet. The problem is that columns tend to be added and deleted as people see fit which throws off the absolute referencing.
I have been trying to tinker with the basic macro produced using the macro recorder but I haven't had any luck selecting columns based on their contents. I have a sheet that is generated from our database daily with changing fields. For example, I would like to select just the Part #, Cost, and Contact fields but the addition of the IDN today threw my old macro off.
So far, I've tried to use basic excel find functions like vlookup, index against a list of constants and the find function below but none seem to work. Is there something I am missing here to select the column with my desired text?
Columns(find("Part #")).Select
Firstly, be careful with .Select and it looks like you're not fully qualifying your references to the worksheet so take note of the full Workbook.Worksheet.Range type referencing below.
Here's your quick fix:
Public Sub Test()
'the Range approach
ThisWorkbook.Worksheets("Sheet1").Rows(1).Find(What:="Part #", LookAt:=xlWhole).EntireColumn.Copy
ThisWorkbook.Worksheets("Sheet2").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats 'or xlPasteAll to include formulas
End Sub
Using entire column ranges isn't usually the best idea. It will work, but only as long as you remember to paste your copied column into the first row of the destination worksheet every time.
Here's the better option (IMO):
Convert your data to a Table (know as a ListObject in VBA) by clicking "format as table" on the home tab of the ribbon. It's now much easier to reference your column - just do it by name, no need to use Find.
Public Sub Test()
'the ListObject approach
ThisWorkbook.Worksheets("Sheet1").ListObjects("MyTable").ListColumns("Part #").Copy
ThisWorkbook.Worksheets("Sheet2").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats 'or xlPasteAll to include formulas
End Sub
You need to know the name of your table (I called it MyTable here), which you can do by clicking on "Table" on the ribbon when your table is selected
You need to specifically find the column, then you can paste off to a destination location, such as:
Sheets("Source").Columns(Sheets("Source").Rows(1).Find("Part #").Column).Copy Sheets("Dest").Cells(1,1)

Pasting same cell on different rows as value is replaced

This sort of follows up from my previous few questions on the same workbook.
I have two sheets, the first being Car Search, which contains a form (NOT a VBA form, just a normal table that appears like a form) to fill in. The second sheet is Raw Data, and will contain all the information entered in the Car Search sheet. It will be displayed row by row (see 2nd image).
In the Raw Data sheet, I am using the formula =""&'Car Search'!B3 to copy the contents of cell B3 in the Car Search spreadsheet.
My question is: If I had a new Car ID value, how can that automatically be entered into the row below?
Essentially, I am trying to use the form to capture all data for new cars coming in, and then I would like all that data to appear in the second sheet in their respective rows/columns.
Any help much appreciated! :)
EDIT:
Good news!
You need to use VBA:
Sub Range_Copy_Examples()
Worksheets("Car Search").Range("B3").Copy Worksheets("Raw Data").Range("=OFFSET(Sheet3!B1,0,0,COUNTA(B1:B300)+1,1)")
End Sub
However there is a small bug where it keeps pasting across past values, so I have suggested an alternative macro below
Re-edit:
A more mechanical way to make macro work (hopefully someone can improve on it) looks like this:
Sub Macro1()
Sheets("Car Search").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Raw Data").Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Worksheets("Car Search").Activate
End Sub
This works as follows:
in Car Search, have cell B3 selected the whole time
in Raw Data, select the cell with the last value in the column where you want you Car Search!B3 data pasted (you only have to do this once)
press Run on the macro (easy enough to just record one yourself)
go back to Car Search and change the value of B3, press Ctrl-Enter to keep the same cell selected after changing value, then press the same macro button without changing a thing.
If someone could add so that when pressing Ctrl-Enter on Car Search!B3 the Raw Data gets automatically added without having to manually run the macro, it would be fully automated!

Copy range until empty row and paste into new sheet

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.

External Data Pull: Pull more specific data or change macro based on data

This question is somewhat difficult to explain, so bear with me.
I am pulling data from a large table for my company and am trying to create a macro to make this data easier to read/understand. The data that is on the site changes every day based on what caused certain failures in our plant, which causes my macro to analyze data that isn't there or wrong cells (due to rows getting shifted/moved/added/removed). Because I don't think that was really clear, here is an example:
The macro says to select cells J5, J13, and J25. These were, when I was creating the macro, the values I wanted to be put in a list. However, when I pulled the data and ran the macro today, these values were in different spots on my sheet (the value for cell J13 is now in J12). This completely messes up all of the analysis and renders my macro / data pull useless.
Is there a way to have the macro select the data more intelligently? Perhaps have it check for the group name, then select the value from the cell next to it? I wish I could word this better... Thanks if you've gotten this far!
Simply put... yes. Here's a code exert for looking for a groupname and getting the adjacent cell:
Dim Group1Range As Range
'Look in ThisWorkbook
With ThisWorkbook
'Look in Sheet1
With .Sheets(1)
'Look in Column I
With .Columns("I:I")
'Find the text Group1
Set Group1Range = .Find(What:="Group1").Offset(0, 1)
End With
End With
End With
'Indicate the address of the found range
Debug.Print Group1Range.Address
End Sub
Now here are ways that you can improve your question:
Explain how you know that cell J13 is no longer valid, and that J12 is now.
Give us some sample data.
Give us your code.
Tell us what your end result would be, possibly with an example.