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
Solved

Excel column and row on one record part 2

Posted on 2014-07-23
6
452 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

840 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