Sub() issue, unable to start running

chanmeggichan

New Member
Joined
Nov 30, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm a newbie and self-learner so please bear with me.
So, Im trying to run a code that allows me to first copy and paste 2 input cells into another 2 cells, subsequently copying and pasting another range of cells into a Worksheet called "Year". This loop will continue until the dimension i meets the cut-off point.
Hence, I started with doing a "do until" loop.
However, this do until loop didn't work as it got stuck at the very first line (I assume that it was stuck there because there was a yellow arrow beside the code and an error message popped out which says "Invalid use of property")

Excel Formula:
Sub Test4()
Dim i As Integer
i = 0
Do While i <= (B58 * 4) - 1
    Range("D58").Copy Range("B15")
    Range("B59").Copy Range("B16")
    Range("B14:B55").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Year").Select
    Range("B2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
i = i + 1
Loop
End Sub

As this couldn't work, I tried For Next loop. But it couldn't give me the correct result too.

VBA Code:
Sub TEST()
Dim i As Integer
i = 0 'Quarter
For i = i To ((Range("B58")) * 4) - 1
    Range("D58").Copy Range("B15")
    Range("B59").Copy Range("B16")
    Range("B15:B56").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Year").Select
    Range("B2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    i = i + 1
Next
End Sub

I was sure that the Do Until loop is correct but Im not too sure now that the "DO Until" loop could not even work.
Please if anyone could just point me in the right direction and let me know why the loop does not work. Thank you and much appreciated~
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
instead of "Do While i <= (B58 * 4) - 1", try "Do While i <= (Range("B58").Value * 4) - 1"
 
Upvote 0
instead of "Do While i <= (B58 * 4) - 1", try "Do While i <= (Range("B58").Value * 4) - 1"
Thank you, it now works!

I have done a few changes, however, my code still doesn't loop although I wrote the do until - loop?

VBA Code:
Sub Year()
Dim lCol As Long
Dim i As Integer
i = 0 'Quarter

Do While i <= (Range("B58").Value * 4) - 1 'Runs the loops until the end of the no. of specified year (B58)
    Range("B58").Copy Range("B15")
    Range("B59").Copy Range("B16")
    Range("B15:B56").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Year").Select
    Cells(2, Columns.Count).End(xlToLeft).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
i = i + 1

Loop

End Sub
 
Upvote 0
What is the name of the intial active sheet, I am assuming it is not "Year" ?
After you switch to year you never switch back to the original sheet.
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,645
Members
448,974
Latest member
DumbFinanceBro

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