excel vba循环最大值和位置更多工作表

时间:2016-12-29 20:55:03

标签: excel vba excel-vba

我需要最大值(一列)或地址的位置,因此我可以找到最大值单元格左侧的两个单元格。接下来是找到两个新单元格的较高值并将较高值除以最大值。最后一步是将值返回到Sheet" List1"。这是基本的逻辑:) thx任何帮助 最大值的定位和定位单元格,这是我的主要关注点。

我无法弄明白。一直在寻找它,但无法让它工作。

Sub DoIt()

    'ONE MAIN SHEET (List1)
    'MORE SECONDARY SHEETS WHERE DATA FOR MAX VALUE IS

    Dim strSheet As String
    Dim rng As Range
    Dim dblMax As Double
    Dim r As Range

    i = 4
    j = 8
    g = 4
    h = 20
    s = 22

    Dim cell As Range
    Dim col, row
    Do While Cells(i, j).Value <> ""

        strSheet = Sheets(1).Cells(i, j)
        Sheets(strSheet).Activate

        'w = 2
        'e = 27
        'a = 2

        'Do While Cells(a, s).Value >= "0"
        'Range("AA1") = "IT WORKS"
        'Cells(w, e) = Cells(a, s).Value
        'a = a + 1
        'w = w + 1

    'Loop

        Set rng = Sheets(strSheet).Range("V2:V8761")
        dblMax = Application.WorksheetFunction.Max(rng)

        'CODE FOR MAX VALUE LOCATION
        'LOCATING TWO LEFT CELLS OF LOCATION MAX VALUE CELL
        'DETERMINING THE HIGHER VALUE
        'DIVIDING

        Range("Z2") = dblMax    'CONTROL
        i = i + 1
        Range("Z1") = "IT WORKS"    'CONTROL

        Sheets("List1").Activate
        Cells(g, h) = "AAA"   'result of higher value cell by max value cell

        g = g + 1

    Loop
End Sub

1 个答案:

答案 0 :(得分:0)

我帮他做了这件事。代码不是refind。这是代码:

Sub DoIt()

Dim strSheet As String
Dim rng As Range
Dim dblMax As Double
Dim r As Range
Dim dely As Double

i = 4
j = 8

g = 4
h = 20

l = 21

s = 22

Dim cell As Range

Dim col, row

Do While Cells(i, j).Value <> ""

strSheet = Sheets(1).Cells(i, j)

Sheets(strSheet).Activate       'error on no strsheet

w = 2
e = 27
a = 2

sumall = Application.Sum(Range("v2:v9000"))

'Do While Cells(a, s).Value >= "0"

    'Range("AA1") = "nekaj dela"

    'Cells(w, e) = Cells(a, s).Value

    'a = a + 1
    'w = w + 1

'Loop

Set rng = Sheets(strSheet).Range("V2:V9000")

dblMax = Application.WorksheetFunction.Max(rng)

mmm = Application.WorksheetFunction.Match(dblMax,     Sheets(strSheet).Range("v:v"), 0)

positionRange = Sheets(strSheet).Range("v:v")

'iii = Application.WorksheetFunction.Index(positionRange, mmm)

'ooo = mmm.Offset(0, -1)
sum1 = Cells(mmm, 12)       
sum2 = Cells(mmm, 21)

If sum1 > sum2 Then

'sumall = Application.Sum(Range("l2:l8761"))

PLDP = sumall / 365

dely = sum1 / PLDP * 100
smer = "1"

Else

'sumall = Application.Sum(Range("u2:u8761"))

PLDP = sumall / 365

dely = sum2 / PLDP * 100
smer = "2"

End If

'Range("AA2") = iii
Range("AB2") = sum1
Range("AC2") = sum2
Range("ad2") = dely
Range("ae2") = sumall
Range("af2") = PLDP

Range("Z2") = dblMax    'test cell

i = i + 1

Range("Z1") = "IT WORKS"

Sheets("List1").Activate
Cells(g, h) = smer   
Cells(g, l) = dely

g = g + 1

Loop

End Sub