2016-02-24 77 views
0

我有一個電子表格,其中第一列的信息從第六行開始,我從這個列表中獲得唯一值作爲第一行中從第D列開始的標題。我想要做的是如果列A中的單元格與標題行匹配,將C列單元格中的信息複製到相應的標題。vba比較行到列

原始格式 enter image description here

所需的格式 enter image description here

這裏是我試過的代碼,但它不給我想要的結果:

For i = 6 To lastRow 
    For j = 4 To lastColumn 
     If Cells(i, 1) = Cells(1, j) Then 
     wksActRawData.Cells(i, 3).Value = wksActRawData.Cells(i, j).Value 
     End If 
    Next j 
    Next i 

回答

0

假設你確實寫了lastRow和lastColumn函數,唯一的錯誤在你的代碼是獲取單元格值

If Cells(i, 1) = Cells(1, j) Then 
    wksActRawData.Cells(i, 3).Value = wksActRawData.Cells(i, j).Value 
End If 

這確實應該倒過來,加上我要補充一條線,清除舊的價格,具體如下

If Cells(i, 1) = Cells(1, j) Then 
    Cells(i, j).Value = Cells(i, 3).Value 
    Cells(i, 3).ClearContents 
    End If 

數據它以這種方式爲我工作

+0

謝謝,很好!猜猜我一直在盯着代碼的方式。我有我的lastRow和lastColumn功能。我將添加一行只刪除整列的行。 –

+0

所有時間都會發生在我身上。我想用更多的數據刪除列可能會更快。很高興能夠提供幫助。 –