|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Complex Looping Function
Hi,
I am looking to run the following macro, but i want to be able to say for Sheets("P0150*") where * is UM, UP, UI etc, and then the whole lot to run without me having to paste the same thing over and over again... Sheets("P0150UM").Select Sheets.Add Sheets("Sheet1").Name = "UM Sold" Sheets("P0150UM").Name = "UM Bought" Sheets("UM Bought").Select Range("A1").Select Call CountRow1 Dim range1 As String range1 = "A" & a + 2 Range(range1).Select Call CountRow2 Dim Range2 As String Range2 = "V" & a + 2 + b Range(range1, Range2).Select Selection.Cut Sheets("UM Sold").Select Range("A1").Select ActiveSheet.Paste Please can someone give me some help writing a loop to do this function? Many thanks in advance James |
|
#2
|
||||
|
||||
|
Code:
Dim sSheetName(number of sheets you have) As String
Dim lCntr As Long
Dim lMaxElements as Long
sSheetName(0) = "UM"
sSheetName(1) = "UP"
sSheetName(2) = "UI"
'continue for all sheets
lMaxElements = number of sheets you have starting from 0
For lCntr = 0 to lMaxElements
Sheets("P0150" & sSheetName(lCntr)).Select
Sheets.Add
Sheets("Sheet1").Name = sSheetName & " Sold"
Sheets("P0150" & sSheetName(lCntr)).Name = sSheetName(lCntr) & " Bought"
Sheets(sSheetName(lCntr) & "Bought").Select
Range("A1").Select
Call CountRow1
Dim range1 As String
range1 = "A" & a + 2
Range(range1).Select
Call CountRow2
Dim Range2 As String
Range2 = "V" & a + 2 + b
Range(range1, Range2).Select
Selection.Cut
Sheets(sSheetName(lCntr) & " Sold").Select
Range("A1").Select
ActiveSheet.Paste
Next lCntr
Something like this would probably work. |
|
#3
|
|||
|
|||
|
Thanks!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Complex Looping Function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|