Add a loop that takes user input, if user inputs 3 the loop should run through three times putting data on three separate lines

Nlhicks

Board Regular
Joined
Jan 8, 2021
Messages
244
Office Version
  1. 365
Platform
  1. Windows
This code works perfectly with only one input but if the user want to enter 3 different lines then I need it to capture all three lookups in different lines.

Sub DoMath()

Dim Ws As Worksheet
Dim i As Long

Set Ws = Sheets("Line Update")
For x = 1 To InputBox
For i = 0 To 1
If Ws.Cells(11, "C").Value <> Ws.Cells(11, "F").Value Then
Ws.Cells(13, "L").Value = Ws.Cells(11, "F") - Ws.Cells(11, "C")
End If

If Ws.Cells(13 + i, "C").Value <> Ws.Cells(13 + i, "F").Value Then
Ws.Cells(13, "M").Value = Ws.Cells(13, "F") - Ws.Cells(13, "C")
End If

If Ws.Cells(15 + i, "C").Value <> Ws.Cells(15 + i, "F").Value Then
Ws.Cells(13, "O").Value = Ws.Cells(15, "F") - Ws.Cells(15, "C")
End If

If Ws.Cells(17 + i, "C").Value <> Ws.Cells(17 + i, "F").Value Then
Ws.Cells(13, "P").Value = Ws.Cells(17, "F") - Ws.Cells(17, "C")
End If
Next i
Next x
End Sub

1668440919640.png
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi Nlhicks,

Maybe this:

VBA Code:
Option Explicit
Sub DoMath()

    Dim Ws As Worksheet
    Dim i As Long, j As Long
    Dim InputBox As Long
    
    Application.ScreenUpdating = False
    
    Set Ws = Sheets("Line Update")
    InputBox = 3
    
    For j = 0 To InputBox
        Ws.Cells(13, "L").Offset(0, j).Value = Ws.Cells(11 + (2 * j), "F") - Ws.Cells(11 + (2 * j), "C")
    Next j
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 0
Solution

Forum statistics

Threads
1,215,465
Messages
6,124,977
Members
449,200
Latest member
Jamil ahmed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top