I am trying to create a code that allows me to paste a selected range of data and paste it into Book2 in the first blank cell in Column A, starting from A1.
This is what I've got so far:
Sub Macro 1 ()
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Book2.xlsm").Activate
**CurrentRow = Range("A1").End(xlDown).Offset(1, 0).Row**
ActiveSheet.Paste
End Sub
I believe the trouble is the line with asterisks (**).Can someone help me rewrite this line/code so the copied data can paste in the first available cell from A1 down? (Up won't work since i have filled in cells further down the chart). Right now the code is pasting the data in whatever cell is selected :(
Thank you for your help everyone.
Try this out:
Dim book2 As Excel.Window
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks("Book2").Activate
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
You could work more directly with ranges (without Select) as below:
Sub Better()
Dim Wb As Workbook
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Range(ActiveCell, ActiveCell.End(xlToRight))
Set rng1 = Range(rng1, rng1.End(xlDown))
On Error Resume Next
Set Wb = Workbooks("book2.xlsm")
On Error GoTo 0
If Wb Is Nothing Then Exit Sub
Set rng2 = Wb.Sheets(1).Columns(1).Find("*", Wb.Sheets(1).[a1], , , xlByRows, xlPrevious)
If rng2 Is Nothing Then Set rng2 = Wb.Sheets(1).[a1]
rng1.Copy rng2.Offset(1, 0)
End Sub
Related
I want to copy a range between sheets using for..next with step, but I'm not fluent with using the for..next statement. I have recorded the step with macro, here is the code:
Sub Macro1()
Range("A2:A22").Select
Selection.Copy
Sheets("Sheet4").Select
ActiveSheet.Paste
Sheets("db_pivot").Select
Range("C2:C22").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("B2").Select
ActiveSheet.Paste
Sheets("db_pivot").Select
Range("E2:E22").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("C2").Select
ActiveSheet.Paste
Sheets("db_pivot").Select
Range("G2:G22").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("D2").Select
ActiveSheet.Paste
End Sub
Could you convert the code with for..next statement with step?
You can use the Uninon to group your non-continous Columns in one Range.
Option Explicit
Sub Macro1()
Dim DBPivotSht As Worksheet
Dim Sht4 As Worksheet
Dim UnionRng As Range
' get used to allways define your worksheet objects
Set DBPivotSht = Worksheets("db_pivot")
Set Sht4 = Worksheets("Sheet4")
With DBPivotSht
Set UnionRng = Union(.Range("A2:A22"), .Range("C2:C22"), .Range("E2:E22"), .Range("G2:G22"))
End With
' copy the entire Union range and paste in "Sheet4"
UnionRng.Copy Destination:=Sht4.Range("A2")
End Sub
Without FOR ... NEXT statement, I propose this code
With Sheets("db_pivot")
.Range("A2:A22").Copy Sheets("Sheet4").Range("A1")
.Range("C2:C22").Copy Sheets("Sheet4").Range("B2")
.Range("E2:E22").Copy Sheets("Sheet4").Range("C2")
.Range("G2:G22").Copy Sheets("Sheet4").Range("D2")
End With
HTH
Jon
I'm working on a code that will copy several columns of data that are not in order. I couldn't figure out how to do it in one step so i was trying to get it in two. After the first set of columns posts I'm having trouble getting it to go back to the sheet I was on to copy the next set of columns. This is what my code looks like so far.
Survey_Macro1 Macro
range("A:D").Select
range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add.Name = "Data"
ActiveSheet.Paste
ThisWorkbook.Save
ThisWorkbook.Sheets("Table").Activate
Application.CutCopyMode = False
range("AK:AL").Select
range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ThisWorkbook.Worksheets("Data").range(E1).Select
ActiveSheet.Paste
See How to avoid using Select in Excel VBA macros.
Sub yg23iwyg()
Dim wst As Worksheet
Set wst = Worksheets.Add
wst.Name = "Data"
With Worksheets("Table")
.Range(.Cells(1, "A"), .Cells(.Rows.Count, "D").End(xlUp)).Copy _
Destination:=wst.Cells(1, "A")
.Range(.Cells(1, "AK"), .Cells(.Rows.Count, "AL").End(xlUp)).Copy _
Destination:=wst.Cells(1, "E")
'alternate with Union'ed range - becomes a Copy, Paste Special, Values and Formats because of the union
.Range("A:D, AK:AL").Copy _
Destination:=wst.Cells(1, "A")
End With
End Sub
I am trying to write a code for the following task, but I have been struggling a quite a bit.
I have 2 workbooks, wb1 and wb2.
wb1 has a table with a list of names in column A, then column B-V has the data I want to copy to the sheet with the same name as in column A but in a different book (wb2). The location its pasted to is also dependent on another criteria on the destination sheet in wb2.
so for example in wb1 "John" is the name in A1, switch to wb2, go to the sheet called John, check the criteria on cell A4 of this sheet:
There are 3 criteria which are: Teen, adult or Elder
If Teen, then copy B1 into B97, copy C1 into B135, copy D1 into B147 & B190, copy E4 into B1100
If Adult, then copy J1 into B97, copy F1 into B135, copy G1 into B147 & B190, copy H4 into B1100
If Elder, then copy B1 into B97, copy C1 into B135, copy D1 into B147 & B190, copy E4 into B1100, copy J1 into B113, copy F1 into B1910, copy G1 into B1473 & B1930, copy H4 into B1190
(The above is just an example, there is a more cells to copy paste than listed above)
This should be looped for all names in column A of wb1.
Below is what macro record gave me, but it doesn't record the criterias. Both workbooks will be open btw.
Sub Summary()
Dim wb1 As Workbook
Dim Sht As Worksheet
Dim Rng, Rng2 As Range
Set wb1= ThisWorkbook
Set Sht = MasterBook.Worksheets("Sheet")
Set Rng = Sht.Range("A2:A" & Sht.Cells(Sht.Rows.Count, "A").End(xlUp).Row)
Dim wb2 As Workbook
Dim cell As Range
For Each cell In Rng '<---Here is where my first problem is,
'not sure how to get the excel to switch to the sheet
'with the same name as in column A then check cell A4 for the criteria'
If cell.Value = "Teen" Then
Range("C12").Select
Selection.Copy
Windows("wb2.xlsx").Activate
ActiveWindow.SmallScroll Down:=81
Range("B97").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=-9
Windows("wb1.xlsx").Activate
Range("D12").Select
Application.CutCopyMode = False
Selection.Copy
Windows("wb2.xlsx").Activate
ActiveWindow.SmallScroll Down:=12
Range("B95").Select
ActiveSheet.Paste
Windows("wb1.xlsx").Activate
Range("E12").Select
Application.CutCopyMode = False
Selection.Copy
Windows("wb2.xlsx").Activate
ActiveWindow.SmallScroll Down:=-45
Range("B47").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=63
Range("B118").Select
ActiveSheet.Paste
Windows("wb1.xlsx").Activate
Range("F12").Select
Application.CutCopyMode = False
Selection.Copy
Windows("wb2.xlsx").Activate
ActiveWindow.SmallScroll Down:=48
Range("B163").Select
ActiveSheet.Paste
Windows("wb1.xlsx").Activate
Range("G12").Select
Application.CutCopyMode = False
Selection.Copy
Windows("wb2.xlsx").Activate
ActiveWindow.SmallScroll Down:=-66
Range("B93").Select
ActiveSheet.Paste
Windows("wb1.xlsx").Activate
Range("H12").Select
Application.CutCopyMode = False
Selection.Copy
Windows("wb2.xlsx").Activate
ActiveWindow.SmallScroll Down:=9
Range("B105").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=60
Range("B167").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("wb1.xlsx").Activate
Range("I12").Select
Application.CutCopyMode = False
Selection.Copy
Windows("wb2.xlsx").Activate
ActiveWindow.SmallScroll Down:=-27
Range("B141").Select
ActiveSheet.Paste
Windows("wb1.xlsx").Activate
Range("J12").Select
Application.CutCopyMode = False
Selection.Copy
Windows("wb2.xlsx").Activate
Range("B145").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=138
Windows("wb1.xlsx").Activate
Windows("wb2.xlsx").Activate
ActiveWindow.SmallScroll Down:=51
Range("B326").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=12
Range("B339").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "1"
Range("B317").Select
ActiveCell.FormulaR1C1 = "1"
Range("B312").Select
ActiveCell.FormulaR1C1 = "1"
Windows("wb1.xlsx").Activate
Range("K12").Select
Selection.Copy
Windows("wb2.xlsx").Activate
Range("B107").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=-63
Range("B49").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=-9
Windows("wb1.xlsx").Activate
Windows("wb2.xlsx").Activate
ActiveWindow.SmallScroll Down:=306
Windows("wb1.xlsx").Activate
else If cell.Value = "Adult" Then
'<-----same stuff as above for different cells copy pasted'
else If cell.Value = "Elder" Then
'<-----same stuff as above for different cells copy pasted'
end if
End Sub
Also I don't know if the case function would be useful instead of the If statement here either.
Thanks a lot in advance
EDIT 1
I changed the code as suggested below
Sub Summary()
Dim wb1 As Workbook
Dim Sht As Worksheet
Dim Rng, Rng2 As Range
Dim wb2 As Workbook
Dim cell As Range
Set wb1 = ThisWorkbook
Set wb2 = Workbooks("Measure Templates.xlsx")
Set Sht = wb1.Worksheets("Summary")
Set Rng = Sht.Range("A5:A" & Sht.Cells(Sht.Rows.Count, "A").End(xlUp).Row)
For Each cell In Rng
Select Case wb2.Sheets(cell.Text).Range("A4").Value
Case "Standard Bathroom Template"
wb1.Sheet("Summary").Range("B5").Value = wb2.ActiveSheet.Range("B97") '<--- I'm getting an error here saying "Object doesn't support this property or method"
'I assume that this is not the right way to copy paste.
'I looked around but everything online uses a specific sheet name for destination
'which is not the case for me, it should be sheet with same name as in column A
wb1.Sheet("Summary").Range("C5").Value = wb2.ActiveSheet.Range("B117")
Case "Standard Kitchen Template"
wb1.Sheet("Summary").Range("B6").Value = wb2.ActiveSheet.Range("B97")
wb1.Sheet("Summary").Range("C6").Value = wb2.ActiveSheet.Range("B117")
Case "Standard Bathroom and Kitchen T"
wb1.Sheet("Summary").Range("B7").Value = wb2.ActiveSheet.Range("B97")
wb1.Sheet("Summary").Range("C7").Value = wb2.ActiveSheet.Range("B117")
End Select
Next cell
End Sub
Have updated and added a sheet variable (ws) which points to the relevant sheet for copying (it does not need to be selected or active).
Sub Summary()
Dim wb1 As Workbook
Dim Sht As Worksheet
Dim Rng, Rng2 As Range
Dim wb2 As Workbook
Dim cell As Range
Dim ws as Worksheet
Set wb1 = ThisWorkbook
Set wb2 = Workbooks("Measure Templates.xlsx")
Set Sht = wb1.Worksheets("Summary")
Set Rng = Sht.Range("A5:A" & Sht.Cells(Sht.Rows.Count, "A").End(xlUp).Row)
For Each cell In Rng
Set ws=wb2.Sheets(cell.Text)
Select Case ws.Range("A4").Value
Case "Standard Bathroom Template"
wb1.Sheet("Summary").Range("B5").Value = ws.Range("B97").Value
wb1.Sheet("Summary").Range("C5").Value = ws.Range("B117").Value
Case "Standard Kitchen Template"
wb1.Sheet("Summary").Range("B6").Value = ws.Range("B97").Value
wb1.Sheet("Summary").Range("C6").Value = ws.Range("B117").Value
Case "Standard Bathroom and Kitchen T"
wb1.Sheet("Summary").Range("B7").Value = ws.Range("B97").value
wb1.Sheet("Summary").Range("C7").Value = ws.Range("B117").Value
End Select
Next cell
End Sub
I have two sheets. One has the complete data and the other is based on the filter applied on the first sheet.
Name of the data sheet : Data
Name of the filtered Sheet : Hoky
I am just taking a small portion of data for simplicity. MY objective is to copy the data from Data Sheet, based on the filter. I have a macro which somehow works but its hard-coded and is a recorded macro.
My problems are:
The number of rows is different everytime. (manual effort)
Columns are not in order.
Sub TESTTHIS()
'
' TESTTHIS Macro
'
'FILTER
Range("F2").Select
Selection.AutoFilter
ActiveSheet.Range("$B$2:$F$12").AutoFilter Field:=5, Criteria1:="hockey"
'Data Selection and Copy
Range("C3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Hockey").Select
Range("E3").Select
ActiveSheet.Paste
Sheets("Data").Select
Range("D3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Hockey").Select
Range("D3").Select
ActiveSheet.Paste
Sheets("Data").Select
Range("E3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Hockey").Select
Range("C3").Select
ActiveSheet.Paste
End Sub
Best way of doing it
Below code is to copy the visible data in DBExtract sheet, and paste it into duplicateRecords sheet, with only filtered values. Range selected by me is the maximum range that can be occupied by my data. You can change it as per your need.
Sub selectVisibleRange()
Dim DbExtract, DuplicateRecords As Worksheet
Set DbExtract = ThisWorkbook.Sheets("Export Worksheet")
Set DuplicateRecords = ThisWorkbook.Sheets("DuplicateRecords")
DbExtract.Range("A1:BF9999").SpecialCells(xlCellTypeVisible).Copy
DuplicateRecords.Cells(1, 1).PasteSpecial
End Sub
I suggest you do it a different way.
In the following code I set as a Range the column with the sports name F and loop through each cell of it, check if it is "hockey" and if yes I insert the values in the other sheet one by one, by using Offset.
I do not think it is very complicated and even if you are just learning VBA, you should probably be able to understand every step. Please let me know if you need some clarification
Sub TestThat()
'Declare the variables
Dim DataSh As Worksheet
Dim HokySh As Worksheet
Dim SportsRange As Range
Dim rCell As Range
Dim i As Long
'Set the variables
Set DataSh = ThisWorkbook.Sheets("Data")
Set HokySh = ThisWorkbook.Sheets("Hoky")
Set SportsRange = DataSh.Range(DataSh.Cells(3, 6), DataSh.Cells(Rows.Count, 6).End(xlUp))
'I went from the cell row3/column6 (or F3) and go down until the last non empty cell
i = 2
For Each rCell In SportsRange 'loop through each cell in the range
If rCell = "hockey" Then 'check if the cell is equal to "hockey"
i = i + 1 'Row number (+1 everytime I found another "hockey")
HokySh.Cells(i, 2) = i - 2 'S No.
HokySh.Cells(i, 3) = rCell.Offset(0, -1) 'School
HokySh.Cells(i, 4) = rCell.Offset(0, -2) 'Background
HokySh.Cells(i, 5) = rCell.Offset(0, -3) 'Age
End If
Next rCell
End Sub
When i need to copy data from filtered table i use range.SpecialCells(xlCellTypeVisible).copy. Where the range is range of all data (without a filter).
Example:
Sub copy()
'source worksheet
dim ws as Worksheet
set ws = Application.Worksheets("Data")' set you source worksheet here
dim data_end_row_number as Integer
data_end_row_number = ws.Range("B3").End(XlDown).Row.Number
'enable filter
ws.Range("B2:F2").AutoFilter Field:=2, Criteria1:="hockey", VisibleDropDown:=True
ws.Range("B3:F" & data_end_row_number).SpecialCells(xlCellTypeVisible).Copy
Application.Worksheets("Hoky").Range("B3").Paste
'You have to add headers to Hoky worksheet
end sub
it needs to be .Row.count not Row.Number?
That's what I used and it works fine
Sub TransfersToCleared()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = Application.Worksheets("Export (2)") 'Data Source
LastRow = Range("A" & Rows.Count).End(xlUp).Row
ws.Range("A2:AB" & LastRow).SpecialCells(xlCellTypeVisible).Copy
Can some one please let me know how I can copy values from sheet1 and paste them to teh rest of existing values?
I have two sheets called "DTMGIS" and "DTMFinal" they have exactly same structure but I need to add values from "DTMGIS" to end of (I mean after Last Row) "DTMFinal"?
I already got this code from Soren at this Post which works for me on copying in an empty sheet but for appending data to existing I think I need something more
Sub CopyPasteValues()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("DTMGIS")
Set ws2 = ThisWorkbook.Sheets("DTMEdit")
ws1.Range(ws1.UsedRange.Address).Copy
ws2.Range("a1").PasteSpecial xlPasteValues
End Sub
Try this
Sub CopyPasteValues()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim rng1 As Range, rng2 As Range
Set ws1 = ThisWorkbook.Sheets("DTMGIS")
Set ws2 = ThisWorkbook.Sheets("DTMEdit")
With ws1
Set rng1 = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious)
Range(.Cells(1, 1), rng1).EntireRow.Copy
End With
With ws2
Set rng2 = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious)
If rng2 Is Nothing Then
.Cells(1, 1).PasteSpecial xlPasteValues
Else
rng2.EntireRow.Cells(2, 1).PasteSpecial xlPasteValues
End If
End With
End Sub
Updated to avoid UsedRange
This is a General Solution to ur Question, Soluton can be made more specific if you can provide some sample data to be copy pasted
General Solution as follows :
Sub CopyPaste()
'Copying Data
Sheets("DTMGIS").Activate
Range("A1").Select
' DataStart = Selection.Address
' Selection.End(xlToRight).Select
' Selection.End(xlDown).Select
' DataEnd = Selection.Address
' Range(DataStart, DataEnd).Select
' Uncomment The Above 5 lines if u have Multiple columns of data
' and Comment the Below line.
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Pasting data
Sheets("DTMFinal").Activate
If WorksheetFunction.CountA(Cells) = 0 Then 'Checking If Sheet has no data
Range("A1").Select
ActiveCell.PasteSpecial
Else
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.PasteSpecial
End If
End Sub
Feel Free to ask for any modifications in the code. As there was no sample data I have made some asumptions..