Solved

Excel column and row on one record part 2

Posted on 2014-07-23
6
446 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

746 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

10 Experts available now in Live!

Get 1:1 Help Now