简体   繁体   中英

VBA EXCEL copy range

I posted the same question a little while ago and was told that I had spelled sheets wrong. I took the question down; because I thought that I understood I was trying to call a method..that was my range (and yes sheet was spelled sheeet). I guess I still do not understand. I am trying to copy column "A2" through the last cell of column "D" from the sheet "TEXT" to Sheet3

Dim TotalRows As Integer
Dim CurrentWorkbook As Workbook ' macro workbook
Dim RangeToCopy As Range
Dim ColumnLetterNumber As String

Set CurrentWorkbook = ThisWorkbook
TotalRows = Sheets("TEXT").UsedRange.Rows.Count
ColumnLetterNumber = "D" & TotalRows


Set RangeToCopy = Range("A2", ColumnLetterNumber)

' Group Last Observed within the first three columns
' Last Observed start in column N
' After Insert end up in column )

 Columns("D:D").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

' Move the Last observed (now column O) to Column D so that they can be fitered together

Sheets("TEXT").Range(Range("O1"), Range("O1").End(xlDown)).Copy Sheets("TEXT").Range("D1")

With Sheets("TEXT")
 .AutoFilterMode = False
 .Range("A1:D1").AutoFilter
End With
'
'Copy Columns to the POAM sheet
Set RangeToCopy = Range("A2", ColumnLetterNumber) 'top left, bottom right
RangeToCopy.Select
CurrentWorkbook.Sheets("Sheet3").Activate


CurrentWorkbook.Sheets("TEXT").Range(RangeToCopy).Copy Action:=xlFilterCopy, _
CopyToRange:=Sheets("Sheet3").Range("A1"), unique:=True


End Sub

I answered your more detailed earlier question. This will not work because this line doesn't work for at least 2 reasons:

CurrentWorkbook.Sheets("TEXT").Range(RangeToCopy).Copy Action:=xlFilterCopy, _
    CopyToRange:=Sheets("Sheet3").Range("A1"), unique:=True

First, "RangeToCopy" is not a defined range on the sheet, so you cannot reference it in this way. It is a variable in your VBA code, so you just need to use the Copy Function on this range. Second, the Copy function of the Range Class does not have all of these parameters. You want:

RangeToCopy.Copy Destination:=Sheets("Sheet3").Range("A1")

If you are actually tryin get unique values, then you need to use the "AdvancedFilter" function.

RangeToCopy.AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Sheets("Sheet3").Range("A1"), unique:=True,  criteriarange:= <Insert range that you wish to get unique values from here> 

Also, you should define RangeToCopy based on the Sheet Name so:

Set RangeToCopy = Sheets("TEXT").Range("A2", ColumnLetterNumber)

Aren't you over doing this?

Sub Macro()

    Dim dEnd As Integer

    Sheets("Sheet1").Select

    Range("D1").Select
    dEnd = Selection.End(xlDown).Row

    Range("A2:" & "D" & dEnd).Copy

    Sheets("Sheet2").Select
    Range("A1").Select
    ActiveSheet.Paste

    Application.CutCopyMode = False

End Sub

(I didn't bother with sheet names)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM