|
- Sub ExtractData()
- Dim sourceSheet As Worksheet
- Dim targetSheet As Worksheet
- Dim lastRow As Long
- Dim i As Long
- Set sourceSheet = ThisWorkbook.Sheets("Sheet2")
- Set targetSheet = ThisWorkbook.Sheets("Sheet1")
- lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
- n = 1
- 男女 = 0: 城市 = 0: 成绩 = 0
- targetSheet.Range("A2:C" & lastRow).ClearContents
- With sourceSheet
- 男女 = .UsedRange.Find("男/女", , , xlWhole).Column
- 城市 = .UsedRange.Find("城市", , , xlWhole).Column
- 成绩 = .UsedRange.Find("成绩", , , xlWhole).Column
- If 男女 = 0 Or 城市 = 0 Or 成绩 = 0 Then Exit Sub
- For i = 2 To lastRow ' Assuming headers are in the first row
- If sourceSheet.Range("B" & i).Value = "CA" Then
- n = n + 1
- targetSheet.Range("A" & n).Value = .Cells(i, 男女).Value
- targetSheet.Range("B" & n).Value = .Cells(i, 城市 + 1).Value
- targetSheet.Range("C" & n).Value = .Cells(i, 成绩).Value
- End If
- Next i
- End With
- MsgBox "Data extracted successfully!", vbInformation
- End Sub
复制代码 |
|