Solved

Excel column and row on one record part 2

Posted on 2014-07-23
6
454 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Office 365 Advanced Training for Admins

Special Offer:  Buy 1 course, get 2nd free!  Buy the 'Managing Office 365 Identities & Requirements' course w/ Accelerated TestPrep, and automatically receive the 'Enabling Office 365 Services' course FREE!

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 …
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

732 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