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.
Related
Looking for a solution to probably a very simple problem but somehow I can not seem to find an answer.
I am looking for a way to copy the first cell of each column. I want to use the copied value to filter it in another workbook. I want to paste the filtered cells below and then repeat the process until all columns have been gone through.
Thus what kind of loop would I use to copy every first cell at a time, store it, then do something with it and then copy the next first cell of a column for all columns?
Could someone please me with this? Your help is much appreciated.
For i = 1 To 207
Columns(i).Select
ActiveCell.Offset(0, 0).Range("A1").Select
Selection.Copy
'How do I store the copied value to use it in an autofilter?
Next i
End Sub
Instead of selecting – and you should avoid using select –
You can set the value to a variable varname = Cells(1,i) for later use,
or better yet, apply directly to your filter.
Dim i As Long
Dim wb2 As Workbook
Set wb2 = Workbooks("Book2") 'the "Other" workbook name here
For i = 1 To wb2.Worksheets.Count 'looping each sheet
wb2.Worksheets(i).Range("A1").AutoFilter Field:=1, Criteria1:= _
ThisWorkbook.Sheets(1).Cells(1, i)
Next i
This is just an example which makes a lot of assumptions that more than likely does not fit with your reality, these has to be changed to fit your scenario, ofc.
But it would go through the specified amount of worksheets in the specified workbook, and filter the first column with the value obtained from the first workbook.
Then, there are quite a few questions on how to copy a filtered range that might give a hint.
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
Relatively new to VBA, so apologies if this is a poorly phrased question:
How can I set up a macro so that it will copy the entire row containing the active cell to the next empty row down?
Potential complications:
All data is being entered in a pivot table due to other functions
One column auto-calculates, does that count as not being empty?
I managed to make a macro copy to the next row down, but it will
overwrite that row as it stands.
(I know I could copy paste the row but I'm making a very large spreadsheet for other people who want to fill it as fast as possible.)
A brief description of what I have so far:
Sub CopyDown()
'
' CopyDown Macro
' duplicate current row
'
' Keyboard Shortcut: Ctrl+w
'
If IsEmpty(ActiveCell.Offset(1, 0).EntireRow) Then
'This part works
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
ActiveSheet.Paste
'This part works
End If
End Sub
Advice would be appreciated, even if it's just the right keywords to search for this type of function.
Sub copy()
Range(“A2:E30”).Select
Application.CutCopyMode =False
Selection.Copy
Windows("yGL dcs.xlsx").Activate
Range("A47").Select
ActiveSheet.Paste
End sub
The first range is not always 29 rows and could be a different row in the yGL workbook. How do I make it be able to copy the data to the last row and add it to the last row of data in the yGL workbook? Thank you
There's a bit to pick apart with what you're trying to do and although using .Select or .Activate is not recommended (I think you may have just recorded a macro, which is more than ok), I'm not going to sit here and rewrite your entire solution because I simply don't have the time.
The following should help you though.
ActiveSheet.Range("A2:" & ActiveSheet.Range("E1").End(xlDown).Address).Select
... that will help you select all of the data in your dynamic source workbook. It'll select a range from A2 to the last row in column E dynamically.
And this piece of code ...
ActiveSheet.Range("A" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row).Select
... will take you to the last row in your destination workbook in column A.
I hope that helps and I hope that makes sense.
I am trying to transfer selected data from one worksheet to another via a Transfer macro.
Every time I try to do so, I get the following message in Debug:
Sub Transfer Macro
ActiveCell.Offset(-10, -7).Range("A1").Select
Selection.Copy
Sheets("Data Presentation Template").Select
ActiveCell.Offset(-25, -6).Range("A1").Select
ActiveSheet.Paste
End Sub
Is there a better way to transfer/ mirror the data from a data entry sheet to a presentation sheet with out macro?
Your ActiveCell.Offset(r, c) commands use negative numbers. These will depend on whether the currently active cell is in a position where the negative offsets don't try to .Select a cell that is either off the worksheet to the left of column A or a cell that is above row 1.
ActiveCell.Offset(-10, -7).Range("A1").Select
This demands that the ActiveCell is at least 10 rows down and 7 columns right from A1; specifically K8 or to the right/down of K8. Anything closer to A1 will produce an error since you are trying to select a cell that is off the worksheet.
ActiveCell.Offset(-25, -6).Range("A1").Select
The same for this statement but the minimal distance for the ActiveCell from A1 would be Z7.
The ActiveCell property changes every time you select another cell. It cannot be relied upon to 'automatically transfer' data between worksheets unless you check to ensure that the ActiveCell is where you want it on each worksheet before running your sub procedure.
Any further recommendations would require specific worksheet names and ranges for the source and target of the copy & paste operation.
The .Range("A1") in ActiveCell.Offset(-10, -7).Range("A1").Select simply means the top-left cell in the ActiveCell.Offset(-10, -7) range. It is likely a 'hangover' from relative positioned macro recording.