根据条件从动态范围剪切粘贴单元格

时间:2019-05-07 20:22:37

标签: excel vba range paste cut

我对VBA很陌生。我需要根据开始月份的值将每月的资源加载向左或向右(月)移动。例如如果最初的开始月份值为May(即M5),并且资源加载是从M5月份开始直到M9月份,后来如果我将开始月份的值更改为7月(即M7),并运行此宏,它将正确地从月份转移资源加载M7到M11月。开始月份也可以向左更改,即从五月到四月,在这种情况下,资源加载应从M4个月开始直到M8个月。

上述操作需要循环执行,因为我有几条记录具有相似的数据,因此行号也会更改,但是“月份”值的列号范围是恒定的。

Project Start Date  Job Stage   Onsite/Offshore M1  M2  M3  M4  M5  M6  M7  M8  M9  M10 M11 M12 M13 M14 M15
1-Jul-2019  8   Offshore    0   0   0   0   0   0   0.2 0.2 0.2 0.2 0.2 0.2 0   0   0
1-Jul-2019  7   Offshore    0   0   0   0   0   0   1   0.5 0   0   0   0   0   0   0
1-Jul-2019  6   Offshore    0   0   0   0   0   0   1   1   1   1   0.5 0.5 0   0   0
1-Jul-2019  5   Offshore    0   0   0   0   0   0   0   0   1   1   0.5 0.25    0   0   0

Dim lastrowCDS As Long
Dim CDSProjectSDateText As String
Dim CDSProjectSDate As Date
Dim CDSProjectSMonth As Integer
Dim CDSMRange As Range
Dim CDSCell As Range
Dim CDSCurrPos As Range
Dim a As Integer
Dim CDSSheet As Worksheet

lastrowCDS = Workbooks("Resource_assignments").Worksheets("Combined Demand Sheet").Cells(Rows.Count, 1).End(xlUp).Row

Set CDSSheet = Workbooks("Resource_assignments").Worksheets("Combined Demand Sheet")

For i = 2 To lastrowCDS

    CDSProjectSDateText = Workbooks("Resource_assignments").Worksheets("Combined Demand Sheet").Cells(i, 7).Value
    CDSProjectSDate = CDate(CDSProjectSDateText)
    CDSProjectSMonth = Month(CDSProjectSDate)

    Set CDSMRange = Workbooks("Resource_assignments").Worksheets("Combined Demand Sheet").Range(Workbooks("Resource_assignments").Worksheets("Combined Demand Sheet").Cells(i, 17), Workbooks("Resource_assignments").Worksheets("Combined Demand Sheet").Cells(i, 40))

    For Each CDSCell In CDSMRange
        If CDSCell.Value <> "0" Then
             CDSCurrPos = CDSCell.Address
        End If
    Next CDSCell

    i = lastrowCDS

 Next i

例如如果最初的开始月份值为May(即M5),并且资源加载是从M5月份开始直到M9月份,后来如果我将开始月份的值更改为7月(即M7),并运行此宏,它将正确地从月份转移资源加载M7到M11月。开始月份也可以向左更改,即从五月到四月,在这种情况下,资源加载应从M4个月开始直到M8个月。

上述操作需要循环执行,因为我有几条记录具有相似的数据,因此行号也会更改,但是“月份”值的列号范围是恒定的。

0 个答案:

没有答案