Loop Within A Loop (repeat The Loop Values)

Mar 31, 2008

With Sheets("regrade pharm_standalone")
For Each r In .Range("standaloneTerritory")
If r.Value = "X101" Then
r.EntireRow.Copy
Sheets("X101").Range("A1").End(xlDown).Offset(1).PasteSpecial xlPasteValues
End If
Next r
End With
-------------------
I need to repeat this loop for values from X101 to X151. In all cases, the sheet name is equal to the value I'm looking up (eg: value = X102 goes to sheet X102).

I have a named range called 'territories' that contains the list of X101 -> X152.

I'm hoping to make the code perform the loop for each of the territories without my having to copy & paste and change the 'X101' 51 times as this would seem a rather silly thing to do!

View 9 Replies


ADVERTISEMENT

Repeat Or Loop VBA Statements

Aug 2, 2014

I am having trouble with trying to repeat or loop the below statements. I am trying to start with applying text in cell "B2" which will hide column "A" on a separate sheet. I want to know if it is possible to repeat the same statement but loop the statement until cell "SL2" and column "SK"?

[If Range("B2").Text = "" Then
Sheets("43mm").Columns("A").EntireColumn.Hidden = True
Else
Sheets("43mm").Columns("A").EntireColumn.Hidden = False
End If

View 3 Replies View Related

Loop Through Multiple Files And Call Macros (but Unable To Loop)

May 14, 2014

Macro which loops through a number of files and calls the same macro in each of them. Unfortunately when I add "Application.Run..." to the code, it no longer loops through the process and instead stops after updating the first file in the loop. If I remove the "Application.Run..." code and add any other code, the loop works fine and it continues through the process repeating all the steps for each file found.

Why it stops after one file when using "Application.Run..." to call the macros?

NB I have a list of path and file names starting in row 8 of columns A and C. Each file in the list has a macro called UpdateS1 and promoupdate1.

Sub C_Run_Loop_Macro()
Dim lastRow As Long
Dim i As Long

[Code]....

View 4 Replies View Related

Store Row Number Within Loop And Delete All Stored Rows After Loop?

Sep 11, 2013

I have working code that returns a row number within a for loop based on parameters I set.

Each time the for loop runs I would like to store this row number, then after the loop has finished, delete all stored rows.

Code:
for rowNum = 1 to x (some variable end row number which I already have worked out using End(xlUp).Row)
if x = y then
*storedRow = rowNum
end if
next rowNum
*

Lines with a * are the bits I can't work out. I've been trying to understand arrays by reading posts on what other people have done, but I can't fit (or fully understand) the reDims, or reDim preserves into my code. I've seen what appear to be quite complex ways involving uBounds and LBounds, but unfortunately I can't see how to use them.

All I want is to simply keep adding a row numbers to a variable, (i.e. row 2, 5, 20, 33, 120, etc) and then delete those specific rows.

View 4 Replies View Related

Loop Through Cells And Ranges Reverse Order With Backwards Loop

Aug 30, 2006

I am looping through each cell in a range and I would like to loop in reverse order.

Dim CELL As range
Dim TotalRows As Long
TotalRows = Cells(Rows.Count, 1).End(xlUp).Row
For Each CELL In Range("C1", "C" & TotalRows)
CELL.Select
'Code here to delete a row based on criteria
Next

I have tried:

For Each CELL In Range("C" & TotalRows, "C1")

and it does not make a difference. I need to loop in reverse order since what I am doing in the loop is deleting a row. I am looking at a cell and determining its value. If the value is so much, then the row gets deleted. The problem is that the next row "moves up" one row (taking the pervious cell's address) and therefore the For Each Next loop thinks it has already looked at that row.

View 7 Replies View Related

Copy Cells In Loop Based On Loop Increment Being Multiplied

Feb 7, 2008

I have some numbers in a column that I need to copy 12 times (each one) into another column. The problem is that I got like 200 records that will be converted in 15000 aprox. I've uploaded an example of what I need,

View 3 Replies View Related

Write Do Loop So It Will Repeat Below Until Row A Empty?

Nov 13, 2013

how do I write a Do Loop so it will repeat the below until row A is empty? I want the sheets to be added and named based on what someone enters on a data input sheet. i.e. if there are 5 rows with data, then it will create 5 sheets named accordinlgy.

Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Tab Organization").Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "ABC DEF"
Sheets("Sheet3").Select
Sheets("Sheet3").Name = "ABC DEF"

View 9 Replies View Related

Loop Through Index Worksheets Using For Loop And Select Clause

Nov 4, 2013

I have a workbook that contains, say, 50 worksheets: the first two worksheets summarise the data and are static in that they don't move position. However, the next four worksheets contain certain data for any given month. Each time a new month comes along, say, November, I insert four new worksheets after the two static ones as a result October's four worksheets are simply moved down the line in terms of worksheet order.

I need a macro to refer to the first six worksheets only (not the other tabs). I opted for index referencing for each worksheet, ie one - six. Now within these six worksheets in any given month, I need to sort the data by a certain column. The problem: in sheets 1,4,5 and 6 I need to rank by column E, but in sheets 2 and 3 I need to rank by column C. I have stepped through the code, which works for sheets 3-6, but doesn't seem to refer to sheets 1-2.

Sub WorksheetLoop()
'
' Loop through an indexed number of worksheets; _
' & this ensures that the worksheet range is dynamic _
' and is able to adjust when new sheets are added/removed, etc.
'
'Dim ws As Worksheet
Dim i As Long
Dim ws As Worksheet

[code]....

View 2 Replies View Related

Loop Or Repeat Recorded Sort Macro?

Aug 10, 2011

I have a workbook where I need to sort data with range (K2:L24), then move down 208 rows and sort range (K210:L232) and repeat this upto 1000 times. I have shortened a recorded macro as an example and would like to know how to loop or repeat.

Sub Macro2()
'
' Macro2 Macro
' Macro recorded 6/04/2011 by Greg

[Code].....

View 4 Replies View Related

Offset Repeat Loop Until Empty Cell

Aug 14, 2008

I would like click in cell a6 and then click a button loaded with VBA code that would:

1. Offset over 2 and down 1 to c7 so that is the current cell
2. Repeat down until an empty cell is reached (our example will be c10)
3. Put the items found in c7, c8, c9 into a variable (is that right?)

Variable would then be used in a vba generated email that I have all ready to go. I would want each item placed in the email one after the other like:

Name
Date
Phone

View 9 Replies View Related

Avoid Changing A Loop Counter Within A Loop?

Oct 24, 2009

I've worked on a solution for this thread (http://www.excelforum.com/excel-prog...-automate.html) but have been mentally challenged with how to avoid changing the loop counter in one of the loops I have used to resort an array of file names from the getopenfile dialog.

The aim of the shown code (see post 12 of the above link for attached file) is to check if the file containing the macro is included in the array returned by getopenfile while sorting the array of file names, and if so, moving it to the end of the array for "deletion" by redimming the array to exclude the last item. This problem of the open file being selected in the dialog may never arise, but... as the OP's request in the other thread was to allow two-way comparisons between numerous files, I've considered it likely enough to test for.

Here's the code I have settled for esp between the commented lines of hash symbols, which does change the counter (see the commented exclamation marks), but prevents an infinite loop (on my second try!) by using a second boolean flag of "HasCounterBeenChanged". Is there a better way of doing this? Or, alternatively (not in my thread title), is it possible to prevent the active file being selected through one of the arguments in the getopenfilename method?

View 3 Replies View Related

Copying From One Loop To Paste Into Another Loop?

Aug 27, 2012

I am working on some code that loops through a column of number values. Whenever it encounters a number value and a blank cell in an offset column, it places that number value into an offset cell (forming a separate column to be compared to another column in a separate sheet). I would like to take all the values in that new column and begin placing them in a new column in a separate sheet adjacent to another table. Most of the time, these values should match the adjacent values in the separate worksheet. However, if they don't match, I would like a new row to be created for that mismatched value.

For example. This is the first worksheet. So far, my macro loops through the column with rows 1-5. It looks in the offset cell(0,2) for Isempty value and then places that value into the offset cell(0,6)

text
1
text
text

[Code]....

View 5 Replies View Related

For Each Loop To Delete Row W/ Value (Loop Backwards)

Aug 2, 2007

For Each loop can be instructed to loop starting the bottom of the range. I know that a For To Loop can handle looping from the bottom up,

Sub Filterout()
Dim c As Range
Dim rng As Range
Dim i As Long
Dim lrow As Long
Dim counter As Integer
lrow = Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Range("c2:c36")

For Each c In rng
If Left(c.Value, 1) "~~" Then
c.EntireRow.Delete
End If
Next c

View 9 Replies View Related

Nested Loop. Inner Loop Not Running

Nov 30, 2006

i have a problem with a nested loop:

it seems like the first instance of the code is running the way i want it to run, but when it starts with the second instance, it does the first search and copy, but it seems like the nested loop is being ignored.

am i doing something wrong?

dan
==========================================================
Thanks to Aaron Blood for the find_range function. i also poached the lastrow function from somewhere on ozgrid, but I cant remember the name of the poster.
==========================================================

Sub new2()

Dim Org_Area As Variant
Dim Item As Variant
Dim Copy_To1 As Variant
Dim Cell_Ref As Variant

r = 1 ..................

View 9 Replies View Related

Do...Loop Error :: Loop Without Do

Dec 17, 2008

Can't seem to figure out why my Do Loop keeps giving me an Error of "Loop without Do".

View 14 Replies View Related

Loop Inside The Loop

Apr 28, 2009

I have got a loop which is working fine but now i need another loop which will run till the end but need to repeat itself as soon the column x become 1 the highest number would be 3

here is my main loop A1 = 5000
and second loop need to run inside the this loop

Sheet1.Activate
i = Range("A1")

For b = 1 To i
If Cells(1 + b, 3).Value = "P" Then
Cells(1 + b, 29).Value = 1
Else
If Cells(1 + b, 3).Value = "S" Then
Cells(1 + b, 29).Value = 2
Else
If Cells(1 + b, 3).Value = "C" Then
Cells(1 + b, 29).Value = 3
End If
End If
End If
Next b

View 9 Replies View Related

Endless Loop Within A Loop

Oct 28, 2006

I have loops working in other loops. The macro is almsot working well. It does the calculation i want but it fails to stop a loop, because of that, the macro can't run the next main loop (c), which is to move to the next cell where the calculations must be run.

I attach a file. the troubleshooting macroation is Sub Itiration.

The code of this macro are bellow. Basically, the loop using d as counter run into an endless loop. I don't how to stop this loop without affecting the results which are calculated correctly.

Sub Itiration()
Dim CurCell As Object
Dim TempSum As Double
Dim d As Integer

For c = 3 To Cells(3, 4)
If Cells(11, c) > 0 Then
For i = 1 To Cells(10, c)
Do

View 9 Replies View Related

Slow Down Loop At Each Loop

Nov 18, 2006

Going through a loop, I am trying to load pictures into an image box (or alternatively into a label) one by one i.e. going through the loop the first time, I want to load picture 1, then on the second loop, picture 2 and so on. A bit like an automated slide show.

I have written a simple loop and have used the loadpicture function to load the picture into the image box. When the code runs, the image box only gets populated after the last run through the loop. I have tried using application.screen updating function and the image.activate function without success. It is a simple bit of code and I expect an easy problem to solve if you know excel vba well.

View 6 Replies View Related

For Loop Goes Into An Infinite Loop

Apr 8, 2007

My code below goes into an infinite loop, with culprit line shown in bold and underline...

View 4 Replies View Related

Loop Gets Slower On Each Loop

Jul 20, 2007

I am parsing 15,000 files from a network server. The files are all the same format and length. The problem is that the first few iterations of my loop run fairly quickly, 7 to 9 seconds a case, but after only 300 iterations I'm up to 60 seconds a case. How do I keep the last iteration running as fast as the first iteration? I've included the main loop of my parsing routine below.

Sub Fill_Summary_Tabs() 'fills out the Datapack and JMP tabs
Application. ScreenUpdating = False 'turn off screen updating for speed

Call PrepImporterTab 'formats the Importer Tab so that everything runs smoothly < 1 second
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject") 'part of the filename test

For N = StartingCase To EndingCase '***** Start of the Parsing Loop *****

Sheets("Setup"). Cells(20 + N, 1).Value = Time 'Print the start time of each case....................

View 7 Replies View Related

Loop Through Column Values?

Feb 5, 2009

What the easiest way of looping though the values in a column in a macro? How to you "call" the cells in the macro?

View 4 Replies View Related

VBA - IF With LOOP To Return Values

May 31, 2012

1. look in column X to see IF there is an X
2. there's over a thousand rows to get through, and many of them have X's
3. IF the row has an X, I want to take some of the information from that row and transfer it into a new spreadsheet (the tricky part is I don't want to copy the entire row, but only certain columns in that row)
4. LOOP... so repeat for the next X down in the column X.... another tricky part is I don't know how to paste the information I want into 1 row lower in the new spreadsheet)

View 6 Replies View Related

Loop Through Combobox Values

Mar 30, 2013

I have two sheets in my workbook:

J_Database, a prototypical database
J_ComData, a custom worksheet that prints out an invoice-like document.

I have a combobox (cboName) in a userform that is populated by a dynamic range from J_Database. Once I hit my command button, I would like the program to loop through all of the records, one-by-one, and input the values from the combobox, and drop them in cell A1 of J_ComData.

From here, the remaining information about the combobox value from J_Database will be retrieved and J_ComData will be printed.

I was working with the following code:

Code:
Private Sub cmdPrint_Click()
If Trim(Me.cboName.Value) "" Then
'Unhide the printable paradigm sheet
Sheets("J_Pref").Select
Sheets("J_Para").Visible = True
'Drop judge paradigm userform information in the sheet
Sheets("J_ComData").Select

[code].....

This code works if I select just one name, have it populate A1 with that name, and then print out the document. However, writing the code so it will just print through all of them at one time.

View 6 Replies View Related

Loop Using Cell Values

Jun 4, 2014

I am attempting to create a loop for each row after cell B2. In column B I have a list of dates. I would like the macro to open the workbook titled "Name (date in cell B)" and copy two cells and paste in columns C and D of the Test.xlsm worksheet. I can do this for a single case but I want to loop for each date (I know the parts in red are incorrect).

In addition, I want to break the date in Cell B2 up into the various components i.e. year, month etc so I can make it more robust in opening a file (see the part highlighted in blue).

Code:
Sub Test()Dim varCellvalue As String
varCellvalue = Range("B2").TextWorkbooks.Open "\.....2014 6 JunName " & varCellvalue & ".xlsx"Windows("Name " & varCellvalue & ".xlsx").ActivateRange("C6:D6").SelectSelection.CopyWindows("Test.xlsm").ActivateRange("C2").SelectSelection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=FalseWindows("Name " & varCellvalue & ".xlsx").ActivateActiveWindow.Close
End Sub

View 5 Replies View Related

Loop That Loops Through The Values

Dec 5, 2007

On my sheet in Range("a4:a20") I have the names of the "Named Ranges" that I set referenicng ranges on different worksheets. Range("b4:b20") I count the # of rows for each of my "Named Ranges." I need a loop that loops through the values in column "B" and if it is > 0 then to take the corresponding "Named Range" in column "A" and paste it starting in cell "E6".

This is the start to my code it isn't working


Dim CellV As Range 'i want this to be the Cell Value
Dim CellValue_Range As Range 'this is the range B4:B20
Dim NRange As Range 'This is range A4:a20
lastrow = Range("a1").Value 'this counts the number of named ranges I am dealing with

For Each CellV In CellValue_Range
If CellV.Value > "0" Then
Range("a4").Value.Copy 'this range "a4" should change one row down each time it loops (this also correspondes to my "NRange"
Range("e" & lastrow).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
End If
Next CellV

View 9 Replies View Related

Insert Values From One Sheet To Another With A Loop

Oct 5, 2008

I need to insert values from a column in one sheet to another sheet with a Loop mentioned here.

(I have attached a sample workbook for your kind reference.)

IN STATEMENT SHEET, I NEED TO INSERT (IN col F) THE VALUES from Col A of NOS sheet.THE INSERTION SHOULD BE LOOPED AS MENTIONED HERE

i.e. First time, it should be 1 to 10
Second time it should be 2 to 10 and 1
Third time it should be 3 to 10 and 1, 2
Fourth time it should be 4 to 10 and 1,2,3
Fifth time it should be 5 to 10 and 1,2,3,4…. And so on, till the last row with a value in ColA.

THE VALUES IN Col A of NOS Sheet MAY BE CHANGED WHENEVER REQUIRED. There it is 1 to 10, but it may be Alphabets or any other words also.
Hence, whatever values in Col A of NOS sheet should be taken for looping.

View 8 Replies View Related

Loop Through Range Find Values?

Dec 6, 2013

I'm working with reports where I am given a list of used RFID tags which contain 13 alpha-numeric characters, and need to compare it against another list, in order to determine if any are matching. I'd like to be able to loop through one column of values to compare against the other, but unfortunately my VBA skills aren't that great..

View 5 Replies View Related

Loop To Print All Values In A List

Jul 14, 2006

I have a sheet that allows users to select a value from a drop down box. The box is linked to cell Y5, which updates values in a print range. A button located with the drop down box then prints the specified range.
Everything is functioning fine, but the process is more complicated than I'd like it to be, since users usually want printouts for every value on the list.

The values for the drop down box are located in a dynamic range in column A beginning with row 5. How would I go about creating a loop with VBA which transfers each value in column A to cell Y5, prints the result, then continues down the entire list?

View 4 Replies View Related

Macro Loop To Extract Specific Values And Min/Max Values From Column/Rows Range

Jun 3, 2009

Hi, I'm very new to writing Excel Macro's and wanted to know if I could do the following. Conceptually, I understand what I need done and think it should be fairly straightforward.

There's 2 main events in this loop (I hope that's the correct terminology):

Input 1) User defines the beginning cell to start the loop. In this case, A2.

Input 2) User defines the range of columns/rows to display. The formula for rows that I've thought of is 4r. So if a user wants 20 rows below cells A2, they simply input 5 for r. The number of columns is a constant 5. So if r=5, then I'd want the range to be A2:E22......

View 13 Replies View Related

Error Filling Array With Only Few Values Within Loop

Feb 26, 2014

I am trying to fill an array with only a few values within a loop (when XspacingA is 0.5, add the value of the Line to the array) and keep getting the error "Subscript out of range".

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved