+ Reply to Thread
Results 1 to 5 of 5

Execute same routine for multiple rows

Hybrid View

Neuk Execute same routine for... 04-20-2018, 02:53 PM
Fluff13 Re: Execute same routine for... 04-20-2018, 03:10 PM
Neuk Re: Execute same routine for... 04-20-2018, 03:44 PM
Neuk Re: Execute same routine for... 04-20-2018, 04:03 PM
Fluff13 Re: Execute same routine for... 04-20-2018, 04:16 PM
  1. #1
    Forum Contributor
    Join Date
    11-21-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    150

    Smile Execute same routine for multiple rows

    I have a code that works, but needs two additional features to make it worthwhile. To see how the code works, open up the attached file and follow the instructions. The two features I tried to add, but am struggling with are outlined in the problem section below.

    INSTRUCTIONS:
    (1) click the command button (G12) and an input box appears.
    (2) Select A15 for the inputbox.
    (3) A part number will then generate in AH of the active row.

    CONCEPT:
    The routine looks in each column M-Q of the active row for a value. If a value is found it then looks in column B of the active row for an identifier. Based on the value and the identifier a part number is generated.

    PROBLEM:
    (1) It only works for one row at a time. I'd like to be able select multiple rows - for example, A15:A19 using the input box and the part number generate for each row. I'm not sure how to do this...I did try adding a secondary input box but couldn't get that to work either.

    (2) I tried "Instr" on each case so that I can use the identifier word with other text present. For example, if the identifier is "XYZ" I'd like "XYZotherwords" to generate the part number also.

    Any help would be greatly appreciated!
    Attached Files Attached Files
    Last edited by Neuk; 04-20-2018 at 02:55 PM. Reason: Changed the file to correct one.

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,791

    Re: Execute same routine for multiple rows

    How about
    Sub AutoPart()
    
    Dim Rng As Range
    Dim X As Range
    
       Set Rng = Application.InputBox(Prompt:="Select a cell.", Title:="X-Axis.", Type:=8)
       For Each X In Rng
          If X.Offset(0, 12).Value Then 'If a value is present in column M Then
              Range(Cells(X.Row, 34).Address) = "P/N Proof" 'insert "P/N Proof" in column AG
          End If
          
          If X.Offset(0, 13).Value Then
              Range(Cells(X.Row, 34).Address) = "P/N Static Pressure"
          End If
          
          If X.Offset(0, 14).Value Then 'If a value is present in column O Then
              Select Case X.Offset(0, 1).Value 'Look in column B for one of the following cases:
                  Case Is = "XYZ"
                      Range(Cells(X.Row, 34).Address) = "P/N XYZ" 'If Case XYZ insert "P/N XYZ" in column AG
                  Case Is = "ZXY"
                      Range(Cells(X.Row, 34).Address) = "P/N ZXY"
                  Case Is = "YXZ"
                      Range(Cells(X.Row, 34).Address) = "P/N YXZ"
                  End Select
          End If
          If X.Offset(0, 15).Value Then
              Select Case X.Offset(0, 1).Value
                  Case Is = "123"
                      Range(Cells(X.Row, 34).Address) = "P/N 123"
                  Case Is = "321"
                      Range(Cells(X.Row, 34).Address) = "P/N 321"
                  End Select
          End If
          If X.Offset(0, 16).Value Then
              Select Case X.Offset(0, 1).Value
                  Case Is = "456"
                      Range(Cells(X.Row, 34).Address) = "P/N 456"
                  Case Is = "654"
                      Range(Cells(X.Row, 34).Address) = "P/N 654"
                  Case Is = "546"
                      Range(Cells(X.Row, 34).Address) = "P/N 546"
                  Case Is = "564"
                      Range(Cells(X.Row, 34).Address) = "P/N 564"
                  End Select
          End If
       Next X
    End Sub

  3. #3
    Forum Contributor
    Join Date
    11-21-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    150

    Re: Execute same routine for multiple rows

    Brilliant! It looks like you created a loop so that each X runs the routine within the range set by the input box. Thank you so much.

  4. #4
    Forum Contributor
    Join Date
    11-21-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    150

    Re: Execute same routine for multiple rows

    Don't worry about the "Instr" problem. I found that it works if I scratch the "select case" method and opt for the "if then elseif" statement

          If X.Offset(0, 15).Value Then
            If InStr(1, X.Offset(0, 1).Value, "123") Then
                Range(Cells(X.Row, 34).Address) = "P/N 123"
            ElseIf InStr(1, X.Offset(0, 1).Value, "321") Then
                Range(Cells(X.Row, 34).Address) = "P/N 321"
            End If

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,791

    Re: Execute same routine for multiple rows

    Glad to help & thanks for the feedback.

+ 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] Excel 2016 Sort routine issue - recording macro has sheet name in sort routine
    By pongmeister in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-05-2016, 02:45 PM
  2. [SOLVED] naming a routine and calling it in another routine - not a loop
    By HeyInKy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2015, 11:54 AM
  3. [SOLVED] How to use multiple instances of worksheet change routine
    By meprad in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2014, 06:44 AM
  4. [SOLVED] Code to execute multiple tasks such as deleting rows, inserting sums, setting print area
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2013, 04:18 PM
  5. [SOLVED] Passing Multiple ranges to Sub Routine
    By madhatter40 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2013, 04:19 PM
  6. Pass a variable from one sub-routine to another sub-routine
    By gowtham_pec in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-01-2013, 07:07 PM
  7. Execute VBA Routine on Enter Key
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-07-2009, 06:50 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