+ Reply to Thread
Results 1 to 8 of 8

Can't figure out how to get my offset function to work correctly in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    01-14-2009
    Location
    Great Bend, Pennsylvania
    MS-Off Ver
    Excel 2000
    Posts
    22

    Can't figure out how to get my offset function to work correctly in VBA

    The code works great fills in all the values. Problem is I'm already offsetting to get my "number1" but I need to offset "DIA" at the same time to get it to stay with the "number1".

    Basically I need to select two different cells and have them offset one cell down at a time together.

    Sub calctest()
    Dim number1 As Double
    Dim number2 As Double
    Dim answer As Double
    Dim SFM As Integer
    Dim RPM As Double
    Dim MAX As Integer
    Dim ALPHA As Integer
    Dim DIA As Double
    
    SFM = InputBox("What is the SFM?")
    ALPHA = InputBox("What Alpha symbol are you using?")
    MAX = InputBox("What is maximum RPM for machine?")
    DIA = Range("b23").Value
    
    Sheets("Feeds and Diameters").Select
    
    If ALPHA = 1 Then
    ALPHA = Range("d23").Select
    ElseIf ALPHA = 2 Then
    ALPHA = Range("e23").Select
    ElseIf ALPHA = 3 Then
    ALPHA = Range("f23").Select
    ElseIf ALPHA = 4 Then
    ALPHA = Range("g23").Select
    ElseIf ALPHA = 5 Then
    ALPHA = Range("h23").Select
    ElseIf ALPHA = 6 Then
    ALPHA = Range("i23").Select
    ElseIf ALPHA = 7 Then
    ALPHA = Range("j23").Select
    ElseIf ALPHA = 8 Then
    ALPHA = Range("k23").Select
    End If
    
    Do Until Selection.Offset(0, -2).Value = ""
    
    RPM = SFM * 3.82 / DIA
    If RPM > MAX Then
    RPM = MAX
    Else
    RPM = RPM
    End If
        
    number1 = Selection.Value
    number2 = Round(RPM, 0)
    
    answer = number1 * number2
    
    Selection.Offset(0, 13).Value = Round(answer, 1)
    
    Selection.Offset(1, 0).Select
    Loop
    
    End Sub
    I am soooo close to my desired outcome, feel free to ask any questions.
    I can send over the file I'm working on if interested as well
    Last edited by Dustin S.; 04-03-2009 at 01:50 PM. Reason: Rule #1

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    re: code works but need it to offset

    Dustin S.,

    What is the sheetname where DIA is getting its value?

    In the next loop, shoud DIA get its next value from Range("b24")?


    Have a great day,
    Stan
    Last edited by stanleydgromjr; 04-03-2009 at 12:44 PM.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    re: code works but need it to offset

    Welcome to the Forum

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    01-14-2009
    Location
    Great Bend, Pennsylvania
    MS-Off Ver
    Excel 2000
    Posts
    22

    Re: code works but need it to offset

    What is the sheetname where DIA is getting its value?
    For now all work is done on "Feeds and Diameters"

    In the next loop, shoud DIA get its next value from Range("b24")?
    exactly!!

  5. #5
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Can't figure out how to get my offset function to work correctly in VBA

    Dustin S.,

    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    
    Option Explicit
    Sub calctest()
    Dim number1 As Double, number2 As Double, answer As Double, RPM As Double, DIA As Double
    Dim SFM As Integer, MAX As Integer, ALPHA As Integer
    Dim NR As Long
    
    SFM = InputBox("What is the SFM?")
    ALPHA = InputBox("What Alpha symbol are you using?")
    MAX = InputBox("What is maximum RPM for machine?")
    
    
    NR = 23
    DIA = Sheets("Feeds and Diameters").Range("B" & NR).Value
    
    Sheets("Feeds and Diameters").Select
    
    If ALPHA = 1 Then
      ALPHA = Range("d23").Select
    ElseIf ALPHA = 2 Then
      ALPHA = Range("e23").Select
    ElseIf ALPHA = 3 Then
      ALPHA = Range("f23").Select
    ElseIf ALPHA = 4 Then
      ALPHA = Range("g23").Select
    ElseIf ALPHA = 5 Then
      ALPHA = Range("h23").Select
    ElseIf ALPHA = 6 Then
      ALPHA = Range("i23").Select
    ElseIf ALPHA = 7 Then
      ALPHA = Range("j23").Select
    ElseIf ALPHA = 8 Then
      ALPHA = Range("k23").Select
    End If
    
    Do Until Selection.Offset(0, -2).Value = ""
    
      RPM = SFM * 3.82 / DIA
      If RPM > MAX Then
      RPM = MAX
      Else
      RPM = RPM
      End If
          
      number1 = Selection.Value
      number2 = Round(RPM, 0)
      
      answer = number1 * number2
      
      Selection.Offset(0, 13).Value = Round(answer, 1)
      
      Selection.Offset(1, 0).Select
      
      NR = NR + 1
      DIA = Sheets("Feeds and Diameters").Range("B" & NR).Value
      
    Loop
    
    End Sub

    Have a great day,
    Stan

  6. #6
    Registered User
    Join Date
    01-14-2009
    Location
    Great Bend, Pennsylvania
    MS-Off Ver
    Excel 2000
    Posts
    22

    Re: Can't figure out how to get my offset function to work correctly in VBA

    EXACTLY what I was looking for!

    Would it be too much to ask what all you changed and why?

    Just trying to learn.

    Thank You either way for the code help.

  7. #7
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Can't figure out how to get my offset function to work correctly in VBA

    Dustin S.,

    Would it be too much to ask what all you changed and why?
    You wanted to get the value for "DIA" from B23, down.


    
    ' I created a variable NR, next row
    Dim NR As Long
    
    ' And set it 23, starting row number 23 from your original code
    NR = 23
    
    ' Then we loaded "DIA" from Sheets("Feeds and Diameters").Range("B23")
    DIA = Sheets("Feeds and Diameters").Range("B" & NR).Value
    
    
    ' We had to use the "Sheets("Feeds and Diameters").Range....) because of 
    '  the next code statement for your loop.
    Sheets("Feeds and Diameters").Select
    
    
      ' Then at the bottom of your loop I added 1 to NR
      NR = NR + 1
      
      ' Then we loaded "DIA" from the next row, B24, and so on for each pass of your loop
      DIA = Sheets("Feeds and Diameters").Range("B" & NR).Value

    Have a great day,
    Stan

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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