我想沿著一行搜索以找到一個字串單元格條目“類別 1”,并從這一點插入一定數量的列(基于另一個單元格值)。然后,新列應具有“類別 2”、“類別 3”等標簽。
到目前為止,我已經設法對其進行編碼,以便能夠根據特定的列索引(而不是字串單元格條目)執行上述操作。但是,我一直在嘗試修改它,以便可以使用“類別 1”作為列插入的參考點。我下面的代碼是我對此的嘗試,但提出“物件不支持此屬性或方法”。在 .Find 函式上查看 microsoft 的建議頁面時,它的代碼與我使用的代碼非常相似,所以我真的很困惑出了什么問題。
任何幫助將不勝感激!
Sub Add_Column()
Dim aCount As Integer
Dim a As Integer
Dim bCol As Long
Dim b As Long
Dim c As Range
aCount = Range("B12").Value
bCol = Cells(15, Columns.Count).End(xlToLeft).Column
With Worksheets(3)
Set c = .Find("Category 1", LookIn:=xlValues)
For a = 1 To aCount - 1
For b = 1 To bCol
c.Offset(0, 1).Insert Shift:=xlToRight
Range("G15").Value = "Category" Str(aCount - a 1)
Next b
Next a
End With
End Sub
uj5u.com熱心網友回復:
一個作業表沒有Find-方法,一個范圍了。所以命令應該是這樣的:
Set c = .Cells.Find("Category 1", LookIn:=xlValues)
(Cells是一個包含作業表所有單元格的范圍)
我不了解您的代碼邏輯的所有細節,但我認為Range("B12")并且Range("G15")在Worksheets(3)? 您應該限定所有范圍(告訴 Excel 您指的是哪個作業表(否則使用 ActiveSheet 并且這并不總是您想要的)并&用于字串連接,而不是
With Worksheets(3)
aCount = .Range("B12").Value
bCol = .Cells(15, .Columns.Count).End(xlToLeft).Column
Set c = .Cells.Find("Category 1", LookIn:=xlValues)
For a = 1 To aCount - 1
For b = 1 To bCol
c.Offset(0, 1).Insert Shift:=xlToRight
.Range("G15").Value = "Category" & Str(aCount - a 1)
Next b
Next a
End With
uj5u.com熱心網友回復:
也許這有效。
Sub x()
Dim aCount, bCol As Long, c As Range, a As Long
With Worksheets(3)
aCount = .Range("B12").Value
If Not IsNumeric(aCount) Then Exit Sub
bCol = .Cells(15, .Columns.Count).End(xlToLeft).Column
Set c = .Cells.Find("Category 1", LookIn:=xlValues)
If c Is Nothing Then Exit Sub
For a = 1 To aCount - 1
c.Offset(0, a).Insert Shift:=xlToRight
c.Offset(0, a).Value = "Category " & a 1
Next a
End With
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374111.html
