简体   繁体   English

VBA EXCEL复制范围

[英]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). 因为我以为我知道我正在尝试调用一个方法..那是我的范围(是的,表被拼写为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 我试图将“ D2”列的最后一个单元格中的“ A2”列从表单“ TEXT”复制到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. 首先,“ RangeToCopy”不是工作表上定义的范围,因此您不能以这种方式引用它。 It is a variable in your VBA code, so you just need to use the Copy Function on this range. 它是VBA代码中的变量,因此您只需要在此范围内使用复制功能。 Second, the Copy function of the Range Class does not have all of these parameters. 其次,Range Class的Copy函数没有所有这些参数。 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. 如果实际上是在尝试获取唯一值,则需要使用“ AdvancedFilter”功能。

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: 另外,您应该基于图纸名称定义RangeToCopy,这样:

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) (我不打扰工作表名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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