|
▼マリモ さん:
Selectを無くして見ました
検証はしていませんのでエラーがあるかもしれません。
Sub sample2()
Dim r As Range
Dim i As Long
Dim z As Long
Dim x As Long
Dim y As Long
Dim j As Long
Dim k As Long
Dim aCode As String
Dim aName As String
Dim mf As String
Dim w() As Variant
Dim v As Variant
With Worksheets("Sheet3") ' 最初に処理するシート名
.Range("C:C,DU:DV").Delete Shift:=xlToLeft
.Rows("1:2").Delete Shift:=xlUp
With .UsedRange
.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
.Replace What:="男", Replacement:="m", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
.Replace What:="女", Replacement:="f", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
z = .Cells(.Cells.Count).Row
End With
For i = 3 To z Step 2
If r Is Nothing Then
Set r = Rows(i).Resize(2)
Else
Set r = Union(r, Rows(i).Resize(2))
End If
Next
If Not r Is Nothing Then r.EntireRow.Delete
.Rows(1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("C1").Value = "0"
.Range("D1").Value = "1"
.Range("C1:D1").AutoFill Destination:=Range("C1:DS1"), Type:=xlFillDefault
End With
With Sheets("Sheet1")
v = .Range("A1").CurrentRegion.Value
y = UBound(v)
x = UBound(v, 2)
ReDim w(1 To y, 1 To 5)
For i = 2 To y Step 2
aCode = v(i, 1)
aName = v(i + 1, 1)
For z = i To i + 1
mf = v(z, 2)
For j = 3 To x
k = k + 1
w(k, 1) = aCode
w(k, 2) = aName
w(k, 3) = mf
w(k, 4) = v(1, j)
w(k, 5) = v(z, j)
Next
Next
Next
End With
With Sheets("Sheet2")
.Cells.ClearContents
.Range("A1").Resize(k, UBound(w, 2)).Value = w
.Rows(1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("A1").Value = "code"
.Range("B1").Value = "cyomei"
.Range("C1").Value = "sex"
.Range("D1").Value = "age"
.Range("E1").Value = "pop"
MsgBox "完了!"
End With
End Sub
|
|