VBA to increase cell value by 1 and loop code

jbench18

New Member
Joined
Feb 12, 2013
Messages
24
Hi all, I have the code below that works as needed, but I'm looking at enhancing it further. I want run the code below, and then have cell F5 increase by 1 and then rerun the code below. I want to do this until F5 hits 40. Appreciate any help and assistance.


Code:
Range("P8").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Upload").Range("A" & Rows.Count).End(xlUp).Offset(1)
    .PasteSpecial Paste:=xlPasteValues
End With


Range("Q8").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Upload").Range("A" & Rows.Count).End(xlUp).Offset(1)
    .PasteSpecial Paste:=xlPasteValues
End With


Range("A8").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Upload").Range("B" & Rows.Count).End(xlUp).Offset(1)
    .PasteSpecial Paste:=xlPasteValues
End With


Range("A8").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Upload").Range("B" & Rows.Count).End(xlUp).Offset(1)
    .PasteSpecial Paste:=xlPasteValues
End With


Range("M8").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Upload").Range("C" & Rows.Count).End(xlUp).Offset(1)
    .PasteSpecial Paste:=xlPasteValues
End With


Range("M8").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("Upload").Range("C" & Rows.Count).End(xlUp).Offset(1)
    .PasteSpecial Paste:=xlPasteValues
End With
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try this

Code:
Sub Increase()
    Dim i As Double
    
    Application.ScreenUpdating = False


    For i = 1 To 40
    
        Range("F5").Value = i
        
        Range("P8").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        With Sheets("Upload").Range("A" & Rows.Count).End(xlUp).Offset(1)
            .PasteSpecial Paste:=xlPasteValues
        End With
        
        Range("Q8").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        With Sheets("Upload").Range("A" & Rows.Count).End(xlUp).Offset(1)
            .PasteSpecial Paste:=xlPasteValues
        End With
    
        Range("A8").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        With Sheets("Upload").Range("B" & Rows.Count).End(xlUp).Offset(1)
            .PasteSpecial Paste:=xlPasteValues
        End With
        
        Range("A8").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        With Sheets("Upload").Range("B" & Rows.Count).End(xlUp).Offset(1)
            .PasteSpecial Paste:=xlPasteValues
        End With
        
        Range("M8").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        With Sheets("Upload").Range("C" & Rows.Count).End(xlUp).Offset(1)
            .PasteSpecial Paste:=xlPasteValues
        End With
        
        Range("M8").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        With Sheets("Upload").Range("C" & Rows.Count).End(xlUp).Offset(1)
            .PasteSpecial Paste:=xlPasteValues
        End With
    Next


    Application.ScreenUpdating = True
    Application.CutCopyMode = False
    
    MsgBox "Finish"
End Sub
 
Upvote 0
Hi jbench

Few ways to do this, I'd use a while loop.



Code:
Dim i As Integer

i = Range("F5").Value


While i <= 40


'Put youre code here


Range("F5") = Range("F5") + 1
i = Range("F5").Value




Wend




End Sub
 
Upvote 0
Hi Dante works perfect! Appreciate the quick response and help.

I'm glad to help you. I appreciate your kind comments.


Summary Code:

Code:
Sub Increase()
    Dim i As Double
    
    Application.ScreenUpdating = False


    For i = 1 To 40
    
        Range("F5").Value = i
        
        Range("P8", Range("P8").End(xlDown)).Copy
        Sheets("Upload").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("Q8", Range("Q8").End(xlDown)).Copy
        Sheets("Upload").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("A8", Range("A8").End(xlDown)).Copy
        Sheets("Upload").Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        Sheets("Upload").Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("M8", Range("M8").End(xlDown)).Copy
        Sheets("Upload").Range("C" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        Sheets("Upload").Range("C" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
    Next


    Application.ScreenUpdating = True
    Application.CutCopyMode = False
    
    MsgBox "Finish"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,954
Members
449,198
Latest member
MhammadishaqKhan

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