Solved

Excel column and row on one record part 2

Posted on 2014-07-23
6
447 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now