2013-10-14 31 views
0

我的Excel工作表在右邊的單元格的數據值,所以在單細胞它像Excel/VBA - 基於定位的多行對齊 - 如何檢查單元格值然後將值保存在另一個單元格中?

+---+--------+---------------------+ 
| | A |   B   | 
+---+--------+---------------------+ 
| 1 | School | newyork high school | 
| 2 | Head | Mr john    | 
| 3 | phone | 0191919    | 
| 4 | email | [email protected]  | 
+---+--------+---------------------+ 

學校名稱,然後其他數據旁邊的單元格。我想將它們垂直排列,如

+---+---------------------+---------+---------+-----------------+ 
| |   A   | B | C |  D  | 
+---+---------------------+---------+---------+-----------------+ 
| 1 | School    | Head | Phone | Email   | 
| 2 | newyork high school | Mr john | 0191919 | [email protected] | 
+---+---------------------+---------+---------+-----------------+ 

我試圖按照以下方式獲取下一個單元格的值。

Dim cell As Range 
For Each cell In Range("a1:a40") 
    If cell.Value = school Then 
     Range("E3").Value = cell.Offset(1, 0).Value 
Next 

回答

0

這是有點不清楚你試圖完成什麼,但是,這應該讓你開始:

Sub transposeCellData() 
    Dim Rng As Excel.Range 
    Dim cll As Excel.Range 
    Dim schoolName As String 
    Dim schoolHead As String 
    Dim schoolAdd As String 
    Dim schoolWeb As String 
    Dim schoolPhone As String 
    Dim schoolFax As String 
    Dim outRow As Currency 
    outRow = 2 
    Set Rng = Range("A1:A40") 
    For Each cll In Rng 
     If ucase(trim(cll.Value)) = "HEAD:" Then 
      schoolName = cll.Offset(-1, 0).Value 
      schoolHead = cll.Offset(0, 1).Value 
      schoolAdd = cll.Offset(1, 1).Value 
      schoolWeb = cll.Offset(2, 1).Value 
      schoolPhone = cll.Offset(1, 3).Value 
      schoolFax = cll.Offset(2, 3).Value 
      Range("I" & outRow).Value = schoolName 
      Range("J" & outRow).Value = schoolHead 
      Range("K" & outRow).Value = schoolAdd 
      Range("L" & outRow).Value = schoolPhone 
      Range("M" & outRow).Value = schoolFax 
      Range("N" & outRow).Value = schoolWeb 
      outRow = outRow + 1 
     End If 
    Next cll 
End Sub 

讓我知道是否有幫助。 +1

+0

它不工作:( –

+0

http://postimg.org/image/tob7f571h/這裏是我的Excel格式 –

+0

@RobertGarcia的截圖:請嘗試更新的答案 – IIIOXIII

相關問題