根据条件在行中找到最小值的函数

时间:2019-07-02 10:05:22

标签: excel vba

我正在研究一个VBA代码,它将在另一个文件中打开一个现有工作簿,并将每一行的数据从所述工作簿复制到活动工作簿。

我想使用一个函数在其他工作簿的第11行中复制最小百分比。该函数必须在另一个工作簿的第11行中找到最小值,但前提是该最小值的绝对值小于100(因为该行同时包含百分比和常规值)。

我的代码已经打开了要从其中复制每一行数据的工作表,我只需要找到一种方法来复制最小百分比。

我要修改的粗体字。 我希望rng1成为整个第11行

任何帮助将不胜感激。

Sub StressTest()

Dim index As Integer
Dim dateColumn As Integer
Dim portfolioDate As String
Dim portfolioName As Variant
Dim ParametricVar As Double
Dim AuM As Double
Dim PreviousVar As Double
Dim PreviousAuM As Double
Dim strPath As String
Dim strFilePath As String
Dim wb As Workbook
Dim sheet As Worksheet
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Dim workB As Workbook
Dim min As Double
Dim max As Double


Set wb = ThisWorkbook
Set sheet = ActiveSheet


portfolioDate = InputBox("Please enter date under the following form : YYYY-MM", "Date at the time of Stress Test", "Type Here")
Debug.Print "Current portfolio date is: " & portfolioDate


For index = 34 To Cells(Rows.Count, "B").End(xlUp).Row

dateColumn = MatchHeader(portfolioDate)
portfolioName = ActiveSheet.Range("B" & index & "").Value


strPath = "G:\Risk\Risk Reports\VaR-Stress test\" & portfolioDate & "\" & portfolioName & ""

Set wb = Workbooks.Open(strPath)

ParametricVar = Workbooks(portfolioName).Worksheets("VaR Comparison").Range("B19")
PreviousVar = sheet.Cells(index, dateColumn + 7).Value
AuM = Workbooks(portfolioName).Worksheets("Holdings - Main View").Range("E11")
PreviousAuM = sheet.Cells(index, dateColumn + 9).Value

sheet.Cells(index, dateColumn).Value = ParametricVar / AuM
sheet.Cells(index, dateColumn + 1).Value = (ParametricVar - PreviousVar) / PreviousVar
sheet.Cells(index, dateColumn + 2).Value = AuM
sheet.Cells(index, dateColumn + 3).Value = (AuM - PreviousAuM) / PreviousAuM
**
Set ws1 = wb.Worksheets("Scenarios - Main View")
Set ws2 = wb.Worksheets("VaR - Main View")
Set rng1 = ws1.Range("P11:AA11")
Set rng2 = ws2.Range("J16:J1000")

min = Application.WorksheetFunction.min(rng1)
max = Application.WorksheetFunction.max(rng2)
sheet.Cells(index, dateColumn + 5).Value = min
sheet.Cells(index, dateColumn + 6).Value = max
sheet.Cells(index, dateColumn + 4).Value = Application.WorksheetFunction.min(rng1)
**


wb.Close Savechanges:=False

Next index




End Sub



0 个答案:

没有答案