我正在尝试编写一些VBA代码以基于if语句识别正确的行,然后将每行中的特定值复制到不同的表以使表格更简洁。我之前已经制作了几个vba程序,并且可以正常修复我自己的错误,但我无法弄清楚这个错误。每次我运行它都会抛出一个400错误,它总是在for循环开始后尝试访问if语句时。 for循环中的所有代码都无法正常工作。我在不同的程序中使用了几乎这个确切的代码,它在那里工作。我认为它与尝试引用它没有认为它有权访问的表格有关,但所有表格都没有受到保护而且没有被隐藏。
如果有人有更好的方法来做到这一点,或者可以向我解释我的错误,我会非常感激。我觉得我有一个很大的想法,我还没有完全理解。我只是不知道在这种情况下是什么,谷歌搜索/浏览这个网站上的多个线程没有帮助。
'Variables for the subs
Dim RowStart As Integer
Dim RowEnd As Integer
Dim ColYel As String
Dim ColRed As String
Dim ProdName As String
Dim ActUse As String
Dim UseDif As String
Dim ActCost As String
Dim CostDif As String
'Set a place to temporarily paste values and copy as a table'
'TAKEN CARE OF BY SHEET TABLE'
Dim count As Integer
Public Sub Yellow()
'Unprotect sheet
'ThisWorkbook.Unprotector
'reset table formatting
Sheets("Table").Range("A1:E36").Borders(xlInsideHorizontal).Weight = xlThin
'Turn off updating and make table visible
Application.ScreenUpdating = False
'Find Yellow rows by identifying ColYel = 1
RowStart = 8
RowEnd = 34
ColYel = U
ColRed = V
ProdName = A
ActUse = G
UseDif = i
ActCost = K
CostDif = M
'Set count to 2 for table sheet
count = 2
'Copy product name, actual use, use dif, actual cost, cost dif
MsgBox "Starting Loop"
For i = RowStart To RowEnd
If ActiveSheet.Range(ColYel & i).Value = 1 Then
ActiveSheet.Range(ProdName & i).Copy (Sheets("Table").Range("A" & count))
ActiveSheet.Range(ActUse & i).Copy (Sheets("Table").Range("B" & count))
ActiveSheet.Range(UseDif & i).Copy (Sheets("Table").Range("C" & count))
ActiveSheet.Range(ActCost & i).Copy (Sheets("Table").Range("D" & count))
ActiveSheet.Range(CostDif & i).Copy (Sheets("Table").Range("E" & count))
count = count + 1
End If
Next i
MsgBox "Finished Loop"
'Copy new table to clipboard.
Sheets("Table").Range("A1:E" & count).Borders(xlEdgeBottom).Weight = xlThick
Sheets("Table").Range("A1:E" & count).Copy
'Turn on updating and make table visible = falst
Application.ScreenUpdating = True
'Reprotect sheet
'ThisWorkbook.Protector
End Sub
答案 0 :(得分:0)
Public Sub Yellow()
'Unprotect sheet
'ThisWorkbook.Unprotector
'reset table formatting
'Sheets("Table").Range("A1:E36").Borders(xlInsideHorizontal).Weight = xlThin
'Turn off updating and make table visible
Application.ScreenUpdating = False
'Find Yellow rows by identifying ColYel = 1
RowStart = 8
RowEnd = 34
ColYel = "U"
ColRed = "V"
ProdName = "A"
ActUse = "G"
UseDif = "i"
ActCost = "K"
CostDif = "M"
'Set count to 2 for table sheet
Count = 2
'Copy product name, actual use, use dif, actual cost, cost dif
MsgBox "Starting Loop"
For i = RowStart To RowEnd
If ActiveSheet.Range(ColYel & i) = 1 Then
ActiveSheet.Range(ProdName & i).Copy (Sheets("Table").Range("A" & Count))
ActiveSheet.Range(ActUse & i).Copy (Sheets("Table").Range("B" & Count))
ActiveSheet.Range(UseDif & i).Copy (Sheets("Table").Range("C" & Count))
ActiveSheet.Range(ActCost & i).Copy (Sheets("Table").Range("D" & Count))
ActiveSheet.Range(CostDif & i).Copy (Sheets("Table").Range("E" & Count))
Count = Count + 1
End If
Next i
End Sub
你从不把字符串放在引号中,即ColRed =" V"没有ColRed = V