Excel 在一个程序中,该代码起作用,在类似的程序中,在复制cell.value时显示运行时错误1004
在一个程序中,此代码起作用,在类似的程序中,在复制cell.value时显示运行时错误1004 错误是未分配列号Excel 在一个程序中,该代码起作用,在类似的程序中,在复制cell.value时显示运行时错误1004,excel,vba,Excel,Vba,在一个程序中,此代码起作用,在类似的程序中,在复制cell.value时显示运行时错误1004 错误是未分配列号 Dim Next_6, PriceChange, Price_i, MyWorksheetLastRow As Long MyWorksheetLastRow = Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Row Next_6 = ColInstance("Next_6_months", 1) 'Next_6 = 15 For Pr
Dim Next_6, PriceChange, Price_i, MyWorksheetLastRow As Long
MyWorksheetLastRow = Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Next_6 = ColInstance("Next_6_months", 1)
'Next_6 = 15
For Price_i = 2 To MyWorksheetLastRow
Cells(Price_i, Next_6).Value = Cells(Price_i, Next_6).Value & " " & Cells(Price_i, Next_6 + 1).Value
Next Price_i
Function ColInstance(HeadingString As String, InstanceNum As Long)
Dim ColNum As Long
On Error Resume Next
ColNum = 0
For X = 1 To InstanceNum
ColNum = (Range("A1").Offset(0, ColNum).Column) + Application.WorksheetFunction.Match(HeadingString, Range("A1").Offset(0, ColNum + 1).Resize(1, Columns.Count - (ColNum + 1)), 0)
Next
ColInstance = ColNum
End Function
调试时,值15(与“下一个6个月”匹配的列号未分配给下一个6)
为什么会这样?这个问题不是很清楚,所以我猜 您的代码有几点需要修复:
范围
s。
这个问题一再出现(例如)
这是什么意思?除非您特别想这样做,否则不要使用单元格
、范围
、行
或列
,而不指定它们所属的工作表
(即使在这种情况下,显式使用ActiveSheet
也可以提高可读性并减少出错的机会,类似于使用Option Explicit
)。
例如,您使用
MyWorksheetLastRow = Worksheets(1)...
在某些情况下,以及在许多其他情况下,您不使用任何东西,默认为ActiveSheet
。请检查这是否是有意的Option Explicit
然后解决这个问题
Dim Next_6 As Long, PriceChange As ..., Price_i As Long, MyWorksheetLastRow As Long
及
如前所述,在声明变量的代码中有一些输入错误,但也有可能coInstance函数并不总是返回您所期望的结果 下面的重写应该是有益的 更新 稍微修改以允许行直接指向图纸1
Sub AssignValues()
Dim Next_6 As Long, PriceChange As Long, Price_i As Long, MyWorksheetLastRow As Long
With ThisWorkbook.Worksheets(1)
MyWorksheetLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Next_6 = ColInstance("Next_6_months", 1)
If Next_6 > 0 Then
For Price_i = 2 To MyWorksheetLastRow
.Cells(Price_i, Next_6).Value = .Cells(Price_i, Next_6).Value & " " & .Cells(Price_i, Next_6 + 1).Value
Next Price_i
End If
End With
End Sub
Function ColInstance(Header As String, Optional Instance As Long = 1) As Long
' Function returns 0 if Header doesn't exist in specified row
' Function returns -1 if Header exists but number of instances < specified
ColInstance = 0
Dim i As Long: i = 1
Dim c As Range
With ThisWorkbook.Worksheets(1).Rows(1)
Set c = .Find(Header, LookIn:=xlValues)
If Not c Is Nothing Then
FirstAdr = c.Address
Do
i = i + 1
If i > Instance Then
ColInstance = c.Column
Exit Do
End If
Set c = .FindNext(c)
Loop While c.Address <> FirstAdr
If c.Address = FirstAdr And Instance > 1 Then ColInstance = -1
End If
End With
End Function
子赋值()
下一步调暗6倍,价格变化倍,价格倍,我的工作表显示倍
使用此工作簿。工作表(1)
MyWorksheetLastRow=.Cells(.Rows.Count,“A”).End(xlUp).Row
下一个6月=持续时间(“下一个6月”,1)
如果Next_6>0,则
对于我的工作表,价格i=2
.Cells(Price_i,Next_6)。Value=.Cells(Price_i,Next_6)。Value&&.Cells(Price_i,Next_6+1)。Value
下一个价格
如果结束
以
端接头
函数ColInstance(标题为字符串,可选实例为Long=1)的长度
'如果指定行中不存在标头,则函数返回0
'如果标头存在但实例数<指定,则函数返回-1
共线度=0
长度为的尺寸i:i=1
调光范围
使用此工作簿。工作表(1)。行(1)
Set c=.Find(头,LookIn:=xlValues)
如果不是,那么c什么都不是
FirstAdr=c.地址
做
i=i+1
如果我>实例,那么
共线状态=c柱
退出Do
如果结束
集合c=.FindNext(c)
循环,而c.地址为FirstAdr
如果c.Address=FirstAdr且实例>1,则COINSTANCE=-1
如果结束
以
端函数
您的for x
循环从1变为1
,但从未使用x
?但这是一个问题吗?这是一个常规函数。我只想找到第一个实例。因此我给出了一个。ColNum=(范围(“A1”).Offset(0,0)。Column)+Application.WorksheetFunction.Match(HeadingString,范围(“A1”).Offset(0,0+1)。调整大小(1,Columns.Count-(0+1)),0)
使用colnum
而不是x
-您的函数应该做什么?您不需要x或colnum变量,而“在调试时,值15(与“下一个6个月”匹配的列号没有分配给下一个6)”是什么意思?检查:1)MyWorksheetLaw的值,2)哪一个Price\u i
值导致错误,3)哪一个是ActiveSheet
,4)如果您可以调试。打印单元格(Price\u i,Next\u 6)。值和单元格(Price\u i,Next\u 6+1)。在出现错误之前,在即时窗口中显示value
。非常感谢。但它不起作用。函数中的“c”值保持为“Nothing”,即使工作簿的第一行中有“Next_6_months”。即使工作簿已打开且全部打开,函数也无法找到确切的单词。我在第一行尝试了不同的字符串,但函数仍然找不到它们。“With Rows(1)Set c=.Find(Header,LookIn:=xlValues)”中是否存在任何错误?如果标题是电子表格(Sheet1)的第1行,则函数将查找该标题;如果标题是第2行或第3行,则需要更新代码以反映该错误。代码也被略微更新,以更明确地指向正在使用的工作表。再次打开多张图纸时,可能需要更新以指向所需的图纸。
Sub AssignValues()
Dim Next_6 As Long, PriceChange As Long, Price_i As Long, MyWorksheetLastRow As Long
With ThisWorkbook.Worksheets(1)
MyWorksheetLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Next_6 = ColInstance("Next_6_months", 1)
If Next_6 > 0 Then
For Price_i = 2 To MyWorksheetLastRow
.Cells(Price_i, Next_6).Value = .Cells(Price_i, Next_6).Value & " " & .Cells(Price_i, Next_6 + 1).Value
Next Price_i
End If
End With
End Sub
Function ColInstance(Header As String, Optional Instance As Long = 1) As Long
' Function returns 0 if Header doesn't exist in specified row
' Function returns -1 if Header exists but number of instances < specified
ColInstance = 0
Dim i As Long: i = 1
Dim c As Range
With ThisWorkbook.Worksheets(1).Rows(1)
Set c = .Find(Header, LookIn:=xlValues)
If Not c Is Nothing Then
FirstAdr = c.Address
Do
i = i + 1
If i > Instance Then
ColInstance = c.Column
Exit Do
End If
Set c = .FindNext(c)
Loop While c.Address <> FirstAdr
If c.Address = FirstAdr And Instance > 1 Then ColInstance = -1
End If
End With
End Function