Find cell & select/copy from active cell to last row including blanks - excel

I'm attempting to find a column using the column header name, then select all the data from the column (including the blank cells) & paste into another range.
Currently I can only copy until the 1st blank cell. I have seen similar problems on the board but the solutions I have seen are coming from the angle of knowing which column it is in first & then finding the last row from the bottom of the worksheet.
Workbooks("PS & Config - Actuals & FC.xlsm").Worksheets(2).Range("A3").CurrentRegion.Find(What:="FFA Name").Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Range("A3").Select
ActiveSheet.Paste

To find the column use Match wit the column header name. Once you have that column, set the values in the destination range to be the same as that column. Selecting, copying, and pasting in VBA are unnecessary as the values themselves can be moved--plus it adds load.
I don't have Office anymore so I'm running this from memory. I hope it helps.
dim wb as workbook, ws1, ws2 as worksheet, myCol, myRow as long
set wb=excel.thisworkbook 'assuming this code goes in that workbook
set ws1=wb.sheets(2) 'set variables
myCol=worksheetfunction.match("FFA Name",ws1.[a1:zz1],0) 'search through 1st row
myRow=ws1.[a3].currentregion.rows.count 'grab last row containing data in this set
'do not select, nor copy and paste, if it can all be done with VBA. This can.
set ws2=wb.sheets.add 'a little rusty on this line, you can get the method from the macro recorder
ws2.cells(1,1).resize(myrow).value=ws1.cells(1,myCol).resize(myrow).value
The reason your original code stops when there's a blank cell is because the .end() method simulates pushing the end key and then an arrow, which goes to the first/last contiguous cell with data.
Range(ActiveCell, ActiveCell.End(xlDown)).Select

Related

Copy a specific range every nth number of rows in Excel VBA

I'm very new to VBA coding and cannot figure out this issue. I tried another solution but found it wouldn't work later down the road. I'm currently testing out this one.
On worksheet "BS growth" I need to read Column A to make sure it says "Asset" If it does I then need to copy and paste the entire rows, excluding A,B & C starting from D5 until D30. The loop then needs to skip 15 cells down and copy the next range of 25 rows (D45:D70, D85:D110, etc) until it no longer says Asset in column A. The first 402 rows in Column A say "Asset," and then the next 300 say "Liability" and the final 200 says "Equity."
I'll then need to paste the data into worksheet "Chart Ref" starting at row A2.
Here is a sample of the data. I am not sure if it is working/embedded properly.
enter image description here
I do not know how to make a loop repeat as needed.
Sub ChartReference4()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("BS growth")
For Each cell In ActiveWorkbook.Worksheets("BS growth").Range("A:A")
If cell.Value = "Asset" Then
Range("D5:D30").Select
'I need this to not be hard coded and only copy from D5 down 25 and then skip 15 cells and repeat the loop
Range(Selection, Selection.End(xlToRight)).Select 'Need all information to right
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets
End If
Next cell
End Sub
Currently this code just copies everything in the sheet from column D to the right.
Thanks so much, please comment if this doesnt make sense or I need more information.

How to Autofill in other Sheet without selecting it?

I'm learning to avoid using .Select and I just cant figure out how to rewrite my .AutoFill code.
I have two different sheets, one where i collect data from, paste that in the other sheet, and I need the autofill to run on the second sheet without activating it.
This is my working code now, works because I Select the sheet and the cell "A30"
Selection.AutoFill Destination:=Range("A30:M30").Resize(NumRows1), Type:=xlFillDefault
This works perfectly, but I would like to NOT activate the sheet and select the cell.
To give you a starting point, here are some examples of using autofill on another sheet after copy/paste.
First - Copy the range you are going to use to the other worksheet
Second - Select the range on the worksheet you are going to autofill
Third - When using Resize the number has to be bigger then the range because when selecting a range the focus is on the first cell in the range. e.g. If not greater
then Range("A1:A10").resize(5).Select the selected range will be Range("A1:A5"), thus not autofill action.
Different ways to Resize are:
Sheets("Sheet3").Range("A1:B10").AutoFill Destination:=Sheets("Sheet3").Range("A1:B10").Resize(20), Type:=xlFillDefault
Or
Dim lr As Long
lr = Sheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
'the lr must be grater then the autofill range
Sheets("Sheet3").Range("A1:B10").AutoFill Destination:=Sheets("Sheet3").Range("A1:B10").Resize(lr), Type:=xlFillDefault
You can also use the With statement
With Sheets("Sheet3").Range("A1:B10")
.AutoFill Destination:=.Resize(.CurrentRegion.Rows.Count + .CurrentRegion.Rows.Count), Type:=xlFillDefault
End With
'the `.CurrentRegion.Rows.Count' doubles the range for the autofill, you can replace the second one with a specific number e.g. `.CurrentRegion.Rows.Count + 5'

VBA Code to Copy Data from Row 2 to Last Row of Data on One Sheet and Paste to the First Empty Row of Another Sheet

I am working with multiple sheets within a workbook. Each sheet has an identical header row. I would like to write a macro to copy a range of data from each sheet (A2:L2 to the last row of data on a sheet) and paste it into the first empty cell in Column A of master sheet.
What I have below doesn't seem to work. Any assistance is appreciated.
Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count,1).End(xlUp).Row
Sheets("Master Sheet").Range("A2:L10000).Clear
Sheets("Sheet1").Activate
Range("A2:L" & Lastrow).Select
Selection.Copy
Sheets("Master Sheet").Select
Range("A30000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1,0).Range("A1").Select
ActiveSheet.Paste
Sheets("Sheet2").Activate
Range("A2:L" & Lastrow).Select
Selection.Copy
Sheets("Master Sheet").Select
Range("A30000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
You're almost there from the looks of it. It looks like you've recorded a macro which is a good place to start. The reason your specific code might not work is because of Sheets("Master Sheet").Range("A2:L10000).Clear. You're missing the end quote inside of the range. In any case though, I've chosen to leave that out so you don't accidentally clear your sheet when you're moving data across
So generally it's good to avoid using select and activate, but with a recorder you don't have much say in the matter. Below you can see how I can do operations directly with the range.
It's important to find the last row of the master sheet as well as your current sheet each time so you know the range you want to copy and where you can paste it. It's important to remember that you're always finding the last filled cell, so in the case of a paste destination, you need to add one more to the row value so you don't accidentally overwrite some data.
For loops are pretty useful, I'm not sure what names you have for the rest of your sheets, but luckily in VBA you can use For each. so what this does is it cycles through every single item that you specify. In this case I've specified worksheets. The only problem now though, is we don't want to try copying and pasting into the same Master Sheet, so we need to do a quick check to make sure we're not on the master sheet. I've done this simply by comparing the name of the worksheet to what you've put as the name of the master sheet.
Interestingly when you copy something, you only need to specify the top left cell and it'll fill in the rest of it. It makes life a little easier because you don't need to figure out the exact dimensions of the array you're pasting. The Copy function in VBA has an optional parameter called Destination which you can use to tell it where you want to paste it right away.
It's also good to fully specify ranges when you're using them so instead of Range, you can see how I use ThisWorkbook.Worksheets("Master Sheet").Range. This tells the computer exactly where you want to reference; whereas Range makes the computer sort of guess, so it assumes you mean the active sheet, which might not be what you want.
Sub Paster()
Dim LastRowCurr As Long
Dim LastRowMaster As Long
Dim wksht As Worksheet
For Each wksht In ThisWorkbook.Worksheets
If Not wksht.Name = "Master Sheet" Then
LastRowCurr = wksht.Cells(wksht.Rows.Count, 1).End(xlUp).Row
LastRowMaster = Worksheets("Master Sheet").Cells(Worksheets("Master Sheet").Rows.Count, 1).End(xlUp).Row + 1
Range("A2:L" & LastRowCurr).Copy Destination:=ThisWorkbook.Worksheets("Master Sheet").Cells(LastRowMaster, "A")
End If
Next wksht
End Sub

Copying values from one Table to the first empty row in another Table

As part of my project I have a Table which includes lookup formulas in each column that are dragged down the whole table. Depending on the case only the first x rows return values. I included an iferror so that the lookups that don't return values return "".
Now I want to copy the rows of the table that return values to the first empty row in a different table in a different worksheet.
The code I have so far:
Sub Copy_Results()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet2")
copySheet.Range("Table1").Copy
pasteSheet.ListObjects("Table2").Range.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Now the big problem is that I want to be able to execute this macro multiple times, each time the values from Table 1 should be pasted below the preexisting values in table 2.
The point being that each time the lookup values change meaning I get new results I want to paste them in a table where all the results are documented.
Issues that I had so far:
The first Copy Paste usually works, but when I copy again the values get pasted way below the first ones outside of the table. Usually the full length of the table away. I guess this is because the whole copy table is filled with formulas.
The easiest way to do this is to restrict the cells that you are going to copy using the SpecialCells method:
https://msdn.microsoft.com/en-us/library/office/ff196157.aspx
In this case you only want to copy the formulas that have numbers as the values, so this would be the syntax:
SpecialCells(xlCellTypeFormulas, xlNumbers)
Put into your code it would be:
copySheet.Range("Table1").SpecialCells(xlCellTypeFormulas, xlNumbers).Copy
You can see this in action outside of your code by selecting the complete range in your source sheet then pressing F5, selecting the "Special" button at the bottom of the dialog that pops up, then select "Formulas" and "Numbers".
To make sure it pastes in the next available row, use the CurrentRegion property:
https://msdn.microsoft.com/en-us/library/office/ff196678.aspx?f=255&MSPPError=-2147217396
This code will tell you what the last row is in the used area defined by cell A1:
pasteSheet.cells(1,1).CurrentRegion.Rows.Count
I believe the paste command you're looking for will be close to this (hard to test exactly without your spreadsheet):
pasteSheet.cells(pasteSheet.cells(1,1).CurrentRegion.Rows.Count + 1, 1).PasteSpecial xlPasteValues

Excel VBA - How to insert values only?

I'm looking for a way to copy a range (copyrange exists from 11 columns and a variety of rows) from an other workbook and insert/paste values only in my primary workbook, underneath the data already there.
Just pasting is no issue, but because I don't want it to overwrite the last (SUM-)line I want to INSERT the values. By my knowing there isn't anything like InsertSpecial xlInsertValues.
So how can I insert entire empty rows based on the counted rows of the copied range and than paste the values only in columns "E" to "O"?
Some preconditions are:
copyrange exist from 11 columns and a variety of rows
I'm trying to avoid having to switch twice between the two documents. So I only want to open the extern workbook, copy the values, open my primary workbook en insert the copied data with blank cells left and right from the range.
This is what I've got so far. It all goes wrong at the Insert part, because it doesn't paste/insert values only. Note that it's only a part of a bigger code. Rng31 is the copied range in the extern workbook.
Dim Rng31 As Range
Set Rng31 = Rng21.Resize(Rng21.Rows.Count, Rng21.Columns.Count + 10)
Dim regels As Integer
regels = Rng31.Rows.Count
Rng31.Copy
Wbbase.Activate
Sheets(2).Activate
Dim onderste As Range
Set onderste = Range("E65536").End(xlUp).Offset(1, 0)
onderste.Insert shift:=xlDown
Selection.PasteSpecial xlPasteValues
...
InsertValues Rng21.Resize(Rng21.Rows.Count, Rng21.Columns.Count + 10), _
Wbbase.Sheets(2).Range("E65536").End(xlUp).Offset(1, 0)
...
Sub InsertValues(rngCopyFrom As Range, rngCopyTo As Range)
Dim rngDest As Range
Set rngDest = rngCopyTo.Resize(rngCopyFrom.Rows.Count, _
rngCopyFrom.Columns.Count)
Application.CutCopyMode = False 'Edit Added:clear any copied data
rngDest.Insert shift:=xlDown
rngDest.Offset(-rngCopyFrom.Rows.Count).Value = rngCopyFrom.Value
End Sub
The problem is that if you have a cell copied (i.e. its on the clipboard) you cannot simply "insert a new row" because the only option is to "insert copied cells" thus the onderste.Insert will insert the copied cell and keep the formula. In addition you then call Selection.PasteSpecial which I do not think is what you desired because you never set your selection anywhere in your code post, so whatever you had selected prior is what will contain the value.
You have a couple options:
You can insert the row first, then copy and paste the cell value (this requires toggling back and forth between workbooks)
Copy the cell, insert the new row (which is what you are doing) then clear the row and paste in the values.
As a side note:
Range("E65536").End(xlUp).Offset(1, 0)
would be better stated as
Range("E" & Rows.Count).End(xlUp).Offset(1, 0)

Resources