定义单个单元格值以用于自动计算

时间:2015-06-11 08:57:11

标签: excel vba excel-vba

我正在尝试在我的VBA编码中定义一个formule,以考虑我工作簿中特定工作表的某个值。 有些人可以查看我的代码并告诉我我做错了什么吗?

' Macro used to create the labour demand
'
' define variables and tables

Dim wb As Workbook
Set wb = Application.ActiveWorkbook
Dim LabourDemand As Worksheet
Set LabourDemand = Sheets("Labour Demand")
Dim LaborPerFTE As Double
Set LaborPerFTE = Application.ActiveWorkbook.Worksheets("FTE Calculation").Range("C34").Value

' define connection string

Dim connectionString As String
connectionString = "ODBC;DSN=Excel Files;DBQ=" & Application.ActiveWorkbook.FullName & ";DefaultDir=" & Application.ActiveWorkbook.Path & ";Dri"

' clear out the old table

LabourDemand.Cells.Clear

然后该值应用于计算如下

'create new table

    With LabourDemand.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
        connectionString), Array("verId=1046;MaxBufferSize=2048;PageTimeout=5;")), _
        Destination:=LabourDemand.Range("$A$1")).QueryTable
        .CommandType = 2
        .CommandText = "SELECT `'Merged demand$'`.Category, `'Merged demand$'`.`Test Name`, `'Labor and equipment times$'`.`Equipment used`, `'Merged demand$'`.SampleDemandN, `'Merged demand$'`.SampleDemandN1, `'Merged demand$'`.SampleDemandN2, `'Merged demand$'`.SampleDemandN3, `'Labor and equipment times$'`.`Typical Batch Size`, `'Labor and equipment times$'`.typical, `'Labor and equipment times$'`.`Minimum batch size`, `'Labor and equipment times$'`.`1`, `'Labor and equipment times$'`.`Maximum batch size`, `'Labor and equipment times$'`.Max " _
        & " ,'=(([@[SampleDemandN]]/[@[Typical Batch Size]])*[@typical])/LaborPerFTE' as 'Labour needed for N (Typical)'" _
        & " ,'=(([@[SampleDemandN]]/[@[Maximum batch size]])*[@Max])/LaborPerFTE' as 'Labour needed for N (MAX)'" _
        & " ,'=(([@[SampleDemandN]]/[@[Minimum batch size]])*[@1])/LaborPerFTE' as 'Labour needed for N (MIN)'" _
        & " ,'=([@[SampleDemandN1]]/[@[Typical Batch Size]])*[@typical]' as 'Labour needed for N+1 (Typical)'" _
        & " ,'=([@[SampleDemandN1]]/[@[Maximum batch size]])*[@Max]' as 'Labour needed for N+1 (MAX)'" _
        & " ,'=([@[SampleDemandN1]]/[@[Minimum batch size]])*[@1]' as 'Labour needed for N+1 (MIN)'" _
        & " ,'=([@[SampleDemandN2]]/[@[Typical Batch Size]])*[@typical]' as 'Labour needed for N+2 (Typical)'" _
        & " ,'=([@[SampleDemandN2]]/[@[Maximum batch size]])*[@Max]' as 'Labour needed for N+2 (MAX)'" _
        & " ,'=([@[SampleDemandN2]]/[@[Minimum batch size]])*[@1]' as 'Labour needed for N+2 (MIN)'" _
        & " ,'=([@[SampleDemandN3]]/[@[Typical Batch Size]])*[@typical]' as 'Labour needed for N+3 (Typical)'" _
        & " ,'=([@[SampleDemandN3]]/[@[Maximum batch size]])*[@Max]' as 'Labour needed for N+3 (MAX)'" _
        & " ,'=([@[SampleDemandN3]]/[@[Minimum batch size]])*[@1]' as 'Labour needed for N+3 (MIN)'" _
        & " FROM `'Labor and equipment times$'` `'Labor and equipment times$'`, `'Merged demand$'` `'Merged demand$'`" _
        & " WHERE `'Merged demand$'`.`Test Name` = `'Labor and equipment times$'`.`Test Name` AND ((`'Labor and equipment times$'`.`Equipment used`='Manual labor'))"
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "LabourDemand"
        .Refresh BackgroundQuery:=False
    End With

' calculation of the values
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    For Each cell In Range(Cells(1, 14), Cells(LastRow, 25))
    cell.FormulaR1C1 = cell.Formula
    Next cell

End Sub

欢迎任何帮助。 作为最后一个问题,我如何调整计算部分,使其在正确的工作表上运行,这样我就不需要将它作为活动工作簿中的活动工作表。

1 个答案:

答案 0 :(得分:0)

更改以下行

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For Each cell In Range(Cells(1, 14), Cells(LastRow, 25))
cell.FormulaR1C1 = cell.Formula
Next cell

要...

with LabourDemand
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    For Each cell In .Range(.Cells(1, 14), .Cells(LastRow, 25))
        cell.FormulaR1C1 = cell.Formula
    Next cell
end with