|
U03さんに2箇所教えていただいたものに
マクロの記録を使ったものを加えてあります。
動作としては一応動作しておりますが、
削除しても構わないところや、コンパクトにまとめれるところがあれば
教えていただきたいです。
Sub sample1()
Range("C:C,DU:DV").Select
Selection.Delete Shift:=xlToLeft
Rows("1:2").Select
Selection.Delete Shift:=xlUp
Cells.Select
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="男", Replacement:="m", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="女", Replacement:="f", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Dim r As Range
Dim i As Long
Dim z As Long
With ActiveSheet.UsedRange
z = .Cells(.Cells.Count).Row
End With
For i = 3 To z Step 4
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:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "0"
Range("D1").Select
ActiveCell.FormulaR1C1 = "1"
Range("C1:D1").Select
Selection.AutoFill Destination:=Range("C1:DS1"), Type:=xlFillDefault
Range("C1:DS1").Select
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
With Sheets("Sheet1")
y = .Range("A" & .Rows.Count).End(xlUp).Row
x = .Cells(1, .Columns.Count).End(xlToLeft).Column
ReDim w(1 To .Rows.Count, 1 To 5)
For i = 2 To y Step 2
aCode = .Cells(i, "A").Value
aName = .Cells(i + 1, "A").Value
For z = i To i + 1
mf = .Cells(z, "B").Value
For j = 3 To x
k = k + 1
w(k, 1) = aCode
w(k, 2) = aName
w(k, 3) = mf
w(k, 4) = .Cells(1, j).Value
w(k, 5) = .Cells(z, j).Value
Next
Next
Next
End With
With Sheets("Sheet2")
.Cells.ClearContents
.Range("A1").Resize(k, UBound(w, 2)).Value = w
.Select
End With
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "code"
Range("B1").Select
ActiveCell.FormulaR1C1 = "cyomei"
Range("C1").Select
ActiveCell.FormulaR1C1 = "sex"
Range("D1").Select
ActiveCell.FormulaR1C1 = "age"
Range("E1").Select
ActiveCell.FormulaR1C1 = "pop"
Range("F1").Select
MsgBox "完了!"
End Sub
|
|