Changing Variables to Perform Same Task

mbpress01

New Member
Joined
Dec 30, 2017
Messages
20
Thanks in advance to everyone. I have the following code that allows me to select a list of names and post to a new range. It works well but my first pass was to just recreate code X times and change the selection criteria called REG to XXX and also change the variable to paste the selection to a new range DNRG_REG to DNRG_XXX. This is very inefficient having to run the procedures say 10 times but it works. I know there is an array approach and if anyone can point me in the right direction it would be great appreciated. I want to be able to setup variables and have the code loop through the each variable names and range for pasting. Thanks again.

Sub Tkr_REG()

'Find the last used row in a Column: column A in this example
Dim LastRow As Long
Dim i As Long, j As Long

With Worksheets("PosSrc")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Debug.Print LastRow
End With


''first row number where you need to paste values in Reg'
With Worksheets("PosSrc")
j = .Cells(.Rows.Count, DRNG_REG).End(xlUp).Row + 2
Debug.Print j
End With


For i = 1 To LastRow
With Worksheets("PosSrc")
If .Cells(i, 2).Value = "REG" And .Cells(i, 5).Value > 0 Then 'OR.Cells(i, 1).Value = 17 Then
.Cells(i, 1).Copy Destination:=Worksheets("PosSrc").Range(DRNG_REG & j)
j = j + 1
End If
End With
Next i
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
On a spare sheet list the variables you want to use, use your lastrow code with a different name i.e. LR then in an outer FOR NEXT Loop you can load that variable, run the code and then it picks the next one for you as does that action on the inner code
 
Upvote 0

Forum statistics

Threads
1,216,104
Messages
6,128,856
Members
449,472
Latest member
ebc9

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