按多列Excel

时间:2017-06-19 11:44:47

标签: excel vba excel-vba

我有一组超过1500行的数据,分布在几列中。我想在每列中找到最小的不同参数(对于每列,有5个参数,我想从每个列中找到最小值)。我在宏的帮助下写了一个VBA代码:

Sub MinimumValue()
    Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Parameter"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "300"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "700"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "1000"
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "1200"
    Range("F1").Select
    ActiveCell.FormulaR1C1 = "1600"
    '' stating the parameters

    Dim s1 As String
    Dim s2 As String
    Dim s3 As String
    Dim s4 As String
    Dim s5 As String

    s1 = Range("H1")
    s2 = Range("I1")
    s3 = Range("J1")
    s4 = Range("K1")
    s5 = Range("L1")

    Sheets.Add After:=Sheets(Sheets.Count)

    Range("B1").Select
    ActiveCell.FormulaR1C1 = "=MIN(Sheet1!B1:s1)"
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "=MIN(Sheet1!s1:s2)"
    Range("B3").Select
    ActiveCell.FormulaR1C1 = "=MIN(Sheet1!s2:s3)"
    Range("B4").Select
    ActiveCell.FormulaR1C1 = "=MIN(Sheet1!s3:s4)"
    Range("B5").Select
    ActiveCell.FormulaR1C1 = "=MIN(Sheet1!s4:s5)"
    Range("B1:B6").Select
    Selection.AutoFill Destination:=Range(Selection, Selection.End(xlToRight)), Type:=xlFillDefault
    Range("B1:J6").Select

我想在另一个工作表上粘贴不同的最小值,并尝试声明一个字符串,该字符串转换为一个单元格的范围,其中值取自单元格本身,但它告诉我#NONAME,因为它无法识别s1,s2 ,s3和s4。我尝试了一个间接和连接的公式,它只能用于一个列,我需要它用于几个列,这将花费大量时间来手动重写它们。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

很多东西可以让你的代码变得更好,但为了让它变得可行,试试这个:

ActiveCell.Formula = "=MIN(Sheet1!s1:s2)"

您应该使用FormulaR1C1代替s1What is the function of FormulaR1C1?

修改

要使用s2s5 ... Range("B1").Formula = "=MIN(Sheet1!B1:" & s1 & ")" Range("B2").Formula = "=MIN(Sheet1!B1:" & s2 & ")" 作为地址,请将代码修改为以下内容:

set county to 0
tell application "iTunes"
    set name of current track to songName
    set county to 1
    set artist of current track to songArtist
    set county to 2
    set genre of current track to genreVar
    set county to 3
    set grouping of current track to aFolder2
    set county to 4
    set comment of current track to fileName
    set county to 5
end tell
set county to 6

答案 1 :(得分:0)

使用变量会更像是这样。

Sub MinimumValue()
Dim sht As Worksheet
Dim rng As Range

    Range("A1").Value = "Parameter"
    Range("B1").Value = "300"
    Range("C1").Value = "700"
    Range("D1").Value = "1000"
    Range("E1").Value = "1200"
    Range("F1").Value = "1600"


    Dim s1 As String
    Dim s2 As String
    Dim s3 As String
    Dim s4 As String
    Dim s5 As String

    s1 = "H1"
    s2 = "I1"
    s3 = "J1"
    s4 = "K1"
    s5 = "L1"

   Set sht = Worksheets.Add(After:=Sheets(Sheets.Count))
   Set rng = sht.Range("B1:B6")

    With sht
    .Range("B1").Formula = "=MIN(Sheet1!B1:" & s1 & ")"
    .Range("B2").Formula = "=MIN(Sheet1!" & s1 & ":" & s2 & ")"
    .Range("B3").Formula = "=MIN(Sheet1!" & s2 & ":" & s3 & ")"
    .Range("B4").Formula = "=MIN(Sheet1!" & s3 & ":" & s4 & ")"
    .Range("B5").Formula = "=MIN(Sheet1!" & s4 & ":" & s5 & ")"
    End With
    rng.AutoFill Destination:=Range(rng, rng.End(xlToRight))
End Sub