I'm having trouble copy pasting a range of cells based on a dynamic start point (the start point is 1 cell beneath a cell with a specified value).
This specific range of cells will always be in columns A-Z, but the row is dynamic across worksheets. However, this range of cells is always preceded by a row above with a specific cell value, let's say "Dataset Here".
So in an example sheet, I need to copy A650:Z700, and the cell "Dataset Here" is in A649.
How can I copy a range based on the requirement that the range falls underneath the cell with the value "Dataset Here"?
I was able to identify the starting row, but am lost how to turn it into a dynamic version of Range("A650:Z700").Copy:
Dim StartRow As Long
StartRow = Range("A:BA").Find("Dataset Here").Row + 1
Hello Crystal and Welcome.
You could do something like the below. This works with the assumption that all your cells and columns below the 'Dataset Here' are populated (otherwise the copy range might not be correctly defined). The following example copies cells A6:B9 to cell F1
Option Explicit
Public Sub sCopyCells()
Call sCopyCellsForWorksheet(Worksheets("Sheet1"))
End Sub
Private Sub sCopyCellsForWorksheet(ByRef ws As Worksheet)
Dim target As Range, source As Range, destination As Range
Set target = ws.Range("A:A").Find("Dataset Here")
Set source = Range(target.Offset(1, 0), target.End(xlDown).End(xlToRight))
Set destination = ws.Range("F1")
source.Copy
destination.PasteSpecial xlPasteAll
End Sub
Related
I'm trying to use VBA to copy a cell on the active row of a range.
A1:A10 is a range named "Panel_Length". I want to click a button to copy the cell from column "A" of the active row. The code works if I specify column "A" and active row but there is a chance that the range "Panel_Length" may get moved to another column.
Sub Plen_Click()
Dim PLength as Range
Set PLength = Sheet1.Range("Panel_Length")
Range(PLength & ActiveCell.Row).Copy
End Sub
Don't know if I understand the question correct. but if you try to copy the cell from column of the named range and the row form the selected cell you can try the code below
Sub Plen_Click()
Sheet1.Cells(Selection.Row, Sheet1.Range("Panel_Length").Column).Copy
End Sub
I'm trying to copy the one specific column(excluding the header) after filtering my dataset. If there are multiple rows after filtering, my selection of range works fine and I am able to copy that column into another sheet. However, if there is only one row after filtering, when I specify the range, it selects all the empty cells along with the non-empty cell and my code is malfunctioning. How do I fix this issue?
I have tried using different range properties but couldn't get the desired outcome
'''Finding the Pack Test Category from the filtered 1st column'''
Set RngA = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas(2)(1, 1)
''Here the selection of range includes all the empty cells as well!
Set RngA = Range(RngA, RngA.End(xlDown))
'''Copy & Pasting in the Forecast Sheet for temporary use'''
RngA.SpecialCells(xlCellTypeVisible).Copy Destination:=wbA.ActiveSheet.Range("L1")
I expect the range to be selected with only the visible cells having data not the empty cells.
Say we start with:
and filter for age above 45:
we want to copy filtered column A to another sheet:
Sub KopyOneKolumn()
Dim r1 As Range
Dim r2 As Range
Dim r3 As Range
Set r1 = ActiveSheet.AutoFilter.Range ' the total visible range
Set r2 = Intersect(r1.Offset(1, 0), r1) ' clip off the header
Set r3 = Intersect(r2, Columns(1)) ' pick only column A
r3.Copy
Sheets("Sheet2").Paste
End Sub
Result in Sheet2:
NOTE:
The key point is that r1 represents a "copy-able" block of visible cells in the autofilter table.
Relying on Areas(2) is not going to work if the first row under the header is part of the filtered (visible) cells.
'at this point, AutoFilter has been applied
'I have no idea what the range is
'or what column you are interested in
with ActiveSheet.AutoFilter.Range
with .cells.resize(.rows.count-1, 1).offset(1, 0)
set RngA = .SpecialCells(xlCellTypeVisible)
end with
end with
RngA.copy Destination:=wbA.ActiveSheet.Range("L1")
I disagree with your use of ActiveSheet as the parent worksheet references. The source and destination worksheets should be explicitly referenced.
I am trying to write a code for inserting a new row in sheet "Sheet3" in which the first column value is a value I have stored as a Range.
I have made a formula for identifying value in the active sheet, as follows>
Dim cellTaxa As Range
Set cellTaxa = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(1, -2)
End Sub
Basically, the above mentioned formula stores a value in a cell that is one cell down and two cells left from the button that I clicked.
The next thing i want to do is to insert a new row in Sheet 3 in which the first column value is cellTaxa.
I couldn't find anything that will work on my structure.
Any help or hint is appreciated.
Thank you!
Option Explicit
Public Sub yoursub()
Dim cellTaxa As Range
Set cellTaxa = Sheets("Sheet1").Range("G3") 'replace with your range
'Would be even better to not use range for a single cell
' E.g.: Set cellTaxa = Sheets("Sheet1").Cells(3,7) OR Cells(3,"G")
'same format can be applied for the .pastespecial part
Sheets("Sheet3").Range("A1").EntireRow.Insert 'adds empty row at "A1"
cellTaxa.Copy
Sheets("Sheet3").Range("A1").PasteSpecial 'pastes CellTaxa into the newly created row
End Sub
I have created a tool in excel which can take two spreadsheets, and copy the content from one to another when we do an update on the sheet itself.
The tool is purely designed to be a copy / paste tool, the current code will copy and paste values from sheet to sheet.
I have to include logic into the tool to skip cells with formulas, if the tool copies and pastes the formulas which are currently in the source sheet to the target sheet, they no longer match and throw #REF errors. Any suggestions on how to put a for loop in here or something similar to allow it to check and ignore cells with formulas? I need it only to copy / paste cells with numbers or values.
Sub CopyWorkbook()
Dim wb1 As Workbook, wb2 As Workbook
wb1.Sheets("Capex").Range("H1124:AT1173").Copy
wb2.Sheets("Capex").Range("H529:AT578").PasteSpecial Paste:=xlPasteAll
wb1.Sheets("Capex").Range("H1275:AT1284").Copy
wb2.Sheets("Capex").Range("H580:AT589").PasteSpecial Paste:=xlPasteAll
Rather than loop cell by cell you can use SpecialCells to identify the formulae
There are two options
Copy only the Constant cells to the destination
Remove any Formula cells from the destination
If your formulae occur in a single contigious block then (1) works easily, else this will result in a number of areas needing to copied over. So (2) is preferable
Your first range can be covered as so.
Dim rng1 As Range
Set rng1 = Range("H1124:AT1173")
rng1.Copy [h1275]
On Error Resume Next
[h1275].Resize(rng1.Rows.Count, rng1.Columns.Count).SpecialCells(xlFormulas) = vbNullString
On Error GoTo 0
If you really want to skip cells containing formulas, you could use this example as a start.
The code assumes that only formulas start with an equals sign.
Edit: expanding the example with the ranges in the question.
Sub example()
Dim source As Range
Dim target As Range
Set source = ActiveSheet.Range("A1:B6")
Set target = ActiveSheet.Range("D1:E6")
copy_non_formulas source:=source, target:=target
'Extended example using the ranges posted in the question
'For the sake of formatting, I omitted the fully qualified
'range names.
copy_non_formulas source:=Range("H1124:AT1173"), target:=Range("H529:AT578")
copy_non_formulas source:=Range("H1275:AT1284"), target:=Range("H580:AT589")
End Sub
Public Sub copy_non_formulas(source As Range, target As Range)
'Assumes that all formulas start with '=' and all non formulas do not
Dim i As Long
Dim j As Long
Dim c As Range
For i = 1 To source.Rows.Count
For j = 1 To source.Columns.Count
Set c = source(RowIndex:=i, ColumnIndex:=j)
If Left(c.Formula, 1) <> "=" Then
target(RowIndex:=i, ColumnIndex:=j).Value = c.Value
End If
Next j
Next i
End Sub
Can't you just use Paste:=xlPasteValues for all cells? That way no formulas get copied to the target sheet.
I have a spreadsheet I'm using to compile text that changes all the time.
In column AD, Row 4(AD4) I put the contents of text, and it can have data going 1000 to 4000 rows down. It changes every time, so there is no static range name. I need a macro that
finds the final piece of data in that column,
then automatically "drags a box" from that spot two columns to the left (AB4)
and copies it... (A 3000 row piece of text would be AB4:AD3004) (Macro stops there, with text to be copied highlighted)
The current version finds the bottom cell correctly, but if I run the macro a 2nd time, with new data, it keeps trying to copy the same range. (I used the Formula Define.Name method, to name the cell, and then selected AB4:LastRow) but it is ALWAYS 3160 whether data goes to row 4000 or not.....
Sub Last_row()
Cells(Application.Rows.Count, 30).End(xlUp).Select
' following lines of code are useless
Range("AB4:AD3160").Select
Range("AD3160").Activate
Selection.Copy
End Sub
To answer your question directly:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy
End With
Copy to specific location WITHOUT using clipboard:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy Sheet2.[A1]
End With
Copy and exclude formatting:
With Sheet1
With .Range("AB4", .Cells(Rows.Count, "AD").End(xlUp))
Sheet2.Cells(1, "A").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End With
Note: Replace all sheet codenames (sheet1, Sheet2) above with your actual sheet codenames.
Your current code hard-codes the range of interest with
Range("AB4:AD3160").Select
This code will define a dynamic range starting from AB4 to the last non-empty cell in column AD
You can then use this range (without selecting) for changing values elsewhere (note that you may not need to actually copy rng1, it is possible to dump these values to a separate range directly without a copy and paste.
Sub Last_row()
Dim rng1 As Range
Set rng1 = Range([ab4], Cells(Rows.Count, 30).End(xlUp))
rng1.Copy
End Sub
Update: Example of how to copy a dynamic sized range from one sheet to another without a copy and paste:
Sub Last_row2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Set ws1 = Sheets(1)
Set ws2 = Sheets(2)
Set rng1 = ws1.Range(ws1.[ab4], ws1.Cells(Rows.Count, 30).End(xlUp))
ws2.[a1].Resize(rng1.Rows.Count, rng1.Columns.Count).Value = rng1.Value
End Sub