樣本資料 :
0votes
https://stackoverflow.com/questions/32382401/autohide-multiple-rows-in-excel/32383360#32383360
vbaexcel
answered Sep 3, 2015 at 18:53
0votes
Accepted
https://stackoverflow.com/questions/32273121/corretc-excel-vba-macro-to-return-the-values-as-text-and-as-date/32273219#32273219 'clickable format
vbaexcel
answered Aug 28, 2015 at 14:18
如果答案不被接受,我想在投票和 url 行之間插入一行,并且 url 立即跟隨投票行,目的是對 5 行進行分組,以便最終在一行中轉置資料。我使用的代碼如下:
Sub insertrow()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet2")
Last = Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "A").Value) Like "*vote*" And (Cells(i 1, "A").Value) <> "Accepted" Then
Cells(i 1, "A").EntireRow.Insert
End If
Next i
End Sub
我在下一行收到運行時錯誤 13 型別不匹配,盡管該程式昨天晚上成功運行以獲取類似的資料。
` If (Cells(i, "A").Value) Like "*vote*" And (Cells(i 1, "A").Value) <> "Accepted" `
任何幫助將不勝感激。
uj5u.com熱心網友回復:
對您提供的資料和代碼進行了快速測驗,沒有任何問題。但是,代碼存在一個問題:盡管您將特定作業表分配給變數,但您仍繼續使用ActiveSheet。此作業表很可能不是包含您的示例資料的作業表,并且可能包含導致型別不匹配錯誤的資料。
在 Excel 中編程 VBA 時,您應該始終準確地確定要在哪個作業表上作業。一種方法是使用With- 陳述句。注意在每個Cells, Range,Rows的實體之前Columns添加一個點 - 告訴 VBA 你指的是 - 陳述句的物件With。
Sub insertrow()
With ThisWorkbook.Sheets("Sheet2")
Dim Last As Long, i As Long
Last = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (.Cells(i, "A").Value) Like "*vote*" _
And (.Cells(i 1, "A").Value) <> "Accepted" Then
.Cells(i 1, "A").EntireRow.Insert
End If
Next i
End With
End Sub
或者,如果你更喜歡這樣:
Sub insertrow()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet2")
Dim Last As Long, i As Long
Last = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (ws.Cells(i, "A").Value) Like "*vote*" _
And (ws.Cells(i 1, "A").Value) <> "Accepted" Then
ws.Cells(i 1, "A").EntireRow.Insert
End If
Next i
End Sub
通過使用除錯器檢查相關資料(在本例中為 2 個單元格的內容),通常可以很容易地發現型別不匹配之類的錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/456330.html
