简体   繁体   English

VBA /复制同一行和上一列中的最大列数和日期,然后粘贴到新表中

[英]VBA / Copy the Max of column and the the date in the same row and previous column and paste into new sheet

I want to find the max of a column and the the date in the same row and previous column and paste into a new sheet. 我想在同一行和上一列中找到一列的最大值和日期,然后粘贴到新表中。

在此处输入图片说明

Please, ignore the comments. 请忽略评论。 I tried over and over just hoping make it work but unfortunately didn't. 我一遍又一遍地尝试,只是希望它能起作用,但不幸的是没有。

Ia appreciate your time and help!!!!!!! Ia感谢您的时间和帮助!

Sub FloodFreqCurve()
'Dim MaxAddr As Variant
Dim MaxN As Integer
Dim rng As Range
Dim i As Integer
Dim Rw As Integer
Dim y As Integer
Dim CopyMax As Range
Dim a As Integer
Dim b As Integer



For i = 2 To 100 Step 2

Worksheets("Discharge").Activate

'MaxN = Worksheets("Discharge").Application.WorksheetFunction.Max(Columns(2))
'Columns(2).Find(MaxN, , xlValues).Row


'This part was from Snakehips
Set rng = Worksheets("Discharge").Columns(i)   'or whatever
Mx = WorksheetFunction.Max(rng)
Rw = WorksheetFunction.Match(Mx, rng, 0) + rng.Row - 1
'-------------------------------


If y = i - 1 > 0 Then
a = Cells(Rw, y).Value
b = Cells(Rw, a).Value


'CopyMax.Copy
'Cells(1, 1).Value = Rw
'Range(Cells(3, 1), Cells(3, 2)).Copy
'Range(
'Cells(Rw, i).Copy ', Cells(Rw, y)).Copy
'Selection.Copy
'CopyMax.Select
'Selection.Copy
'Range("A1").Paste
'MaxAddr = Application.WorksheetFunction.CELL("ADDRESS", Index(Columns(2), Match(Max(Columns(2)), Columns(2), 0)))
'RowNo = Application.WorksheetFunction.Match(Max(Columns(2)), Columns(2))
'MaxAddr.Select
'r = ActiveCell.Row
'ActiveSheet.Range(Cells(r, i), Cells(r - 1, i - 1)).Select
'Worksheets("FLOOD-FREQUENCY CURVE").Activate
'.Paste




End If

Worksheets("FLOOD-FREQUENCY CURVE").Activate
Cells(i, 1).Value = a
Cells(i, 2).Value = b '.PasteSpecial xlPasteAll


Next i

End Sub

I think this will do what you require. 我认为这将满足您的要求。 You can modify it to fit your needs, but it works fine on my side. 您可以对其进行修改以适合您的需求,但对我而言它可以正常工作。 The main sub to call is MoveMaxValuesFromColumns() . 要调用的主要子项是MoveMaxValuesFromColumns() You will notice that I used if dblTemp >= dblMax then add to max list. 您会注意到,如果dblTemp >= dblMax然后添加到max列表中,我就会使用它。 this can be changed to only get the maximum once by the following dblTemp > dblMax . 可以通过以下dblTemp > dblMax将其更改为仅获得一次dblTemp > dblMax Finally, I did the comparison using doubles however, you can modify it to use whatever value you prefer, even variants if you like. 最后,我使用双精度进行比较,但是,您可以修改它以使用您喜欢的任何值,甚至可以根据需要使用变体。 I hope this helps. 我希望这有帮助。

Option Explicit

Public Sub MoveMaxValuesFromColumns()
    Dim lngI As Long
    Dim strSheet As String
    Dim strCol As String
    Dim strSplit() As String

    Dim strFrom as string
    Dim strTo as string

    strFrom = "Sheet1"
    strTo = "Sheet2"   

    With ThisWorkbook.Worksheets(strFrom)
        For lngI = 2 To 100 Step 2
            strCol = .Cells(1, lngI).Address(ColumnAbsolute:=True)
            'Now, Parse the $'s out to get just the column!
            strSplit = Split(strCol, "$")
            strCol = strSplit(1)

            'call the MoveMax routine 
            MoveMax strCol, strFrom, strTo
        Next lngI
    End With
End Sub

Private Sub MoveMax(strInColumn As String, strFromSheet As String, strToSheet As String)
    Dim rng As Range
    Dim dblMax As Double
    Dim dblTemp As Double
    Dim strMySheet As String
    Dim strTransferSheet As String
    Dim lngLastRow As Long
    Dim lngI As Long
    Dim lngJ As Long
    Dim strOutVals() As String
    Dim strTemp As String
    Dim intCnt As Integer

    Dim lngColOffset As Long

    strMySheet = strFromSheet
    strTransferSheet = strToSheet

    With ThisWorkbook.Worksheets(strMySheet)
        lngColOffset = .Range(strInColumn & ":" & strInColumn).Column

        lngLastRow = .Range(strInColumn & .Range(strInColumn & ":" & strInColumn).Rows.Count).End(xlUp).Row

        Set rng = .Range(strInColumn & "1:" & strInColumn & lngLastRow).Cells

        dblMax = -1.79769313486231E+308  'Set the max to the double precision absolute minimum!
        ReDim strOutVals(0 To (rng.Rows.Count - 1), 0 To 1)
        For lngI = 1 To rng.Rows.Count
            strTemp = rng.Cells(lngI, 1).Value
            If IsNumeric(strTemp) Then
                dblTemp = CDbl(strTemp)
                If dblTemp >= dblMax Then
                    dblMax = dblTemp
                End If
            End If
        Next lngI

        'Now, loop through again and get the max's
        intCnt = 0
        For lngI = 1 To rng.Rows.Count
            strTemp = rng.Cells(lngI, 1).Value
            If IsNumeric(strTemp) Then
                dblTemp = CDbl(strTemp)
                If dblTemp >= dblMax Then
                    strOutVals(intCnt, 1) = rng.Cells(lngI, 1).Value
                    strOutVals(intCnt, 0) = rng.Cells(lngI, 1).Offset(0, -1).Value
                    intCnt = intCnt + 1
                End If
            End If
        Next lngI
    End With

    'Finally, Write out to new Sheet
    With ThisWorkbook.Worksheets(strTransferSheet)
        For lngI = 0 To (intCnt - 1)
            For lngJ = 0 To UBound(strOutVals, 2)  'This is just 1
                .Cells(lngI + 1, lngColOffset + lngJ - 1).Value = strOutVals(lngI, lngJ)
            Next lngJ
        Next lngI
    End With

    Set rng = Nothing

End Sub

Option Explicit

Sub test()

    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim LastRow1 As Long, LastCol1 As Long, LastRow2 As Long, Column As Long, Row As Long
    Dim iDate As Date
    Dim Amount As Double

    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set ws2 = ThisWorkbook.Worksheets("Sheet2")

    LastCol1 = ws1.Cells(2, ws1.Columns.Count).End(xlToLeft).Column

    For Column = 10 To LastCol1 Step 2

        With ws1

            LastRow1 = .Cells(.Rows.Count, Column).End(xlUp).Row

            Amount = 0
            iDate = Empty

                For Row = 3 To LastRow1

                    If .Cells(Row, Column).Value > Amount Then
                        Amount = .Cells(Row, Column).Value
                        iDate = .Cells(Row, Column - 1).Value
                    End If

                Next Row

        End With

        With ws2

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

            .Range("A" & LastRow2 + 1).Value = iDate
            .Range("B" & LastRow2 + 1).Value = Amount

        End With

    Next Column

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA如果第一行包含单词“结束日期”,则将工作表1的粘贴列复制到工作表2 - Excel VBA Copy Paste column from sheet 1 to sheet 2 if first row countain the word “End Date” VBA 从表 1 复制列并将粘贴转置到表 2 中的行 - VBA copy column from sheet 1 and transpose paste into row in sheet 2 复制并粘贴到另一个工作表Excel vba中的新列 - Copy and paste to new column in another sheet Excel vba 为多个工作表创建VBA,并使用公式将粘贴复制到新列中 - Create VBA for multiple sheet and copy paste in new column with formula VBA 复制列并粘贴为包含合并单元格的新行 - VBA to copy the column and paste as new row with merged cells VBA 用于过滤的宏,从列中复制指定值并创建然后粘贴到具有该列名称的新工作表中 - VBA Macro to filter, copy the specified value from a column and create then paste in a new sheet with that column name excel VBA复制仅粘贴行的列A? - excel VBA copy paste only column A of the row? VBA:在多列中找到最大值和最小值,并在另一列中找到最小值的匹配项并粘贴到新工作表中 - VBA: find the max and minimum value in multiple columns and the match for the minimum in another column and paste in a new sheet 从第二行复制一列数据,直到其中有数据的最后一行,然后粘贴为另一张表 VBA 上一列的最后一行 - Copy a column of data from the second row until the last row with data in it and paste as the last row in a column on another sheet VBA 使用VBA中的应用程序工作表功能从一个工作表中复制列并粘贴为另一行中的行 - Copy a column from one sheet and paste as row in another using application worksheetfunciton match in vba
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM