VBA EXCEL copy range

左心房为你撑大大i 提交于 2019-12-13 02:47:17

问题


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

回答1:


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)



回答2:


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)



来源:https://stackoverflow.com/questions/28730624/vba-excel-copy-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!