+ Reply to Thread
Results 1 to 11 of 11

Macro to find the lowest value and cut/insert to a different cell

Hybrid View

  1. #1
    Registered User
    Join Date
    12-01-2017
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2212 Build 16.0.15928.20196) 64-bit
    Posts
    8

    Macro to find the lowest value and cut/insert to a different cell

    Hello to the VBA pros here,

    Is it possible for VBA macro to do this?
    I wish to highlight a section of cells and have it automatically select the lowest value out of each row, cut that cell and 1 cell to it's right and have it be inserted to the Z column.

    It's basically a bunch of prices and vendor names listed to the right of the price and i want to take the lowest price and have it be cut and inserted (has to be the insert function instead of paste) to the Z column, just by highlighting the cells i want the macro to affect.

    Example of my worksheet. I high-lite this entire thing and hit the macro.

    |12.00|vendor A|22.00|vendor B|15.00|vendor C
    | 5.00|vendor A| 7.00|vendor B| 8.00|vendor C
    |45.00|Vendor A|45.00|Vendor B|40.00|Vendor C <------ (I have no idea how the macro will affect prices that are the same, maybe have it ignore same prices and i will pick manually)

    Seems like quite a complicated macro. Appreciate it if any experts here can figure it out.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,690

    Re: Macro to find the lowest value and cut/insert to a different cell

    1) do you really need a macro to do this - can be domne with formulas (may be not real cutting, but listing lowest value and text (name) right to it.
    2) how about 2 (or more) equal lowest values in a row)
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    12-01-2017
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2212 Build 16.0.15928.20196) 64-bit
    Posts
    8

    Re: Macro to find the lowest value and cut/insert to a different cell

    Cause ultimately i want to actually move all the lowest value to one column for easy reference and to do calculations with formulas.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,690

    Re: Macro to find the lowest value and cut/insert to a different cell

    Try such code (made simple - will not be quick for large datasets):


    Sub test()
    Dim i As Long, j As Long, k As Long, minval As Double
    i = 1
    While Cells(i, "A") <> ""
      minval = WorksheetFunction.Min(Range(Cells(i, "A"), Cells(i, "W")))
      Range(Cells(i, "Z"), Cells(i, Columns.Count)).Clear
      Cells(i, "Z") = minval
      k = 27
      For j = 1 To 23 Step 2
        If Cells(i, j) = minval Then
          Cells(i, k) = Cells(i, j + 1)
          k = k + 1
        End If
      Next j
      i = i + 1
    Wend
    End Sub
    Attached Images Attached Images

  5. #5
    Registered User
    Join Date
    12-01-2017
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2212 Build 16.0.15928.20196) 64-bit
    Posts
    8

    Re: Macro to find the lowest value and cut/insert to a different cell

    Close but not quite. Attached file.

    I want to highlight I3 to N16 (or how many more vendors there are) and hit the macro. And it will move the lowest value and the corresponding vendor to the I and J column.

    Hope it's clear enough to understand what i'm trying to do.
    Attached Files Attached Files
    Last edited by Kyoukami; 12-06-2017 at 05:59 AM.

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,690

    Re: Macro to find the lowest value and cut/insert to a different cell

    please use attachment on the forum.

    SMALL sample Excel workbook(s) (10-20 rows of data is usually enough) Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. (If trere are typical cases like: all unique values/duplicates could occur, day/night, nobody present/several persons at once, before/on/past due, etc. - please show them all or at least indicate in text) The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution(s) is/are also shown (mock up the results manually). (also try showing the final (ultimate) step)

    3. Make sure that all confidential/restricted information (either personal or business) like real e-mails, social security numbers, bank accounts, etc. is removed first!!

    To attach an Excel file you have to do the following: Just before posting, scroll down and press Go Advanced button and then scroll down and press Manage Attachments link. Now follow the instructions at the top of that pop-up screen.

  7. #7
    Registered User
    Join Date
    12-01-2017
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2212 Build 16.0.15928.20196) 64-bit
    Posts
    8

    Re: Macro to find the lowest value and cut/insert to a different cell

    Quote Originally Posted by Kaper View Post
    please use attachment on the forum.

    SMALL sample Excel workbook(s) (10-20 rows of data is usually enough) Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. (If trere are typical cases like: all unique values/duplicates could occur, day/night, nobody present/several persons at once, before/on/past due, etc. - please show them all or at least indicate in text) The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution(s) is/are also shown (mock up the results manually). (also try showing the final (ultimate) step)

    3. Make sure that all confidential/restricted information (either personal or business) like real e-mails, social security numbers, bank accounts, etc. is removed first!!

    To attach an Excel file you have to do the following: Just before posting, scroll down and press Go Advanced button and then scroll down and press Manage Attachments link. Now follow the instructions at the top of that pop-up screen.
    I have edited my post to explain exactly what i want to achieve and attached a sample file.

  8. #8
    Registered User
    Join Date
    12-02-2017
    Location
    India
    MS-Off Ver
    2007
    Posts
    16

    Re: Macro to find the lowest value and cut/insert to a different cell

    This seems to work.

    May require some fine tuning and error handling as per your requirements.

    Sub FindMin()
        firstrow = Selection.Row
        FIRSTCOL = Selection.Column
        rowscount = Selection.Rows.Count
        colscount = Selection.Columns.Count
        
        For R = firstrow To firstrow + rowscount - 1
            minval = 999999999999#
            MINVALCOL = 0
            For c = FIRSTCOL To FIRSTCOL + colscount - 1 Step 2
                cellval = Val(Cells(R, c).Text)
                If (cellval <> 0) And (cellval < minval) Then
                    minval = cellval
                    MINVALCOL = c
                End If
            Next
            
            If MINVALCOL > FIRSTCOL Then
                Range(Cells(R, MINVALCOL), Cells(R, MINVALCOL + 1)).Select
                Selection.Cut
                Cells(R, FIRSTCOL).Select
                Selection.Insert Shift:=xlToRight
            End If
        Next
        
        MsgBox "Done"
        
    End Sub

  9. #9
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,690

    Re: Macro to find the lowest value and cut/insert to a different cell

    This shall work too (I started it earlier, but had to leave the office). If there are 2 or more minimal values all will be moved to column I, K, etc.
    Mote that moving cells can make your formula referring to column I invalid. To make it independant on moving cells you could either use MIN (you anyway look for lovest value) or INDIRECT function inside your CEILING
    Sub test()
    Dim arr, i As Long, j As Long, k As Long, startr As Long, startc As Long
    Dim minval As Double, helper1, helper2
    Application.ScreenUpdating = False
    arr = Selection.Value
    startr = Selection.Cells(1, 1).Row - 1
    startc = Selection.Cells(1, 1).Column - 1
    For i = 1 To UBound(arr)
      If IsNumeric(arr(i, 1)) And Len(arr(i, 1)) > 0 Then
        minval = arr(i, 1)
      Else
        minval = Application.Max(Selection)
      End If
      For j = 3 To UBound(arr, 2) - 1 Step 2
        If IsNumeric(arr(i, j)) And Len(arr(i, j)) > 0 Then
          If arr(i, j) <= minval Then
            Cells(startr + i, startc + j).Resize(1, 2).Cut
            Cells(startr + i, startc + 1).Resize(1, 2).Insert shift:=xlToRight
            minval = arr(i, j)
            Application.CutCopyMode = False
          End If
        End If
    Next j, i
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    12-01-2017
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2212 Build 16.0.15928.20196) 64-bit
    Posts
    8

    Re: Macro to find the lowest value and cut/insert to a different cell

    Quote Originally Posted by Kaper View Post
    This shall work too (I started it earlier, but had to leave the office). If there are 2 or more minimal values all will be moved to column I, K, etc.
    Mote that moving cells can make your formula referring to column I invalid. To make it independant on moving cells you could either use MIN (you anyway look for lovest value) or INDIRECT function inside your CEILING
    Sub test()
    Dim arr, i As Long, j As Long, k As Long, startr As Long, startc As Long
    Dim minval As Double, helper1, helper2
    Application.ScreenUpdating = False
    arr = Selection.Value
    startr = Selection.Cells(1, 1).Row - 1
    startc = Selection.Cells(1, 1).Column - 1
    For i = 1 To UBound(arr)
      If IsNumeric(arr(i, 1)) And Len(arr(i, 1)) > 0 Then
        minval = arr(i, 1)
      Else
        minval = Application.Max(Selection)
      End If
      For j = 3 To UBound(arr, 2) - 1 Step 2
        If IsNumeric(arr(i, j)) And Len(arr(i, j)) > 0 Then
          If arr(i, j) <= minval Then
            Cells(startr + i, startc + j).Resize(1, 2).Cut
            Cells(startr + i, startc + 1).Resize(1, 2).Insert shift:=xlToRight
            minval = arr(i, j)
            Application.CutCopyMode = False
          End If
        End If
    Next j, i
    Application.ScreenUpdating = True
    End Sub
    Works amazing!! Would love to give you more rep but can't for now

  11. #11
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,690

    Re: Macro to find the lowest value and cut/insert to a different cell

    Glad to hear it's working
    Reputation is nice, but it's not as important as satisfaction

    Would be lovely if you edit your post (as a matter of fact previous one too), to follow https://www.excelforum.com/forum-rul...rum-rules.html Rule 12

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] how to find the lowest to highest value and display the name who is the lowest and the hig
    By jasond1992 in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 09-23-2015, 09:20 PM
  2. Replies: 4
    Last Post: 01-07-2014, 03:38 AM
  3. Copy the entire lowest row > Select the row below said row > Insert copied cell
    By saturos in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2013, 11:22 AM
  4. VBA macro Find & Copy the five highest and lowest value and paste it to a Table
    By genera74 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-29-2013, 09:55 PM
  5. [SOLVED] Need Macro to find cell and insert a row above it.
    By Dnakr in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-23-2013, 03:17 PM
  6. macro to find first blank cell in rows and insert text
    By jlm13 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-21-2010, 06:27 PM
  7. Find lowest number in groups then lowest overall.
    By swieduwi in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-24-2008, 01:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1