Neater syntax

Paul Sansom

Board Regular
Joined
Jan 28, 2013
Messages
178
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
Hi
I have quite a few repetitions of the scrip below with similar changing values.
IS there a neater (For each) loop that i could simplify this code rather than just repeating it?

Code:
Private Sub Worksheet_Activate()


    MyRng = 4 + Sheet12.Cells(3, 5)
    MyRng = "E5:" & "E" & MyRng
    Sheet12.Range(MyRng).Copy
    Sheet5.Range("I4").PasteSpecial xlPasteValues, , , True


    MyRng = 4 + Sheet12.Cells(3, 7)
    MyRng = "G5:" & "G" & MyRng
    Sheet12.Range(MyRng).Copy
    Sheet5.Range("M4").PasteSpecial xlPasteValues, , , True



End Sub

All/Any suggestions gratefully received..... Paul
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How about
Code:
Private Sub Worksheet_Activate()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array(5, "I4", 7, "M4")
   For i = 0 To UBound(Ary) Step 2
      Cells(4, Ary(i)).Resize(Cells(3, Ary(i))).Copy
      Sheet5.Range(Ary(i + 1)).PasteSpecial xlPasteValues, , , True
   Next i
End Sub
 
Last edited:
Upvote 0
Or:

Code:
Sub test1()
Dim i As Long, x As Long

    For i = 0 To 1
        x = Sheet12.Cells(3, i * 2 + 5)
        Sheet5.Cells(4, 9 + i * 4).Resize(, x) = WorksheetFunction.Transpose(Sheet12.Cells(5, i * 2 + 5).Resize(x).Value)
    Next i
    
End Sub
But I'm not a huge fan of using sheet names in that fashion, and the calculations for figuring out the columns can be confusing, so you could do something like this:
Code:
Sub test1()
Dim Source As Variant, Target As Variant, i As Long, x As Long
    
    Source = Array(Sheets("Sheet12").Range("E5"), Sheets("Sheet12").Range("G5"))
    Target = Array(Sheets("Sheet5").Range("I4"), Sheets("Sheet5").Range("M4"))
    
    For i = 0 To UBound(Source)
        x = Source(i).Offset(-2).Value
        Target(i).Resize(, x).Value = WorksheetFunction.Transpose(Source(i).Resize(x).Value)
    Next i
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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