VBA在搜索值后定义范围

时间:2017-07-31 12:55:54

标签: excel vba excel-vba

我有一张工作表,其中包含B列中的周数。我正在尝试创建一个宏来查找Cell J2中的周数(或使用输入框)​​,然后在B列中查找周数选择数据在CG栏中,将它们复制到第2页(我可以复制等,它定义了我正在努力的范围。)Image of datasheet

以下代码显示了我需要复制和粘贴的信息。我努力让它找到周数,然后定义范围。下面的代码只进行了第4周。

"clean": "rimraf dist/**/*",
"build": "tsc",
"watch:start": "npm-run-all clean build --parallel --race watch:build watch:serve --print-label",
"watch:build": "tsc -w",
"watch:serve": "nodemon dist/index.js"

提前感谢您提供任何帮助

1 个答案:

答案 0 :(得分:1)

Sub copy()
Dim CopySheet As Worksheet
Dim PasteSheet As Worksheet
Set CopySheet = Worksheets("Sheet2")
Set PasteSheet = Worksheets("Sheet3")

Dim tableMaxCol As Integer
Dim tableMaxRow As Integer

Dim weekNumber As Integer

Dim row As Integer

tableMaxCol = CopySheet.Cells(1, CopySheet.Columns.Count).End(xlToLeft).Column
tableMaxRow = CopySheet.Cells(CopySheet.Rows.Count, 1).End(xlUp).row

weekNumber = CopySheet.Cells(2, 10).Value

For row = 2 To tableMaxRow step 4
    If CopySheet.Cells(row, 2).Value = weekNumber Then
        CopySheet.Range(CopySheet.Cells(row, 3), CopySheet.Cells(row + 3, 7)).copy _
            Destination:=PasteSheet.Range("C4")
    End If
Next row
End Sub

回答编辑。