Microsoft Excel - Macro that moves through a worksheet row by row

Asked By Cara Portka
17-Aug-10 09:48 PM
I have a spreadsheet that contains 3 types of columns, Item, description and finally dates in chronological order. Each row contains and item number, description and a qty under each date (either 0 or higher).

What I need is a macro/code that will go through each row cell by cell and stop at the first cell with a qty>0. When the first cell in the row with a qty>0 is found, the value returned will be the column header(the date). Ideally I would love to have the Item#, Desc and column header of the first column with a qty>0 returned.

I attempted this in a function with nested If statements, but there are too many columns (there can only be 7 nested statements in excel). The if statement went like this (assuming Row 1 consists of the column headers: Item, Desc, 9/10/2010, 9/12/2010, 9/14/2010, etc. And each row would hold information such as Item#, Description, 0, 0, 4000, 0, 700, etc. The goal would be to return the Item#, Description and the column header (date) for the value 4000 (in this example 9/14/2010).

Thank you in advance!
  Rolf Jaeger replied to Cara Portka
18-Aug-10 12:52 AM
Hi Cara:

have you tried to apply Excel's built-in auto-filtering capabilities? It seems to me that should exactly do what you need. Please let me know if you need any help with that or if I misunderstood your requirements.

Best wishes,
Rolf
  Cara Portka replied to Rolf Jaeger
18-Aug-10 08:22 AM
Rolf,

Thank you for your suggestion. Unfortunately the data/layout of the data does not facilitate the ease of filtering. I have tried filtering and attempting to manipulate the data in a pivot table, but the format (which comes from an export from another program) does not allow for these options to easily work. There are many different items in the list with different values in each of the columns headed with a date.

A function or macro would allow for this functionality to be used for multiple spreadsheets with ease (rather than taking the time to try and look at each item one line at a time to find the first production date).

Here is a better example of my data.

*Item Descr 8/17/2010 8/22/2010 8/29/2010 9/5/2010 9/12/2010 9/19/2010 9/26/2010
99990 prod a 0 29,092.00 0 0 27,373.67 0 0
99991 prod b 0 26,000.00 0 0 0 28,910.69 0
99992 prod c 9,000.00 0 4,500.00 4,284.00 4,680.00 3,996.00 3,924.00
99993 prod d 0 0 0 0 0 0 0
99994 prod e 0 0 0 0 0 0 0
99995 prod f 0 0 90000.00 0 0 0 0
99996 prod g 0 0 0 6,284.00 0 0 0
99997 prod h 0 0 0 0 0 0 0
99998 prod i 0 24,575.00 21,145.00 15,750.00 21,685.85 16,785.00 14,044.15

What I need is something that will go thru each row and return the column header when it hits the first  value>0. For the first row in my example I would like to have the information 99990-prod a-8/22/2010 returned to me in a cell. I would then want to move on to the next row and have 99991-prod b-98/22/2010. The third row 99992-prod c-8/17/2010 and the fourth row would return 99993-prod d-"No Date"

I'm not sure how this would work in terms of selecting each row or selecting a range of values to be evaluated.

I hope this clears any confusion.
  Rolf Jaeger replied to Cara Portka
18-Aug-10 11:44 AM
Hi Cara:

this code should do the trick for you:

Const DATA_WORKSHEET As String = "Sheet1"
Const OUTPUT_WORKSHEET As String = "ExtractedData"
  
Sub ExtractRowsWithNonZeroQnty()
  Dim shData As Worksheet
  Set shData = Worksheets(DATA_WORKSHEET)
  Dim shOutput As Worksheet
  On Error Resume Next
  Set shOutput = Worksheets(OUTPUT_WORKSHEET)
  If shOutput Is Nothing Then
    'Add and name output worksheet
    Worksheets.Add After:=Worksheets(Worksheets.Count)
    Worksheets(Worksheets.Count).Name = OUTPUT_WORKSHEET
    Set shOutput = Worksheets(OUTPUT_WORKSHEET)
    'Create header row
    shOutput.Range("A1").Value = "Item"
    shOutput.Range("B1").Value = "Descr"
    shOutput.Range("C1").Value = "Date"
  Else
    'Clear output worksheet except for header
    shOutput.UsedRange.Clear
    shOutput.Range("A1").Value = "Item"
    shOutput.Range("B1").Value = "Descr"
    shOutput.Range("C1").Value = "Date"
  End If
  Dim db As Range
  Set db = shData.UsedRange
  Dim rRow As Range
  Dim rcell As Range
  Dim iCol As Integer
  Dim iColNonZero As Integer
  For Each rRow In db.Rows
    If rRow.Row > 1 Then
      For Each rcell In rRow.Cells
        If rcell.Column >= 3 Then
          If rcell.Value > 0 Then
            iColNonZero = rcell.Column
            Exit For
          End If
        End If
      Next rcell
      Dim targetRow As Range
      Set targetRow = shOutput.Range("A" & Rows.Count).End(xlUp).Offset(1)
      If iColNonZero > 0 Then
        targetRow.Cells(1, 1).Value = rRow.Cells(1, 1).Value
        targetRow.Cells(1, 2).Value = rRow.Cells(1, 2).Value
        targetRow.Cells(1, 3).Value = db.Cells(1, iColNonZero).Value
        iColNonZero = 0
      Else
        targetRow.Cells(1, 1).Value = rRow.Cells(1, 1).Value
        targetRow.Cells(1, 2).Value = rRow.Cells(1, 2).Value
        targetRow.Cells(1, 3).Value = "None"
      End If
    End If
  Next rRow
End Sub

It could be written in a more compact fashion, but I am afraid doing so would obscure its intentions.

Hope this helped,
Rolf
  Cara Portka replied to Rolf Jaeger
20-Aug-10 09:43 AM
Thanks so much Rolf! You definately helped me out :)
Create New Account
help
Conflict with Valid Range Reference Error in Microsoft Excel 2007 Excel Hi, While converting the Macros enabled Microsoft Excel 2003 to Microsoft Excel 2007 format, I am getting the following Conflict with Valid Range Reference Error. After clicking
excel error Excel Whenever I download a CSV file, whether I Save it or Open it I get an error message that "Microsoft Excel Viewer cannot read files of this type" - - Will Pfister Excel Miscellaneous Discussions Microsoft Excel (1) Pfister (1) Challa (1) Prabhu (1) Willpfister (1) The free Excel Viewer can
Microsoft Excel Error Starting MS Help Excel A coworker was having problems with office and ran the detect and repair back to defaults. Since doing that, she is getting the error in the subject. Help in Excel works if going into it from Help / Microsoft Excel Help. If she selects a link in the Search Results pane, she gets the error
Error msg when sending data from Excel to Access table Excel Here is the message: Microsoft Excel [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process Ox3f4 Thread Ox8d8 DBC
Excel Error Excel different chart type." I receive this error whenever i click insert chart in all office applications even within excel itself. Tried disabling Norton Internet Security 2007, problem persists. Tried running Diagnostics, problem persists. Any solutions? Excel Charting Discussions Microsoft Excel (1) Office 2007 (1) Error (1) Excel (1) Database (1) Diagnostics (1) Tejas (1) DF2461D0B1F6