I am new to Excel Macro and i was given the task with the following excel data
Date Brazil Turkey Italy Panama Lithuania
3/15/2014 110 47 192 0 2
3/16/2014 118 54 179 0 7
3/17/2014 72 22 96 0 1
3/18/2014 17 20 60 2 2
3/19/2014 19 17 57 0 1
3/20/2014 15 21 52 0 2
I am to create a Macro so the ending result for each Date like such.I have looked up various of ways to do it, but cant seem to find a way. Thank you very much
Date Country Downloads
3/15/2014 Brazil 110
3/15/2014 Turkey 47
3/15/2014 Italy 192
3/15/2014 Czech Republic 19
3/15/2014 Panama 0
3/15/2014 Lithuania 2
3/15/2014 Costa Rica 6
3/15/2014 Luxembourg 1
I tried using just regular Excel interface commands, but found out it will not work and i dont know where to begin with VBA.
Range("B9:F10").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=18
Range("W51").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveWindow.SmallScroll Down:=-15
Range("A10").Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=12
Range("V51").Select
ActiveSheet.Paste
Range("V51").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "3/15/2014"
Selection.AutoFill Destination:=Range("V51:V55"), Type:=xlFillCopy
Range("V51:V55").Select
Range("Y49").Select
End Sub
Interesting application here -- as Tim's link shows in the comment, it's a lot like "un-pivoting" the data. Assuming you start off like this:
The following routine ought to do the trick:
Option Explicit
Sub DeconstructData()
Dim DataSheet As Worksheet, OutSheet As Worksheet
Dim CountryArray(100) As String
Dim LastRow As Long, LastCol As Long, _
RowIndex As Long, ColIndex As Long, _
TargetCounter As Long
'assign sheets and variables up-front for easy reference
TargetCounter = 1
Set DataSheet = ThisWorkbook.Worksheets("Sheet1")
Set OutSheet = ThisWorkbook.Worksheets("Sheet2")
LastRow = DataSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastCol = DataSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
'error trap
If LastCol > 100 Then
MsgBox ("Script designed for a max of 100 countries, exiting sub...")
Exit Sub
End If
'fill up the Country array with the name in [Index, 1] and the position in [Index, 2]
For ColIndex = 2 To LastCol
CountryArray(ColIndex - 1) = DataSheet.Cells(1, ColIndex).Value
Next ColIndex
'set the headers on our target sheet
OutSheet.Cells(1, 1) = "Date"
OutSheet.Cells(1, 2) = "Country"
OutSheet.Cells(1, 3) = "Downloads"
'loop through rows on data sheet
For RowIndex = 2 To LastRow
'loop through columns on data sheet
For ColIndex = 2 To LastCol
OutSheet.Cells(TargetCounter + 1, 1) = DataSheet.Cells(RowIndex, 1)
OutSheet.Cells(TargetCounter + 1, 2) = CountryArray(ColIndex - 1)
OutSheet.Cells(TargetCounter + 1, 3) = DataSheet.Cells(RowIndex, ColIndex)
TargetCounter = TargetCounter + 1
Next ColIndex
Next RowIndex
'set the A column on the out sheet to be of date format
OutSheet.Range("A:A").NumberFormat = "m/d/yyyy"
'let the user know the script is complete
MsgBox ("Transformation complete!")
End Sub
Which will result in this:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.