Help creating vba code

sofas

Active Member
Joined
Sep 11, 2022
Messages
469
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone, I tried to create this code, but it is very slow. Can someone help me to solve this problem. Thank you

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
Dim d As Variant
Dim f As Variant
Dim e As Integer
e = d + f
For d = 4 To 99
For f = 100 To 1000 Step 100
Cells(d, 1).Value = Val(Cells(d, 3).Value - Cells(d, 2).Value + Cells(d - 1, 1).Value)
Cells(d, 12).Value = Val(Cells(d, 11).Value * Cells(d, 10).Value)
Cells(d, 14).Value = Val(Cells(d, 13).Value * Cells(d, 10).Value)
Cells(d, 10).Value = Val(Cells(d, 9).Value * Cells(d, 8).Value)
Cells(d + f - 3, 1).Value = Val(Cells(d + f - 3, 3).Value - Cells(d + f - 3, 2).Value + Cells(d + f - 4, 1).Value)
Cells(d + f - 3, 12).Value = Val(Cells(d + f - 3, 11).Value * Cells(d + f - 3, 10).Value)
Cells(d + f - 3, 14).Value = Val(Cells(d + f - 3, 13).Value * Cells(d + f - 3, 10).Value)
Cells(d + f - 3, 10).Value = Val(Cells(d + f - 3, 9).Value * Cells(d + f - 3, 8).Value)
Next
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Does the code do what you want it to?
What does 'slow' mean to you? How long does it take?
 
Upvote 0
Test the following on a copy of your workbook:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
    Application.ScreenUpdating = False
'
    Dim e As Integer
    Dim LastRowInSheet  As Long
    Dim d As Variant
    Dim f As Variant
    Dim InputArray      As Variant
'
    LastRowInSheet = Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
'
    InputArray = Range("A1:N" & LastRowInSheet)                                                             ' Load data from sheet into 2D 1 Based InputArray
'
    e = d + f
'
    For d = 4 To 99
        For f = 100 To 1000 Step 100
             InputArray(d, 1) = InputArray(d, 3) - InputArray(d, 2) + InputArray(d - 1, 1)
            InputArray(d, 12) = InputArray(d, 11) * InputArray(d, 10)
            InputArray(d, 14) = InputArray(d, 13) * InputArray(d, 10)
            InputArray(d, 10) = InputArray(d, 9) * InputArray(d, 8)
'
             InputArray(d + f - 3, 1) = InputArray(d + f - 3, 3) - InputArray(d + f - 3, 2) + InputArray(d + f - 4, 1)
            InputArray(d + f - 3, 12) = InputArray(d + f - 3, 11) * InputArray(d + f - 3, 10)
            InputArray(d + f - 3, 14) = InputArray(d + f - 3, 13) * InputArray(d + f - 3, 10)
            InputArray(d + f - 3, 10) = InputArray(d + f - 3, 9) * InputArray(d + f - 3, 8)
        Next
    Next
'
    Range("A1:N" & LastRowInSheet) = InputArray                                                             ' Load 2D 1 Based InputArray back to the sheet
'
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,048
Messages
6,122,862
Members
449,097
Latest member
dbomb1414

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