如何从VBA Sub中获取价值?
如何使用单元格中的Submacros中计算的值?
所以,如果我已经计算了一个macros中的value1,value2和value3,那么我怎样才能在例如一个单元格的计算中使用value1呢?
编辑:
这里是我的代码,运行良好,打印正确的值,当我使用Debug.Print,但我怎样才能使用值Price1,…,Price4进一步计算?
Sub GetPrice() Dim wb As Workbook Dim ws As Worksheet Dim ws1 As Worksheet Dim Rnge As Range Dim rRange As Range Dim rng As Range Dim catRange As Range Dim rCell As Range Dim sdate As Date Dim name As String Dim discount As Integer Set ws = Sheets("Sheet3") Set ws1 = Sheets("Report") Set rng = ws1.Range("B22") Set catRange = ws1.Range("W21:Z21") sdate = rng name = rng.Offset(0, -1).Value discount = 12 ' ActiveCell.Offset(0, 25).Value Set rRange = ws.ListObjects("Pricing").Range ws.ListObjects("Pricing").AutoFilter.ShowAllData If name = "SomeName" Then With rRange .AutoFilter Field:=2, Criteria1:="AA" .AutoFilter Field:=6, Operator:=xlFilterValues, Criteria2:=Array(2, Format(sdate, "yyyy-mm-dd")) .AutoFilter Field:=13, Criteria1:=discount For Each rCell In catRange.Cells If rCell = "SomeValue" Then .AutoFilter Field:=11, Criteria1:="AA" Set Rnge = .Offset(1, 0).SpecialCells(xlCellTypeVisible) Price1 = Rnge.Cells(1, 14).Value ElseIf rCell = "SomeName2" Then .AutoFilter Field:=11, Criteria1:="=AA", Operator:=xlOr, Criteria2:="=AA" Set Rnge = .Offset(1, 0).SpecialCells(xlCellTypeVisible) Price2 = Application.WorksheetFunction.Min(Rnge.Columns(14)) ElseIf rCell = "SomeName3" Then .AutoFilter Field:=11, Criteria1:="=AA", Operator:=xlOr, Criteria2:="=AA" Set Rnge = .Offset(1, 0).SpecialCells(xlCellTypeVisible) Price3 = Application.WorksheetFunction.Min(Rnge.Columns(14)) ElseIf rCell = "SomeName4" Then .AutoFilter Field:=11, Criteria1:="=AA", Operator:=xlOr, Criteria2:="=AA" Set Rnge = .Offset(1, 0).SpecialCells(xlCellTypeVisible) Price4 = Application.WorksheetFunction.Min(Rnge.Columns(14)) End If Next rCell End With End If ws.ListObjects("Pricing").AutoFilter.ShowAllData End Sub
包含在macros中:
Sheet.Cells(1,"A").Value = value1 Sheet.Cells(2,"A").Value = value2
等设置单元格B2的公式,其中
= A1 * A2
或者,在macros中包含公式:
result = value1*value2 Sheet1.Cells(1,"A").Value = result