Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 3rd, 2007, 10:21 AM
Authorized User
 
Join Date: May 2007
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to loop for Select Values

Hi,

I have listed of unsorted names of Country's In Sheet1 ColumnA1:A200. I need a formula that cut the name of each country from the orginal sheet1 and place it on a new sheet. For example if i have "India" on Sheet1 Cell A1 then it should cut the name from there and place it Sheet2 Cell A1..If the next occurence of "India" happens in Sheet 1 Cell A156 then it should cut the Value "India" and place on Sheet2 Cell A2 and so on and so forth...i dont know how to loop this kind of formula given the condition....Also the programme should stop as soon as all the occurence of "India" is over....and then jump to the next Country and start doing the same on Sheet2 B1...by taking the values from Sheet1 Column A.

 
Old May 3rd, 2007, 01:14 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Do you have a consistant list as far as the number of items in the list? If so you can loop by a for/next loop. Then you could take the first item in the list and put it to a variable to compare. When the variable and the item in the list are equal you do a subroutine to open the other file, put the variable into the first cell, get a counter and increment to keep track of the line, save and close the current window, then clear the contents of the current cell. That way any blank line or line that doesn't match does not get processed.

If you do not have consistancy in the number of items, it will make it more difficult. This would involve creating an array of items, going through that array and deleting any duplicates, reposition all of the rest of the array that follows, then work off of the array to go through the list and find a match. This would be faster but the array would need to be set up with a very large number on the index (more than what you would ever use) so that you don't run out of space in the array.

 
Old May 4th, 2007, 04:45 AM
Authorized User
 
Join Date: May 2007
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont have a consistent list... the lenght of the list is sometimes from A1:A200 to A1:A30000......I am not aware of Array programming can somebody help me with a sample program.....

 
Old May 8th, 2007, 03:29 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Actually it would be easier to sort the range you wish to remove duplicates for and write a routine to check and see if the next line matches then delete the line you don't need in the loop. Played with the problem a while and made a simple module that checks current selected column for duplicates assuming the data is presorted.
Remember that you can sort ranges by using "Range(sMyRange).Sort" to automate the sorting. Note that I only allow for 1 blank line in the determining column.

Select a cell. The selected cell's row and column becomes the start row and the source column.

Here's the simple example I made that can be put into a module:
------------------------------------------------------------------------------
Public Sub DeleteDuplicates()

'Checks Current column for duplicates that's sorted and deletes the duplicate rows
  Dim iRowOn As Long, iCheckedColumn As Long, iBlanklines As Integer
  Dim vCurrentContent As Variant
  iCheckedColumn = ActiveCell.Column
  iRowOn = ActiveCell.Row
  vCurrentContent = UCase(Cells(RowOn, CheckedColumn).Value)
  iRowOn = iRowOn + 1
  Do While iBlanklines < 2
    If InvalidOrBlank(iRowOn, iCheckedColumn) Then
      iBlanklines = iBlanklines + 1
      iRowOn = iRowOn + 1
    Else
      If UCase(Cells(iRowOn, iCheckedColumn).Value & " ") = vCurrentContent & " " Then
        Cells(iRowOn, iCheckedColumn).EntireRow.Delete
      Else
        vCurrentContent = UCase(Cells(iRowOn, iCheckedColumn).Value)
        iRowOn = iRowOn + 1
      End If
    End If
    Cells(iRowOn, iCheckedColumn).Activate
  Loop

End Sub

Private Function InvalidOrBlank(iPassedRow As Long, iPassedColumn As Long) As Boolean

'If cell is invalid or blank returns True
  InvalidOrBlank = True
  On Error GoTo ExitInvalidOrBlank
  If Cells(iPassedRow, iPassedColumn).Value & " " <> " " Then InvalidOrBlank = False
ExitInvalidOrBlank:

End Function
------------------------------------------------------------------------------

Hope this helps. If you want auto sorting and need help with the sort function let me know.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop a select but with a delay between each try chubnut SQL Server 2005 1 June 11th, 2008 06:26 AM
Need to concatenate values in a loop LeoMathew XSLT 6 February 14th, 2008 09:03 AM
loop values and text box values move mateenmohd Classic ASP Basics 2 April 5th, 2005 11:33 PM
Select box for loop(probably really simple) interrupt Javascript How-To 2 March 18th, 2005 12:25 PM
Nested Loop Select Box Issue mat41 Classic ASP Basics 5 August 24th, 2004 11:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.