Excel VBA:复制单元格内容

时间:2014-05-19 14:28:40

标签: excel vba excel-vba copy paste

我有一个excel程序,它运行产品配置的测试计划。在此程序中,用户单击"测试计划"按钮,然后将在每个产品上运行配置。发生这种情况时,会收集其他工作表中的信息,以了解成本和总计。我已经能够收集每列中的总成本,乘以费率,并将每个总数加在一起,以获得总人工成本,总材料成本,总人工MTO成本和总材料MTO成本。我非常希望将这些总计存储在另一张表格中,称为" Totals",以便我以后可以轻松监控和操作它们。

但是,当我尝试将这些值存储在我想要的单元格中时,它只返回最近的工作表值。 (重要的是要注意,当通过单击按钮运行程序时,旧数据的旧工作表将被删除并替换为较新的工作表。这意味着我不能在工作表中放置任何公式,它必须是VBA代码,我做不知道将制作多少张。)我尝试了很多不同的方法,其中两种我会发布。

    For i = 1 To 10
        wsTotals.Cells(1, i).Value = laborTotal
        wsTotals.Cells(2, i).Value = laborMTOTotal
    Next i

这里我试图将我计算的值存储在Totals表单上的单元格中。但是当它运行时,只复制了2个最新的值。 (第1行中的laborTotal,第1行中的A到J和第2行中的laborMTOTotal,A到J)。但是,不是这两个值,我的测试应该有8个,因为我有4张。

我也尝试过:

    wsNewSheet.Range("AN1:AO1").Copy 'I stored the totals out of the way of vision to try to copy them later
    wsTotals.Select
    wsTotals.Range("A1:J2").Select
    ActiveSheet.Paste

这返回了类似的结果。非常感谢任何帮助,如有必要,我可以提供更多信息。

编辑:我还应该补充一点,当程序运行时,会为用户在"测试计划"上输入的每个智能编号创建一个新工作表。片。在我的代码中,我只是循环遍历每个工作表以执行适当的任务。我的问题在于它并没有抓住每一个总数(或者它只是简单地用最先前的那些替换总数)。每张新工作表都存储在" wsNewSheet"。

好的,让我再来一次。这是我的代码:

Function Test_MLFB(sMLFB, sPattern) As Boolean
Set re = New RegExp
re.Pattern = "^" & Replace(sPattern, "…", "...") & "$"
re.IgnoreCase = False
Test_MLFB = re.Test(sMLFB)
End Function

Function Test_Option_Code(sCode, sPattern) As Boolean
Set re = New RegExp
bInvert = False
If Left(sPattern, 1) = "!" Then
    sPattern = Right(sPattern, Len(sPattern) - 1)
    bInvert = True
End If
re.Pattern = "^.*" & sPattern & ".*$"
If bInvert Then Test_Option_Code = Not (re.Test(sCode)) Else Test_Option_Code = re.Test(sCode)
End Function

Function Test_MLFB_OPTS(sConfiguration, sMLFBm, sOpt1m, sOpt2m, sOpt3m, sOpt4m, sOpt5m) As Boolean
Set re = New RegExp
re.Pattern = f_Lookahead(sOpt5m) & f_Lookahead(sOpt4m) & f_Lookahead(sOpt3m) & f_Lookahead(sOpt2m) & f_Lookahead(sOpt1m) & Replace(sMLFBm, "…", "...")
Test_MLFB_OPTS = re.Test(sConfiguration)
End Function

Function f_Lookahead(sOpt_mask) As String
    If sOpt_mask = "" Then
        f_Lookahead = ""
    ElseIf Left(sOpt_mask, 1) = "!" Then 'negative lookahead assertion
        f_Lookahead = "(?!.*" & Right(sOpt_mask, Len(sOpt_mask) - 1) & ")"
    Else 'positive lookahead assertion
        f_Lookahead = "(?=.*" & sOpt_mask & ")"
    End If
End Function

Function Run_Test()
Remove_Old_Instance_BoMs
Run_Test_Case

End Function

Function Remove_Old_Instance_BoMs()

iSheetCount = ThisWorkbook.Sheets.Count
For Each aSheet In ThisWorkbook.Sheets
    If Not (aSheet.Name = "Test Plan" Or aSheet.Name = "SBoM" Or aSheet.Name = "Transformer SN" Or aSheet.Name = "Tables" Or aSheet.Name = "Phase" _
    Or aSheet.Name = "bomcost.csv" Or aSheet.Name = "bomtrafocost.csv" Or aSheet.Name = "bomhourlyrate.csv" Or aSheet.Name = "base" _
    Or aSheet.Name = "digit 9" Or aSheet.Name = "digit 14" Or aSheet.Name = "digit 15" Or aSheet.Name = "options" Or aSheet.Name = "rates" _
    Or aSheet.Name = "opt short desc" Or aSheet.Name = "Totals" Or aSheet.Name = "bomcostINFO" Or aSheet.Name = "bomcost" Or aSheet.Name = "trans") Then
        'MsgBox ThisWorkbook.Sheets(i).Name
        aSheet.Delete
    End If
Next
End Function

Function Run_Test_Case()
Dim totals As Worksheet
Dim bApplies As Boolean
Dim buildRate As Long
Dim sysBuildRate As Long
Dim testRate As Long
Dim engRate As Long
'the following are columns of the SBOM
iType = 5
iPosNum = 6 'f
iQtyNum = 7
iMatlNum = 8
iMatlDesc = 9
imlfbmask = 11
iOpt1Mask = 12
iOpt2Mask = 13
iOpt3Mask = 14
iOpt4Mask = 15
iOpt5Mask = 16
inMTOmPartDesc = 20
inMTOmCost = 21
iTotalMatCost = 22
inMTOmDTKCost = 23
iTotalLabCost = 24
inMTOmHAWACost = 25
inMTOmCurrency = 26
inMTOmBuildHrs = 27
inMTOmSysBuildHrs = 28
inMTOmTestHrs = 29
inMTOmEngHrs = 30
inMTOmIndex = 31
iMTOmPartDesc = 32
iMTOmCost = 33
iMTOmTotalMatCost = 34
iMTOmDTKCost = 35
iMTOmTotalLabCost = 36
iMTOmHAWACost = 37
iMTOmCurrency = 38
iMTOmBuildHrs = 39
iMTOmSysBuildHrs = 40
iMTOmTestHrs = 41
iMTOmEngHrs = 42

For iRow = 2 To 152 'Rows of Test Plan
    Set curTestCase = Worksheets("Test Plan").Cells(iRow, 1)
    Set curMLFBCell = Worksheets("Test Plan").Cells(iRow, 2)
    Set curoptlistcell = Worksheets("Test Plan").Cells(iRow, 3)
    Set wsSBoM = Worksheets("SBoM")
    Set wsTotals = Worksheets("Totals")
    If Not (IsEmpty(curMLFBCell)) And Not (IsEmpty(curoptlistcell)) Then
        Set wsNewSheet = Worksheets.Add
        wsNewSheet.Name = curTestCase.Value
        Worksheets("Test Plan").Rows(iRow).Copy
        wsNewSheet.Rows("1:1").Select
        wsNewSheet.Paste

        iNewSheetRow = 2
        For iSBoMRow = 4 To 1271
            bApplies = False
            If Not IsEmpty(wsSBoM.Cells(iSBoMRow, imlfbmask)) Then
                bApplies = Test_MLFB_OPTS(curMLFBCell.Value & curoptlistcell.Value, wsSBoM.Cells(iSBoMRow, imlfbmask).Value, wsSBoM.Cells(iSBoMRow, _
                iOpt1Mask).Value, wsSBoM.Cells(iSBoMRow, iOpt2Mask).Value, wsSBoM.Cells(iSBoMRow, iOpt3Mask).Value, wsSBoM.Cells(iSBoMRow, _
                iOpt4Mask).Value, wsSBoM.Cells(iSBoMRow, iOpt5Mask).Value)
            End If

            If bApplies Then
                iNewSheetRow = iNewSheetRow + 1
                wsNewSheet.Cells(iNewSheetRow, 1).Value = wsSBoM.Cells(iSBoMRow, iPosNum).Value
                wsNewSheet.Cells(iNewSheetRow, 2).Value = wsSBoM.Cells(iSBoMRow, iQtyNum).Value
                wsNewSheet.Cells(iNewSheetRow, 3).Value = wsSBoM.Cells(iSBoMRow, iMatlNum).Value
                wsNewSheet.Cells(iNewSheetRow, 4).Value = wsSBoM.Cells(iSBoMRow, iMatlDesc).Value
                wsNewSheet.Cells(iNewSheetRow, 5).Value = wsSBoM.Cells(iSBoMRow, iType).Value
                If wsSBoM.Cells(iSBoMRow, iType).Value = 1 Then
                    wsNewSheet.Cells(iNewSheetRow, 6).Value = wsSBoM.Cells(iSBoMRow, inMTOmPartDesc).Value
                    wsNewSheet.Cells(iNewSheetRow, 7).Value = wsSBoM.Cells(iSBoMRow, inMTOmCost).Value
                    wsNewSheet.Cells(iNewSheetRow, 8).Value = wsSBoM.Cells(iSBoMRow, iTotalMatCost).Value
                    wsNewSheet.Cells(iNewSheetRow, 9).Value = wsSBoM.Cells(iSBoMRow, inMTOmDTKCost).Value
                    wsNewSheet.Cells(iNewSheetRow, 10).Value = wsSBoM.Cells(iSBoMRow, iTotalLabCost).Value
                    wsNewSheet.Cells(iNewSheetRow, 11).Value = wsSBoM.Cells(iSBoMRow, inMTOmHAWACost).Value
                    wsNewSheet.Cells(iNewSheetRow, 12).Value = wsSBoM.Cells(iSBoMRow, inMTOmCurrency).Value
                    wsNewSheet.Cells(iNewSheetRow, 13).Value = wsSBoM.Cells(iSBoMRow, inMTOmBuildHrs).Value
                    wsNewSheet.Cells(iNewSheetRow, 14).Value = wsSBoM.Cells(iSBoMRow, inMTOmSysBuildHrs).Value
                    wsNewSheet.Cells(iNewSheetRow, 15).Value = wsSBoM.Cells(iSBoMRow, iMTOmTestHrs).Value
                    wsNewSheet.Cells(iNewSheetRow, 16).Value = wsSBoM.Cells(iSBoMRow, iMTOmEngHrs).Value

                    wsNewSheet.Cells(iNewSheetRow, 18).Value = wsSBoM.Cells(iSBoMRow, inMTOmIndex).Value
                    wsNewSheet.Cells(iNewSheetRow, 19).Value = wsSBoM.Cells(iSBoMRow, iMTOmPartDesc).Value
                    wsNewSheet.Cells(iNewSheetRow, 21).Value = wsSBoM.Cells(iSBoMRow, iMTOmCost).Value
                    wsNewSheet.Cells(iNewSheetRow, 22).Value = wsSBoM.Cells(iSBoMRow, iMTOmTotalMatCost).Value
                    wsNewSheet.Cells(iNewSheetRow, 23).Value = wsSBoM.Cells(iSBoMRow, iMTOmDTKCost).Value
                    wsNewSheet.Cells(iNewSheetRow, 24).Value = wsSBoM.Cells(iSBoMRow, iMTOmTotalLabCost).Value
                    wsNewSheet.Cells(iNewSheetRow, 25).Value = wsSBoM.Cells(iSBoMRow, iMTOmHAWACost).Value
                    wsNewSheet.Cells(iNewSheetRow, 26).Value = wsSBoM.Cells(iSBoMRow, iMTOmCurrency).Value
                    wsNewSheet.Cells(iNewSheetRow, 27).Value = wsSBoM.Cells(iSBoMRow, iMTOmBuildHrs).Value
                    wsNewSheet.Cells(iNewSheetRow, 28).Value = wsSBoM.Cells(iSBoMRow, iMTOmSysBuildHrs).Value
                    wsNewSheet.Cells(iNewSheetRow, 29).Value = wsSBoM.Cells(iSBoMRow, iMTOmTestHrs).Value
                    wsNewSheet.Cells(iNewSheetRow, 30).Value = wsSBoM.Cells(iSBoMRow, iMTOmEngHrs).Value
                End If
            End If
        Next iSBoMRow

        wsNewSheet.Range("G1:AD1").Formula = "=SUM(G2:G152)"
        wsNewSheet.Range("G1:AD1").NumberFormat = "0.00"
        buildRate = (wsNewSheet.Cells(1, 13).Value * 123.35)
        MTOBuildRate = (wsNewSheet.Cells(1, 27).Value * 123.35)
        sysBuildRate = (wsNewSheet.Cells(1, 14).Value * 123.35)
        MTOSysBuildRate = (wsNewSheet.Cells(1, 28).Value * 123.35)
        testRate = (wsNewSheet.Cells(1, 15).Value * 126.22)
        MTOTestRate = (wsNewSheet.Cells(1, 29).Value * 126.22)
        engRate = (wsNewSheet.Cells(1, 16).Value * 97.14)
        MTOEngRate = (wsNewSheet.Cells(1, 30).Value * 97.14)

        laborBuild = (wsNewSheet.Cells(1, 10).Value + buildRate)
        laborSysBuild = (wsNewSheet.Cells(1, 10).Value + sysBuildRate)
        laborTestBuild = (wsNewSheet.Cells(1, 10).Value + testRate)
        laborEngBuild = (wsNewSheet.Cells(1, 10).Value + engRate)
        laborMTOBuild = (wsNewSheet.Cells(1, 10).Value + MTOBuildRate)
        laborMTOSysBuild = (wsNewSheet.Cells(1, 10).Value + MTOSysBuildRate)
        laborMTOTestBuild = (wsNewSheet.Cells(1, 10).Value + MTOTestRate)
        laborMTOEngBuild = (wsNewSheet.Cells(1, 10).Value + MTOEngRate)

        laborTotal = (laborBuild + laborSysBuild + laborTestBuild + laborEngBuild)
        laborMTOTotal = (laborMTOBuild + laborMTOSysBuild + laborMTOTestBuild + laborMTOEngBuild)

    End If
Next iRow
End Function

1 个答案:

答案 0 :(得分:1)

  

但是,当我尝试将这些值存储在我想要的单元格中时,它只返回最近的工作表值。

因为您的任何变量中只有一个(即"最近的")值。所以像这样的循环将一个最近的值放在10个不同的单元格中:

For i = 1 To 10
    wsTotals.Cells(1, i).Value = laborTotal
    wsTotals.Cells(2, i).Value = laborMTOTotal
Next i

这段代码完全按照你的要求去做。这可能与您想要要做的事情有所不同。那么,你实际上想要做什么呢?这是一个1 to 10循环,因此您在运行时创建的新工作表(其中152个)与此循环之间的关系不会立即显现。

因此,当i = 1时,laborTotal将根据wsNewSheet上的数据计算得出。

i = 2时,该值应该如何计算?

评论更新

我不明白为什么For i = 1 to 10...会在iRow循环中成为嵌套循环。正如你所解释的那样,我认为没有必要。

我认为这应该将laborTotal的值放在Totals工作表的第1行,以及laborMTOTotal工作表第2行中Totals的值。

    ...
    laborMTOTestBuild = (wsNewSheet.Cells(1, 10).Value + MTOTestRate)
    laborMTOEngBuild = (wsNewSheet.Cells(1, 10).Value + MTOEngRate)

    laborTotal = (laborBuild + laborSysBuild + laborTestBuild + laborEngBuild)
    laborMTOTotal = (laborMTOBuild + laborMTOSysBuild + laborMTOTestBuild + laborMTOEngBuild)

    '## ADD THESE LINES, or modify as needed:
    wsTotals.Cells(1, iRow).Value = laborTotal
    wsTotals.Cells(2, iRow).Value = laborMTOTotal

End If