这是我的代码。我尝试运行它,它不断弹出这个错误,即使它在它正常工作之前。我添加的唯一内容是工作表的最后一件事。
哦,除此之外,如何在打开一张特定纸张时让它运行。我问的是,因为每当我将文件保存在另一张纸上然后重新打开文件时,它就会显示一个正常的错误,因为我在另一张纸上,我应该这样做。
Private Sub Workbook_Open()
Dim j As Integer, i As Integer, k As Integer, result As String, temp As Long, answer As Integer, myValue1 As Integer, l As Integer, Number As Integer, Number1 As Integer
If Cells(3, 2) = Cells(3, 8) Then
result = "fail"
j = 2
Do While result = "fail" And Cells(9, j) <> ""
If Cells(9, j).Value >= 15 Then
result = "pass"
Else
j = j + 1
End If
Loop
If result = "fail" Then
answer = MsgBox("Do you want to add more weeks?", vbYesNo + vbQuestion, "Question To User")
If answer = vbYes Then
myValue1 = InputBox("How many more weeks do you want to bulk?")
For i = 7 To 13
Cells(i, j - 1).Select
Selection.Copy
Cells(i, 100).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Cells(i, j - 1) = " "
Next i
For i = 15 To 15 + Cells(2, 8)
Cells(i, j - 1).Select
Selection.Copy
Cells(i, 100).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Cells(i, j - 1) = " "
Next i
For i = 17 + Cells(2, 8) To 26 + Cells(2, 8)
Cells(i, j - 1).Select
Selection.Copy
Cells(i, 100).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Cells(i, j - 1) = " "
Next i
For i = j - 1 To j + myValue1 - 2
Cells(7, i) = "Week" & " " & i - 1
Cells(7, i).Select
Selection.Font.Bold = True
With Selection.Font
.Color = -4165632
.TintAndShade = 0
End With
Cells(15, i) = "Week" & " " & i - 1
Cells(15, i).Select
Selection.Font.Bold = True
With Selection.Font
.Color = -4165632
.TintAndShade = 0
End With
Cells(17 + Cells(2, 8), i) = "Week" & " " & i - 1
Cells(17 + Cells(2, 8), i).Select
Selection.Font.Bold = True
With Selection.Font
.Color = -4165632
.TintAndShade = 0
End With
Next i
For j = 7 To 13
Cells(j, 100).Select
Selection.Copy
Cells(j, i).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Cells(j, 100) = " "
If j = 7 Then
Cells(j, i).Select
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.Font.Bold = True
End If
Next j
For j = 15 To 15 + Cells(2, 8)
Cells(j, 100).Select
Selection.Copy
Cells(j, i).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Cells(j, 100) = " "
If j = 15 Then
Cells(j, i).Select
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.Font.Bold = True
End If
Next j
For j = 17 + Cells(2, 8) To 26 + Cells(2, 8)
Cells(j, 100).Select
Selection.Copy
Cells(j, i).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Cells(j, 100) = " "
If j = 17 + Cells(2, 8) Then
Cells(j, i).Select
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.Font.Bold = True
End If
Next j
Number = i
Cells(3, 8) = myValue1 - 2
Else
'do nothing
End If
Else
MsgBox "It is generally advised for men to stay under 15% Body-Fat when trying to gain muscle. So please move to the next Cut Sheet to start losing fat."
k = j
Do While Cells(8, k) <> ""
k = k + 1
Loop
For i = 8 To 13
For l = j To k - 2
Cells(i, l) = " "
Next l
Next i
For i = 8 To 13
Cells(i, j) = Cells(i, k - 1)
Cells(i, k - 1) = " "
Next i
Number = k - 1
Cells(3, 8) = k - 2
End If
For i = 1 To Number
Columns(i).Select
Columns(i).EntireColumn.AutoFit
Next i
End If
Worksheets("Sheet4").Range("B4") = Worksheets("Sheet1").Cells(3,Worksheets("Sheet1").Cells(3, 8))
End Sub
答案 0 :(得分:0)
在处理对工作簿和工作表的特定引用时,应特别调用当前目标。 (如果我完全理解你的询问)
'active target
Dim objWB As Workbook: Set objWB = ActiveWorkbook
Dim objWS As Worksheet: Set objWS = objWB.ActiveSheet
objWS.Cells(...
objWS.Range(...
如果您打开了多个Excel工作簿,则无论代码在何处运行,代码有时会针对焦点目标运行。
修改*附加强> 在过程调用开始时命名工作表并引用它们也将消除焦点代码问题的错误。
Dim objWS As Worksheet:设置objWS = objWB.Worksheets(“MyWorksheet”)