Loop Through A Range From Bottom To Top

Sep 30, 2013

I tend to use a lot of For Each loops when I want to go through a range looking for something but this time I'm not so sure it will work.

I have 9 databases each containing over 400 rows with different pieces of client information, these databases are shared between around 40 people (I've also used the 'Allow users to edit ranges' utility to restrict access). What this means however is that the users cannot delete an entire row, instead they can only 'clear contents' on individual cells. This is fine until I try to generate statistics from the databases using autofilters which stop at the first blank row.

So I need to write a backwards for each statement that starts at the bottom and deletes all the blank rows on the way up to the top. I've tried similar things to this before using the For Each but when you use it to delete a row it sometimes skips the next row as it continues at the next one.

I've not managed to get it to work before, I'm just looking for a little guidance really as to how to do this correctly.

VB:
For TopRow = 1 To BottomRow = Range("A1").SpecialCells(xlCellTypeLastCell).Row Step -1
'If is blank delete row.....
Next

View 1 Replies


ADVERTISEMENT

Loop Range Of Cells. Top To Bottom, Left To Right

Nov 7, 2006

I have a spreadsheet which is going to be populated everyday from figures from another spreadsheet using autofilters and the very useful SubTotal worksheet function. I'll attach a copy of the work in progress, also there will be a macro in here which Everytime the user presses update, it copies a range of cells and formatting and pastes it to the next available cell on the right. I've also drawn the movement of the cells i'd like The reason being, is that its going to give day to day, up to date figures from a datasheet.

The problem i have is that when i come to populate the grid which has been copied, i need a calculations to populate each cell in the Less<1week column, then when it gets to the bottom, move Back to the top, and accross to the right 1, and then populate down again, and so on until the end of the grid in question, i dont think i can use a for/next loop becuase it will be a different column address everytime. I dont really like loops, but i feel this needs it.

View 6 Replies View Related

Loop Through Columns And Sum Them At Bottom

Nov 17, 2008

I've been working on coding a summary sheet that pulls data from
worksheets in the active workbook.

I'm almost done, but am stumped at writing some code that will loop
through the columns that I have copied and total them. I recorded a
macro, but nothing happened when I ran it.

What I have now is this code snippet that I found which works
perfectly when I put my cursor in the active cell.
I am not sure how I should write it into my sub.

code that I found that works:

Set rng1 = ActiveCell.End(xlDown)
Set rng2 = rng1.End(xlUp)(2, 1)
rng1(3, 1).Formula = _
"=Sum(" & Range(rng2, rng1).Address(False, False) & ")"

I tried this:

For Each col In NewSh
ColNum = ColNum + 1
Range(RwNum, ColNum).Select

Set rng1 = ActiveCell.End(xlDown)
Set rng2 = rng1.End(xlUp)(2, 1)
rng1(3, 1).Formula = _
"=Sum(" & Range(rng2, rng1).Address(False, False) & ")"

Next col

View 9 Replies View Related

Function Macro Debug: Continue Onto The Do Loop At The Bottom That Does All The Work

Aug 9, 2006

I am having a problem with a custom funciton I am trying to create. It will exit after it is finished with the IF Then Else statement. I need it to continue onto the Do loop at the bottom that does all the work.

Function UPCECheck( num As String) As Long
Dim CheckNum As Long
Dim TempCheck As Long
Dim X As Long
Dim Holdtxt As Variant
UPCECheck = 0
CheckNum = 0
Debug.Print Len(num)
If Len(num) = 12 Then
Holdtxt = num
ElseIf Len(num) < 12 Then
Holdtxt = "000000000000" & num
Holdtxt = Val(Mid(Holdtxt, Len(holdtext) - 12, 12))
End If...................

View 6 Replies View Related

Finding Bottom Row Of A Range In VBA

Nov 14, 2008

How can I determine what the bottom row is in a range in VBA? I have an SheetChange event sub that takes in Target as Range. I want to know what the first/last row/column is in the Range. So, for example, say the Sheet has values in A1:B5 and I paste over A1:B4. Target will be A1:B4. I need a method that returns 4. I tried Target.End(xldown).row, but that gives me 5 (since theres data in A5).

View 4 Replies View Related

Determining Bottom Of A Range?

Sep 13, 2013

I've recorded a macro that selects a bunch of cells so I can work with them. However, it's hard-coded to the bottom cell of H1551, and I need it to work no matter how large the range is.

Code:
''' Concatenate column H with B & F
Application.Goto Reference:="R2C8"
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-6],"" "",RC[-2])"

[Code]....

View 4 Replies View Related

Find Top/bottom 3 Values From A Range

Aug 13, 2008

I now have everything set up to do exactly what I want except this last bit.

All I want is for the overview page to show the top three referrers for each month. At the moment, it's finding the top and bottom 'scorer' but if more than one has the same score I can only see the first alphabetically.

View 10 Replies View Related

VLookup To Start At The Bottom Of Range Instead Of Top?

Dec 17, 2012

2012PlayerStats.sample1.xlsx

I have a list of players and the team they play for in Columns A and B.

In column E I have created a list of only unique names and in F I need the Team the played for last. However with Vlookup I am only getting the team they played for first. How can I get the team they played for last?

I have tried vlookup and index/match and get the same.

View 3 Replies View Related

Inverting Range (top To Bottom) In Same Location

Apr 11, 2012

I am in need if inverting a Range (top to bottom), in the same location, like this ....

1 2 3
4 5 6
7 8 9

to...

7 8 9
4 5 6
1 2 3

How would this be done ?

View 9 Replies View Related

Copy Data In Row And Paste To The Bottom Of Another Range?

Aug 2, 2012

The following code is doing what I want it to do in part, its copying the data in range k3 to q3 to the next empty row in columns a to f

It works when I change any cell in column K (11), I want to change to only on a change in K7 or K8? As an afterthought if I'm dealing with the same sheet do I need to make reference to sheet2?

VB:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextRow As Long
If Target.Column = 11 And Target.Cells.Count = 1 Then

[Code].....

View 2 Replies View Related

Is There A Macro To Transfer A Row Of Data From Top To Bottom Row Of Range ?

Oct 23, 2008

I have a s/s encompassing over 350,000 rows.

Data consists of a series of ranges from columns A:N and rows varying in number between 3 and 30.

There are two blank lines between each range of data.

Each range is (with exception) numerically ordered down column H (e.g, 1, 2, 3, etc).

Problem: there are around 1100 occasions when a range contains a row of data which is to be disqualified from consideration. On these occasions the data always appears in the top row of the range and is identifiable in column H by the nine codes 111, 222, 333, 444, 555, 666, 777, 888, and 999).

I wish to find a macro which will :

1. where the top row of a range in column H is one of the codes 111 to 999, transfer that row of data to the bottom of the range.

2. delete the now empty top row from which the transfer was made from.

3. insert a new blank row at the bottom of the range below the row which the data was transferred to (in order to maintain the two blank rows above and below each range).

View 12 Replies View Related

Dynamic Shifting Range, Always At Bottom Of Column

Aug 13, 2009

I was wondering if there was a way to create a two cell dynamic range that doesn't expand, but instead shifts.

The cells that I'm interested in are always at the bottom of the column of data. For example, the first two cells I want as my range are C13 and D13. Then, for my next use of the range, I would like it to include C14 and D14.

View 11 Replies View Related

Find The Bottom Left Cell Of A Range

Oct 27, 2008

I am trying to find a code that will simply find the last cell in Column A of a named Range (titled: ConditionlFormatArea)?

Also IF I could get a line(s) of code that would just find the last cell in Column A of the Used Area.

I ask for both because I am not yet certain which line of code will work best for me.

When I have that cell located I need to Remove the Conditional Formatting. I think I can get the code for that part.

View 9 Replies View Related

Identify Bottom Left/Right Cell In Named Range

Aug 24, 2006

Is there any easy way to identify the bottom left cell in a named range?

View 6 Replies View Related

Adding Values To Bottom Of Row In Range In Excel After Index Match

Jul 12, 2013

Basically, I am trying to write a program that will index match through a range and if it doesn't find the value that it is matching to, it will add the value to the bottom of the range it is indexing against. In other words:

My C column has a list of CUSIPS. In column J, I would like for the User to be able to add a list of cusips and then have the ones that don't already exist in the list of CUSIPS in column C be added at the bottom of the range. I have written a macro now that uses the index match, but for some reason it adds all of the cusips that exist in J range to column C, not just the ones that are missing. I can provide a template if necessary.

Sub Filler()
Dim Row As Variant
Dim NumberOfRows As Long
Row = 0

[Code]...

View 4 Replies View Related

For Next Loop Using Used Range

Jun 21, 2012

Code:

For lead = 1 To 20
CurrentFile = ActiveCell
Workbooks.Open Filename:=CurrentFile
ActiveWindow.Caption = "Graboid"

I have a file I use to grab information off a website I subscribe to.

I get daily emails containing a list, then I use that list to extract additional information for each item in the list.

View 8 Replies View Related

How To Loop Through Range

Jan 12, 2013

I am trying to do Loop through Range I have workbook and it has two sheet (sheet1 and sheet2) sheets ("Sheet1").range(A1:K30") hold the Doc_ID (E.g. 78002)

What I want to do........if Range(A1:K30) = Inputbox("Enter Your Doc_Id Number") Then Copy that Cell Only and paste it to in Sheets("Sheet2") -Column A

View 6 Replies View Related

For Each Loop In A Range

Jan 12, 2014

I have a range of letter values. The values vary between A, a, and B. I have a "For Each" loop that doesn't do what I'd like. The way it's written, I'd think it'd delete every column that doesn't contain the letter "A".

Code:
For Each k In rngMyRange
If k.Value "A" Then
k.EntireColumn.Delete
End If
Next k

It seems to miss entire columns. Do I need to initialize k?

View 8 Replies View Related

Sum Of Range + Loop

Nov 2, 2007

I have percentages in each row. Let's say from A4 to Z4. Each cell can be 20% at maximum In Visual Basic I want to write a macro that sums the cells up. So when the sum is less than 100% I want to distribute the remainder among the cells that are below 20%. I want to do this until the total is 100%. So the new percentages have to be entered in the appropriate cells.

How can I do this? I need some sort of loop but I can't get anything to work. With a 'for each cell' statement I always get Value#.

View 9 Replies View Related

Using MID On Range In For Each Loop With IF Statement?

Aug 8, 2013

I am trying to loop through a range, checking for a cell to contain one of two characters and then put a value in a cell to the side (well 3 away) of it. Im getting an error for Type mismatch on line:

VB:
If CellContent = "h" Or "v" Then

the data in the cell is an along the lines of '12.34.56.43 som-thi-vh-ng1'

VB:
Dim MyRange, MyCell As Range
Dim CellContent As String
Sheets("Sheet1").Activate

[Code]....

View 1 Replies View Related

Sum A Dynamic Range Within A For Loop?

Sep 15, 2014

I am working with loans, and I'm trying to program a routine that adds every installment a client owes, so I can know the total amount owed, depending on how many unpaid installments she has. Let say for a client I have every installment in column A (rows 1 to 5), and in column B, I want to add them all. The output should be

Cell B1: "sum(A1:A5)"
Cell B2: "sum(A2:A5)"
Cell B3: "sum(A3:A5)"... and so forth

Since I have many loans, I need a dinamic routine to that sums every installment, where amount of installments vary from client to client. My proposal is (adding from the last installment to the first one):

VB:
months = 4 'an example
For j = 0 To months - 1
initial_row = ActiveCell.Row 'because I run this several times. In this example I am in row 18[code]....

For some reason this is not working, and what I see in Excel after running it is: SUM('AJ18':'AJ18'), which doesn't work.

View 1 Replies View Related

For Loop With Range Variable

Apr 28, 2014

I'm trying to understand a code from work and I can't get it. I copied only a part of the code here so please don't run it. Also, I have manual inputs in columns L and M (nodes labels such as 1, 2 and 2,3) and section labels on column N (such as BarFT3, BarFT4 etc):

[Code] ..........

What is this loop doing? I just get the first For loop: goes through every lable on colum N (from 1 to ne) but then what?

What is happening to range nudo(n,j)? How works this ">" sign between nudo(n.j) and nn?

View 1 Replies View Related

Put A Range Command As Loop?

Aug 14, 2014

how do i put the following as loop. Range("A2) then Range("A4") then Range ("A6) and so on.

View 8 Replies View Related

Incrementing Range In A Loop

Jul 22, 2008

I have a spreadsheet which I need to remove all the blanks from, but instead of shifting cells up (I figured out how to do that in a macro), I want them to shift left.

I understand the basics of for...next loops etc, but I can't seem to understand how to make the selection in a range string variable. Not sure if this is a good explanation!

The code I have so far is....

View 10 Replies View Related

Use Specific Range In For Loop

Feb 13, 2009

I have an excel sheet with set ranges in it. I would like to refer to these ranges in my code, but i am doing something wrong. Here is a portion of the code. The range name is 'Status'

View 4 Replies View Related

Getting Range To Step Through In A Loop

Apr 6, 2012

This little piece of code is where I'm having problems.

A36:A45 will always be the same.

The problem is in getting ("R2:AA2") to move to ("R3:AA3") ...

What needs to happen for the loop to work?

Code:
Worksheets("List").Select
Range("A36:A45").Select
Selection.Copy
Worksheets("Month").Select
Range("R2:AA2").Select

I've tried:

Dim rngDC as Range

then Set rngDC = Worksheets("Month").Range("R2:AA2")

So that I can later use rngDC = RngDC.Offset(1,0)

However, when I try rngDC.Select I get an error saying I can't select the variable that way.

Here's the complete code:

Sub Import()

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

With Sheets("Month")

[Code] ....

View 2 Replies View Related

Loop Through Range And Copy Row

Apr 19, 2012

What I'm trying to do is open a closed workbook from an active one, then go through a range looking for cells that has a different value than "noone", and copy the rows that does have something else than "noone" into the first worksheet.

What I've got so far is:

Code:
Private Sub CmdHent_Click()
Dim vikar As Range
vikar = Range(I12, I42)

Application.Workbooks.Open Filename:="C:Documents and SettingsoybMine dokumenterook1.xlsm"
Windows("book1.xlsm").Activate
Sheets("Sheet1").Select

View 9 Replies View Related

How To Loop Through A Range ? For Formula

Nov 25, 2008

Image column A, starting at A2 to LASTROW of say 10, I have the LASTROW value stored in a variable.

Each cell in the range contains a file location/document name, example:

C:Documents and Settingsuser-idDesktopDOCSDoc01.xls < cell A2

this repeats for A3, A4, to A10, for each different document.

So starting at LASTROW + 3, I've created a new Header for Hyperlinks: and below that I want to create the 10 hyperlinks for each of the 10 documents above.

formula is =hyperlink(a2,a2) and then (a3,a3) and so on...

View 9 Replies View Related

Loop Until Range Of Cells All Have Value Of 0

Aug 11, 2009

Do Mymacro1
loop until

I've tried a couple of ideas but none seem to work. I even googled this hoping that since this seemed like such a simple thing that I couldn't be the first person to run into this... guess I was wrong. the range of cells is BF30:CF56. I tried using a cell in the worksheet to test if all the cells equaled 0, that didn't work, even after I wrote the calculate code into the macro. Nothing I try seems to get the code to stop. is there somthing I'm missing?

View 9 Replies View Related

Loop Through Range From Another Worksheet

Nov 17, 2006

I created an array from data on one sheet...then when using the array (in a loop) on a different active sheet, half the values in the array are lost. However, if I run the code and keep the focus on the sheet with the array data, it works. I can't understand why.

I'll include my code.

aDepartments = Worksheets("sheet1"). Range("A5", Range("B5").End(xlDown).Address)

For Each iZ In aDepartments
MsgBox iZ
Next iZ

If i run this while sheet1 is the active sheet, it works fine - all 15 values (15 in each dimension) are there. If I click on any other sheet and then run the code, only 7 values are in the array (7 in each dimension).

View 4 Replies View Related







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