Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

excel - Find Min Value and paste in at the end of a table VBA

I have a sheet with 2 tables. Both table have 2 columns but table2 has a column(2) with integers values. I want a piece of code that takes the minimum value of table2 column(2) and paste table2 Column(1) at the bottom of table(1).

Basically, the code will analyse table2, by finding the minimum value and will paste the column1 of table 2 at the bottom of table column1. (Table2 stays the same, while table1 increases).

I have no clue how to approach the problem. I have tried something but it does not work (See DOES NOT WORK in code). It somehow give me the result which is not the lowest. Am I missing something ?

Sub ssNewJoinerM()

Dim YesOrNoAnswerToMessageBox As String
Dim QuestionToMessageBox As String

    QuestionToMessageBox = "Do you want to add someone to a Hub ?"

    YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "New joiner Process")

If YesOrNoAnswerToMessageBox = vbYes Then
    GoTo Start
    Else: GoTo Finish
End If
' Double check if the user wants to start the process of adding a new employee to a Hub. If yes, start the Macro. If No, Finish now.

Start:


Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim ws4 As Worksheet
Dim ws5 As Worksheet
Dim ws6 As Worksheet
Dim ws7 As Worksheet
Dim ws8 As Worksheet

Set ws1 = ActiveSheet
Set ws2 = ActiveSheet
Set ws3 = ActiveSheet
Set ws4 = ActiveSheet
Set ws5 = ActiveSheet
Set ws6 = ActiveSheet
Set ws7 = ActiveSheet
Set ws8 = ActiveSheet

Set ws1 = ThisWorkbook.Sheets("Monthly Movements")
Set ws2 = ThisWorkbook.Sheets("Howard-Marle Hub")
Set ws3 = ThisWorkbook.Sheets("Bernard Hub")
Set ws4 = ThisWorkbook.Sheets("Thomas Hub")
Set ws5 = ThisWorkbook.Sheets("Michael Hub")
Set ws6 = ThisWorkbook.Sheets("Oliver Hub")
Set ws7 = ThisWorkbook.Sheets("Lance Hub")
Set ws8 = ThisWorkbook.Sheets("John Hub")

Dim table1 As ListObject
Dim table2 As ListObject
Dim table3 As ListObject
Dim table4 As ListObject
Dim table5 As ListObject
Dim table6 As ListObject
Dim table7 As ListObject
Dim table8 As ListObject
Dim table9 As ListObject
Dim table10 As ListObject
Dim table11 As ListObject
Dim table12 As ListObject
Dim table13 As ListObject
Dim table14 As ListObject
Dim table15 As ListObject

Set table1 = ws2.ListObjects("Table1")
Set table2 = ws2.ListObjects("Table2")
Set table3 = ws1.ListObjects("Table3")
Set table4 = ws3.ListObjects("Table4")
Set table5 = ws3.ListObjects("Table5")
Set table6 = ws4.ListObjects("Table6")
Set table7 = ws4.ListObjects("Table7")
Set table8 = ws5.ListObjects("Table8")
Set table9 = ws5.ListObjects("Table9")
Set table10 = ws6.ListObjects("Table10")
Set table11 = ws6.ListObjects("Table11")
Set table12 = ws7.ListObjects("Table12")
Set table13 = ws7.ListObjects("Table13")
Set table14 = ws8.ListObjects("Table14")
Set table15 = ws8.ListObjects("Table15")

' Declaration of my objects (tables, worksheets etc..)

Dim NewJoiner As String
NewJoiner = InputBox("Enter new joiner name in the following format (Surname, First Name)", "Adding New Joiner to Hub")
Dim Position As String
Position = InputBox("Enter new joiner Position (A, C, SC, PC, MP, Partner, Admin, Analyst, Director)", "Assigning New Joiner to a position")
'Input Name and Position and stores it (Could be improved with user form...)


If Position = "" Or NewJoiner = "" Then
    GoTo StringEmpty
    Else: GoTo StringNotEmpty
End If
'If Position or NewJoiner name are empty, end the process. Otherwise continue

StringNotEmpty:


Dim tbl As ListObject
Dim sht As Worksheet
Dim MyTable As ListObject

'Loop through each sheet and table in the workbook
For Each sht In ThisWorkbook.Worksheets
    For Each tbl In sht.ListObjects 'loop through all tables
        'To omit certain tables you can do the below
        If tbl.Name <> "Table2" And tbl.Name <> "Table3" And tbl.Name <> "Table5" And tbl.Name <> "Table7" _
        And tbl.Name <> "Table9" And tbl.Name <> "Table11" And tbl.Name <> "Table13" And tbl.Name <> "Table15" And tbl.Name <> "Table16" Then
            If MyTable Is Nothing Then
                Set MyTable = tbl 'set the table if not previously set
                Set MyWorksheet = sht 'set the worksheet if not previously set
            ElseIf tbl.ListRows.Count < MyTable.ListRows.Count Then   'if table rows is smaller than previously set one, reset
                Set MyTable = tbl
                Set MyWorksheet = sht
            End If
        End If
    Next tbl
Next sht

'DOES Not WORK
Dim Coach As String
Dim ws As Worksheet, t As ListObject, r As Long

    For Each t In MyWorksheet.ListObjects
        Select Case t.Name
            Case "Table1", "Table3", "Table4", "Table6", "Table8", "Table10", "Table12", "Table14", "Table16"
                'do nothing
            Case Else
                For r = t.DataBodyRange.Rows.Count To 1 Step -1
                    If t.DataBodyRange(r, 2) <= t.DataBodyRange(r + 1, 2) Then
                    Coach = t.DataBodyRange(r, 1)
                    End If
                Next r
        End Select
    Next t


' Adds the NewJoiner to the Hub with least members as long as the Hub as less than 50 employees
If MyTable.ListRows.Count <= 50 Then
        Set newrow1 = MyTable.ListRows.Add
        With newrow1
             .Range(1) = NewJoiner
             .Range(2) = Position
             .Range(3) = Coach
        End With
'Populates the monthly movemement tab with relevant information as long as the Hub as less than 50 employees
        Set newrow2 = table3.ListRows.Add
        With newrow2
              .Range(1) = NewJoiner
              .Range(2) = Position
              .Range(3) = MyWorksheet.Name
        End With


'Informative message for End-User as long as the Hub as less than 50 employees (Which Hub the NewJoiner has been added to)
MsgBox (NewJoiner + " has been added to the " + MyWorksheet.Name + "." & vbNewLine & vbNewLine & "Its details can be seen on the monthly movements tab.")
' If all the HUBS have more than 50 members, the programme does not  do anaything and ask for the creation of a New Hub
Else: MsgBox (" All the Hubs have more than 50 members !" & vbNewLine & vbNewLine & " A new hub needs to be created.")

End If

Finish:
StringEmpty:
'MsgBox (" You have not entered a Name or a Position for the new joiner !")

End Su

b

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's say your table 1 is on columns A and B and table 2 on columns D and E. Should be something like this:

With ActiveSheet
    .Range("A" & .Cells(Rows.Count, 1).End(xlUp).Row + 1).Value = .Range("D" & Application.Match(Application.Min(.Range("E:E")), .Range("E:E"), 0))
End With

Note: I used "Range" so you could easily edit the columns.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...