How to have formula auto fill down a row

blu817

New Member
Joined
Aug 18, 2017
Messages
20
Trying to do some income tax reporting for example if A1 income is X multiply by the following if its X do this instead. I am fairly new to VBA but how do I make it auto fill a range for example A1:A10? Here is the code I wrote and if there are better ideas I am open to that.




Dim income As Double
Dim tax As Double


income = Round(Range("A2").Value)
Range("A2").Value = income


Select Case income
Case Is >= 180001
tax = 55850 + 0.45 * (income - 180000)
Case Is > 80001
tax = 17850 + 0.38 * (income - 80000)
Case Is >= 35001
tax = 4350 + 0.3 * (income - 35000)
Case Is >= 6001
tax = 0.15 * (income - 6000)
Case Else
tax = 0

End Select


Range("B2").Value = tax




End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You can use For and next to loop though data, you simply replace the row number with your changing variable, you'd stick your select case income in a function but just put it up quickly so you'd get an idea of the for next loops

Code:
Sub tax()
Dim income As Double
Dim tax As Double
Dim i As Integer






For i = 1 To 10  ' i.e. A1 to A10


income = Round(Range("A" & i).Value)
Range("A" & i).Value = income




Select Case income
Case Is >= 180001
tax = 55850 + 0.45 * (income - 180000)
Case Is > 80001
tax = 17850 + 0.38 * (income - 80000)
Case Is >= 35001
tax = 4350 + 0.3 * (income - 35000)
Case Is >= 6001
tax = 0.15 * (income - 6000)
Case Else
tax = 0


End Select




Range("B" & i).Value = tax


Next i




End Sub
 
Last edited:
Upvote 0
@MrTeeny it worked like a charm!

What would I have to change on my code if column varied in number? For this one I knew my last row was 10 but my next one could be 22.
 
Upvote 0
Try this variation:
Code:
Sub tax()

[COLOR=#ff0000]    Dim lastRow As Long[/COLOR]
    Dim income As Double
    Dim tax As Double
    Dim i As Integer

[COLOR=#ff0000]'   Find last row in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row[/COLOR]
[COLOR=#ff0000][/COLOR]
    For i = 1 To [COLOR=#ff0000]lastRow[/COLOR]
        income = Round(Range("A" & i).Value)
        Range("A" & i).Value = income

        Select Case income
            Case Is >= 180001
                tax = 55850 + 0.45 * (income - 180000)
            Case Is > 80001
                tax = 17850 + 0.38 * (income - 80000)
            Case Is >= 35001
                tax = 4350 + 0.3 * (income - 35000)
            Case Is >= 6001
                tax = 0.15 * (income - 6000)
            Case Else
                tax = 0
        End Select

        Range("B" & i).Value = tax
    Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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