使用变量时Range的语法

时间:2016-05-16 20:10:27

标签: vba excel-vba excel

我试图让我的代码选择一个单元格,然后使用.End(xlDown)。然后选择Selection.Copy来复制数据集,但是我无法让我的代码在我的循环中使用变量选择第一个单元格。我知道我的语法在范围函数上没有,但我不知道在哪里。任何帮助将不胜感激。

Sub Copy_Function_Data()
Dim Target As String
Dim X As Integer
Dim Y As Integer
Dim Target_2 As String
Dim Last_Row3 As Integer
Dim Last_Column3 As Integer
Dim Title_Count As Integer
Dim Country_Count As Integer
Dim Total_Count As Integer
Dim Title_Column As Long
Dim Country_Column As String

Y = ActiveWorkbook.Worksheets("Calculations").Range("B2", Worksheets("Calculations").Range("B2").End(xlDown)).Rows.Count

For X = 1 To Y

Sheets("Calculations").Select
Target = Range("B2").Offset(X, 0)
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = "TempData"
Worksheets("TempData").Range("A1").Value = Target
ActiveSheet.Range("A1").Select
    Selection.Replace What:="Open Position - ", Replacement:=" ", LookAt:=xlPart, _
    SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
Target_2 = Worksheets("TempData").Range("A1")
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = (Target_2)
Sheets("Raw Data").Select
Selection.AutoFilter
ActiveSheet.Range("$B$1:$Z$10000").AutoFilter Field:=5, Criteria1:= _
    (Target)
Range("B1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets(Target_2).Select
Range("B20").Select
ActiveSheet.Paste
Total_Count = ActiveWorkbook.Worksheets(Target_2).Range("B20", Worksheets(Target_2).Range("B20").End(xlDown)).Rows.Count
Last_Row3 = Cells.Find(What:="*", _
            After:=Range("A1"), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Row
Last_Column3 = Cells.Find(What:="*", _
            After:=Range("A1"), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Column
Title_Column = Cells.Find(What:="Title", _
            After:=ActiveCell, _
            LookIn:=xlFormulas, _
            LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False).Column
Country_Column = Cells.Find(What:="Country", _
            After:=ActiveCell, _
            LookIn:=xlFormulas, _
            LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False).Column


'************* This is where I need to add the code to copy and paste the range

Sheets("TempData").Delete






Next X
End Sub

1 个答案:

答案 0 :(得分:2)

您之后的语法是:

Range(Title_Column & "20")

因此您的代码可以像这样编写:

Range(Range(Title_Column & "20"), Range(Title_Column & "20").End(xlDown)).Copy

Range()方法可以通过以下几种方式使用:

'// return range covering everywhere between start_range and end_range
Range([start_range], [end_range])

'// This can be used with range or cells:
Range(Range("A1"), Range("C5"))
Range(Cells(1, 1), Cells(5, 3))
Range(Cells(1, "A"), Cells(5, "C"))

'// Pass a string argument as the identifier
'// Single range
Range("A1")
'// Multiple cell range
Range("A1:C5")
'// non-contiguous range
Range("A1,B1,C5")