Solved

Excel column and row on one record part 2

Posted on 2014-07-23
6
453 Views
Last Modified: 2014-07-28
Hello Experts Exchange
I have some code for Excel that will create one record from the data I have attached.

The code works if I have the table of data on row 1 and starting in column A, how do I change the code so that it works on row 7 like the example I have attached?

This is the code;
Sub Normalize()
    
    Dim SourceRange As Range
    Dim SourceArr As Variant
    Dim r As Long, c As Long
    Dim DestR As Long
    
    ' Update as needed
    
    Const StartDate As Date = #6/1/2014#
    
    ' Update as needed
    
    Set SourceRange = ActiveSheet.Range("a1:ak13")
    
    SourceArr = SourceRange.Value
    
    Worksheets.Add
    
    Range("a1:f1").Value = Array("Die", "Machine", "Part Desc", "Part No", "Date", "Units")
    DestR = 2
    
    For r = 4 To UBound(SourceArr, 1)
        For c = 7 To UBound(SourceArr, 2)
            If SourceArr(r, c) <> "" Then
                Cells(DestR, 1) = SourceArr(r, 1)
                Cells(DestR, 2) = SourceArr(r, 2)
                Cells(DestR, 3) = SourceArr(r, 3)
                Cells(DestR, 4) = SourceArr(r, 4)
                Cells(DestR, 5) = StartDate + SourceArr(1, c) - 1
                Cells(DestR, 6) = SourceArr(r, c)
                DestR = DestR + 1
            End If
        Next
    Next
    
    MsgBox "Done"
    
End Sub

Open in new window


Regards

SQLSearcher
Losses-Example2.xls
0
Comment
Question by:SQLSearcher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213778
Could it explain in detail for the question. What  you want to
do on Row1 and Row7

Duncan
0
 

Author Comment

by:SQLSearcher
ID: 40213793
Hello Duncan
The code works on the table of data when the data is stored on the top of the work sheet, however the spreadsheets that I need the code to work on the table is stored on row 7.  I have tried to change the code to reflect the change in location but it keeps failing on the line of code;

Range("a1:f1").Value = Array("Die", "Machine", "Part Desc", "Part No", "Date", "Units")

Open in new window


I think it just needs a couple of changes to work but I have had a play with it but unable to get it to work for me.

The spreadsheet I have attached is a example sheet of how the data is stored, and on sheet2 is example of how I want the data.

Regards

SQLSearcher
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40213838
What Excel 2003 or 2007 are you using ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 40213916
This changes things a bit, using constants to define where to find the data.  Update the constants as needed.

Sub Normalize()
    
    Dim SourceRange As Range
    Dim SourceArr As Variant
    Dim r As Long, c As Long
    Dim DestR As Long
    
    ' Update as needed
    
    Const StartDate As Date = #6/1/2014#
    Const LastRow As Long = 19
    Const DateOffsetRow As Long = 7
    Const DataStartRow As Long = 10
    
    Set SourceRange = ActiveSheet.Range("a1:ak" & LastRow)
    
    SourceArr = SourceRange.Value
    
    Worksheets.Add
    
    Range("a1:f1").Value = Array("Die", "Machine", "Part Desc", "Part No", "Date", "Units")
    DestR = 2
    
    For r = DataStartRow To LastRow
        For c = 7 To UBound(SourceArr, 2)
            If SourceArr(r, c) <> "" Then
                Cells(DestR, 1) = SourceArr(r, 1)
                Cells(DestR, 2) = SourceArr(r, 2)
                Cells(DestR, 3) = SourceArr(r, 3)
                Cells(DestR, 4) = SourceArr(r, 4)
                Cells(DestR, 5) = StartDate + SourceArr(DateOffsetRow, c) - 1
                Cells(DestR, 6) = SourceArr(r, c)
                DestR = DestR + 1
            End If
        Next
    Next
    
    MsgBox "Done"
    
End Sub

Open in new window

0
 

Author Comment

by:SQLSearcher
ID: 40214150
Hello Patrick
What change do I have to make to the code if I want it to skip cells that have a zero in them?

Regards

SQLSearcher
0
 
LVL 27

Assisted Solution

by:Glenn Ray
Glenn Ray earned 50 total points
ID: 40215109
SQLSearcher,
RE: skipping zero values
Change line 26 of Patrick's code to
           If SourceArr(r, c) <> "" and SourceArr(r,c) <> 0 Then

Open in new window


Regards,
-Glenn
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question