Automating row offset

itisananas

New Member
Joined
Jul 12, 2021
Messages
9
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hey all!

SO far i tried this vba code and it works, but the cons is that it only works for 12 rows and
i want is to make like it works for a bigger amount of rows.
I am kinda new to this things and just trying to figure it out.

What it does is its reads cell value in J5 and if that value is the same as the given value then in uses the given macro.
That macro always copies the value in cell B5 and paste it in the given cell G5,G6,G7 and so on.

J5 btw is a sum of the already excisting values in the column.

So what im trying to achief is the part where i can given i variable(i think) where it can automated the row part, that it goes on like +1,+1,+1

The VBA code i am using currently:
VBA Code:
Sub Vergelijking()

    If Range("J5").Value = 1 Then
    Macro1
   
    ElseIf Range("j5").Value = 2 Then
    Macro2
   
    ElseIf Range("J5").Value = 3 Then
    Macro3
   
    ElseIf Range("J5").Value = 4 Then
    Macro4
   
    ElseIf Range("J5").Value = 5 Then
    Macro5
   
    ElseIf Range("J5").Value = 6 Then
    Macro6
   
    ElseIf Range("J5").Value = 7 Then
    Macro7
   
    ElseIf Range("J5").Value = 8 Then
    Macro8
   
    ElseIf Range("J5").Value = 9 Then
    Macro9
   
    ElseIf Range("J5").Value = 10 Then
    Macro10
   
    ElseIf Range("J5").Value = 11 Then
    Macro11
   
    ElseIf Range("J5").Value = 12 Then
    Macro12
           
    End If
   
   
End Sub

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("G5").Select
    ActiveSheet.Paste
    Range("B5").Select
    Selection.ClearContents
End Sub
Sub Macro2()
'
' Macro2 Macro
'

'
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("G6").Select
    ActiveSheet.Paste
    Range("B5").Select
    Selection.ClearContents
End Sub
Sub Macro3()
'
' Macro3 Macro
'

'
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("G7").Select
    ActiveSheet.Paste
    Range("B5").Select
    Selection.ClearContents
End Sub
Sub Macro4()
'
' Macro4 Macro
'

'
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("G8").Select
    ActiveSheet.Paste
    Range("B5").Select
    Selection.ClearContents
End Sub
Sub Macro5()
'
' Macro5 Macro
'

'
    Range("B5").Select
    Selection.Copy
    Range("G9").Select
    ActiveSheet.Paste
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub
Sub Macro6()
'
' Macro6 Macro
'

'
    Range("B5").Select
    Selection.Copy
    Range("G10").Select
    ActiveSheet.Paste
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub
Sub Macro7()
'
' Macro7 Macro
'

'
    Range("B5").Select
    Selection.Copy
    Range("G11").Select
    ActiveSheet.Paste
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub
Sub Macro8()
'
' Macro8 Macro
'

'
    Range("B5").Select
    Selection.Copy
    Range("G12").Select
    ActiveSheet.Paste
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub
Sub Macro9()
'
' Macro9 Macro
'

'
    Range("B5").Select
    Selection.Copy
    Range("G13").Select
    ActiveSheet.Paste
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub
Sub Macro10()
'
' Macro10 Macro
'

'
    Range("B5").Select
    Selection.Copy
    Range("G14").Select
    ActiveSheet.Paste
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub
Sub Macro11()
'
' Macro11 Macro
'

'
    Range("B5").Select
    Selection.Copy
    Range("G15").Select
    ActiveSheet.Paste
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub
Sub Macro12()
'
' Macro12 Macro
'

'
    Range("B5").Select
    Selection.Copy
    Range("G16").Select
    ActiveSheet.Paste
    Range("B5").Select
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub
 

Attachments

  • Schermafbeelding 2021-07-12 172214.png
    Schermafbeelding 2021-07-12 172214.png
    19.1 KB · Views: 27

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.
How about
VBA Code:
Sub itisananas()
   With Range("J5")
      If .Value > 0 Then
         Range("B5").Copy Range("G" & .Value + 4)
         Range("B5").ClearContents
      End If
   End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub itisananas()
   With Range("J5")
      If .Value > 0 Then
         Range("B5").Copy Range("G" & .Value + 4)
         Range("B5").ClearContents
      End If
   End With
End Sub
Hey FLuff,

Thank you so much it works! :D
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
How about
VBA Code:
Sub itisananas()
   With Range("J5")
      If .Value > 0 Then
         Range("B5").Copy Range("G" & .Value + 4)
         Range("B5").ClearContents
      End If
   End With
End Sub

Uhm i got a extra question, what needs to be changed when i want this to apply on 2 different sheets? Lets say B5 is on Sheet 1 and the past and J5 value are on sheet 2?
I dont know if i need to make a new thread out of this, but if so then i will :)
 
Upvote 0
How about
VBA Code:
Sub itisananas()
   With Sheets("Sheet2").Range("J5")
      If .Value > 0 Then
         Sheets("Sheet1").Range("B5").Copy Sheets("Sheet2").Range("G" & .Value + 4)
         Sheets("Sheet1").Range("B5").ClearContents
      End If
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub itisananas()
   With Sheets("Sheet2").Range("J5")
      If .Value > 0 Then
         Sheets("Sheet1").Range("B5").Copy Sheets("Sheet2").Range("G" & .Value + 4)
         Sheets("Sheet1").Range("B5").ClearContents
      End If
   End With
End Sub
Okey i tried it oud but it seems it doesnt count anymore. I changed the sheet names, since my Excel is in Dutch and changed the copy paste cells.

VBA Code:
Sub itisananas()
   With Sheets("Blad6").Range("D2")
      If .Value > 0 Then
         Sheets("Blad5").Range("D3").Copy Sheets("Blad6").Range("E" & .Value + 4)
         Sheets("Blad5").Range("D3").ClearContents
       End If
   End With
End Sub
 

Attachments

  • Blad 5.png
    Blad 5.png
    11.4 KB · Views: 20
  • Blad 6.png
    Blad 6.png
    23.5 KB · Views: 24
Upvote 0
What do you mean "it doesn't count" ?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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