根据 2 个条件输入将行从一个 excel sheet 复制到另一个
copy row from one excel sheet to another based on 2 criteria inputs
我有一个 excel sheet,其中第一个 sheet 包含所有主数据,这些数据大约有 500 行并到达 R 列,这个 sheet 称为总体 Sheet。 O 列包括月份,P 列包括年份。
我有另一个 sheet,我希望在其中复制数据,这叫做 "Forecast Month"。在 B1 的顶部,选择了我要复制的月份,在 D1 中,选择了年份。我希望按钮读取这两个单元格并基于此从 "overall sheet" 复制数据。
我已经编写了如下所示的代码,但由于某种原因,在添加下一个(也是 10 次)之前,数据被输入 "forecast month" 10 次。我应该在这个 sheet 中只有 3 条数据,但实际上有 30 条,每条 10 条。
每行的前 3 行 sheet 都有标题,因此数据应该从第 4 行开始写入(确实如此)
有人能帮忙吗??
Private Sub CommandButton1_Click()
Dim month As String
Dim year As String
Dim c As Range
Dim d As Range
Dim k As Integer
Dim source As Worksheet
Dim targetforecastmonth As Worksheet
'change worksheet designations as needed
Set source = ActiveWorkbook.Worksheets("Overall Sheet")
Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")
targetforecastmonth.Range("A4:Z1000").Clear
month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")
k = 4
For Each c In source.Range("O4:O1000")
For Each d In source.Range("P4:P1000")
If c = month And d = year Then
source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
k = k + 1
End If
Next d
Next c
End Sub
试试这个,我希望这会有所帮助。
Private Sub CommandButton1_Click()
Dim month As String
Dim year As String
Dim c As Range
Dim k As Integer
Dim source As Worksheet
Dim targetforecastmonth As Worksheet
'change worksheet designations as needed
Set source = ActiveWorkbook.Worksheets("Overall Sheet")
Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")
targetforecastmonth.Range("A4:Z1000").Clear
month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")
k = 4
For Each c In source.Range("O4:O1000")
If c = month And source.Cells(c.Row, 16).Value = year Then
source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
k = k + 1
End If
Next c
End Sub
您有一个嵌套的 For Each
循环,这意味着您使用单元格 "O4"
然后循环遍历单元格 "P4:P1000"
,然后继续移动到单元格 "O5"
并循环遍历单元格 "P4:P1000"
等等...例如,如果 "O4"
的单元格值满足 month
条件,则每次循环遍历 P 列时都会找到满足 [=18= 的单元格] 标准,行号 4
将被复制并粘贴。
试试这个:
Private Sub CommandButton1_Click()
Dim month As String
Dim year As String
Dim c As Range
Dim d As Range
Dim x As Long
Dim k As Integer
Dim source As Worksheet
Dim targetforecastmonth As Worksheet
'change worksheet designations as needed
Set source = ActiveWorkbook.Worksheets("Overall Sheet")
Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")
targetforecastmonth.Range("A4:Z1000").Clear
month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")
k = 4
x = 4
For Each c In source.Range("O4:O1000")
Set d = source.Range("P" & x)
If c.Value = month And d.Value = year Then
source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
k = k + 1
End If
x = x + 1
Next c
End Sub
好像逻辑不对。
我想你需要 Expl.: O8, P8 matches B1, D1
所以你只需要一个周期:
For Each c In source.Range("O4:O1000")
d = source.Range("P" & k)
If c = month And d = year Then
source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
End If
k = k + 1
Next c
我有一个 excel sheet,其中第一个 sheet 包含所有主数据,这些数据大约有 500 行并到达 R 列,这个 sheet 称为总体 Sheet。 O 列包括月份,P 列包括年份。
我有另一个 sheet,我希望在其中复制数据,这叫做 "Forecast Month"。在 B1 的顶部,选择了我要复制的月份,在 D1 中,选择了年份。我希望按钮读取这两个单元格并基于此从 "overall sheet" 复制数据。
我已经编写了如下所示的代码,但由于某种原因,在添加下一个(也是 10 次)之前,数据被输入 "forecast month" 10 次。我应该在这个 sheet 中只有 3 条数据,但实际上有 30 条,每条 10 条。
每行的前 3 行 sheet 都有标题,因此数据应该从第 4 行开始写入(确实如此)
有人能帮忙吗??
Private Sub CommandButton1_Click()
Dim month As String
Dim year As String
Dim c As Range
Dim d As Range
Dim k As Integer
Dim source As Worksheet
Dim targetforecastmonth As Worksheet
'change worksheet designations as needed
Set source = ActiveWorkbook.Worksheets("Overall Sheet")
Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")
targetforecastmonth.Range("A4:Z1000").Clear
month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")
k = 4
For Each c In source.Range("O4:O1000")
For Each d In source.Range("P4:P1000")
If c = month And d = year Then
source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
k = k + 1
End If
Next d
Next c
End Sub
试试这个,我希望这会有所帮助。
Private Sub CommandButton1_Click()
Dim month As String
Dim year As String
Dim c As Range
Dim k As Integer
Dim source As Worksheet
Dim targetforecastmonth As Worksheet
'change worksheet designations as needed
Set source = ActiveWorkbook.Worksheets("Overall Sheet")
Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")
targetforecastmonth.Range("A4:Z1000").Clear
month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")
k = 4
For Each c In source.Range("O4:O1000")
If c = month And source.Cells(c.Row, 16).Value = year Then
source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
k = k + 1
End If
Next c
End Sub
您有一个嵌套的 For Each
循环,这意味着您使用单元格 "O4"
然后循环遍历单元格 "P4:P1000"
,然后继续移动到单元格 "O5"
并循环遍历单元格 "P4:P1000"
等等...例如,如果 "O4"
的单元格值满足 month
条件,则每次循环遍历 P 列时都会找到满足 [=18= 的单元格] 标准,行号 4
将被复制并粘贴。
试试这个:
Private Sub CommandButton1_Click()
Dim month As String
Dim year As String
Dim c As Range
Dim d As Range
Dim x As Long
Dim k As Integer
Dim source As Worksheet
Dim targetforecastmonth As Worksheet
'change worksheet designations as needed
Set source = ActiveWorkbook.Worksheets("Overall Sheet")
Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")
targetforecastmonth.Range("A4:Z1000").Clear
month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")
k = 4
x = 4
For Each c In source.Range("O4:O1000")
Set d = source.Range("P" & x)
If c.Value = month And d.Value = year Then
source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
k = k + 1
End If
x = x + 1
Next c
End Sub
好像逻辑不对。 我想你需要 Expl.: O8, P8 matches B1, D1 所以你只需要一个周期:
For Each c In source.Range("O4:O1000")
d = source.Range("P" & k)
If c = month And d = year Then
source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
End If
k = k + 1
Next c