很抱歉发布了很多问题,我现在发现了,我应该做些什么。
我想要一个子CountIf很多不同的变量,并保存这些。然后它应检查名为“Statistics”的工作表是否已存在。如果是,它应该询问是否“覆盖”是和/否。如果它尚不存在,则应创建它,并将信息放入“统计”表中。
它应该是1分,但是我在2开始时不要混淆 - 但我似乎无法让我的Sheet-checker工作。
你们其中一个人有可能将我的2个潜艇分成1个潜艇,也许可以解释我应该做些什么?
非常感谢
Sub1
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets("Statistics")
If Not ws Is Nothing Then
MsgBox "A Sheet named 'Statistics' already exists"
Else
MsgBox "'Statistics'-sheet does not already exist"
End If
If Err.Number = 9 Then
ans = MsgBox("The file, " & save_name & " exists, do you want to overwite it?", vbYesNo + vbQuestion)
Select Case ans
Case vbYes
'overwrite the file
wb.SaveAs FileName:=WAD_path & "\" & "WADs " & "Rev " & Rev & "\" & save_name, FileFormat:=51
Case vbNo
'exit
MsgBox "Ensure the data is correct." & vbCrLf & "The process will end.", vbOKOnly + vbExclamation
Exit Sub
End Select
Set ws = Worksheets.Add(After:=Sheets(Worksheets.Count))
ws.Name = "Statistics"
End If
With ws
'do stuff
End With
End Sub
分公司2
Sub Opgave3Dim()
'Dim'er de forskellige fakulteter 1-5 som Long
Dim information1 As Long
Dim information2 As Long
Dim information3 As Long
Dim information4 As Long
Dim information5 As Long
'Sætter informationerne til et eller 2 af tallene hvor 2*x = Kandidat, 1*x = Bachelor
information1 = WorksheetFunction.CountIfs(Columns("I"), "1", Columns("K"), "Bachelor")
information11 = WorksheetFunction.CountIfs(Columns("I"), "1", Columns("K"), "Master")
information2 = WorksheetFunction.CountIfs(Columns("I"), "2", Columns("K"), "Bachelor")
information22 = WorksheetFunction.CountIfs(Columns("I"), "2", Columns("K"), "Master")
information3 = WorksheetFunction.CountIfs(Columns("I"), "3", Columns("K"), "Bachelor")
information33 = WorksheetFunction.CountIfs(Columns("I"), "3", Columns("K"), "Master")
information4 = WorksheetFunction.CountIfs(Columns("I"), "4", Columns("K"), "Bachelor")
information44 = WorksheetFunction.CountIfs(Columns("I"), "4", Columns("K"), "Master")
information5 = WorksheetFunction.CountIfs(Columns("I"), "5", Columns("K"), "Bachelor")
information55 = WorksheetFunction.CountIfs(Columns("I"), "5", Columns("K"), "Master")
End Sub
答案 0 :(得分:1)
正如Shai所提到的,完全限定你的对象,否则他们会引用活动表,而活动表可能不是你真正想要的那个。
不要声明这么多变量。您可以在您的案例中使用数组,然后使用循环来填充它们。
避免使用.Select
。您可能希望查看How to avoid using Select in Excel VBA
这是你在尝试什么? (的未测试强>)
Sub Opgave3Dim()
Dim InfoAr_A(1 To 5) As String
Dim InfoAr_B(1 To 5) As String
Dim i As Long
Dim ws As Worksheet, wsNew As Worksheet
'~~> Change this to the sheet where you want to do Countifs
'~~> I have moved this before you add the sheet because if
'~~> you do not qualify your range, your code will refer to
'~~> the new sheet
Set ws = Sheet1
With ws
For i = 1 To 5
InfoAr_A(i) = Application.WorksheetFunction.CountIfs(.Columns("I"), i, .Columns("K"), "Bachelor")
InfoAr_B(i) = Application.WorksheetFunction.CountIfs(.Columns("I"), i, .Columns("K"), "Master")
Next i
End With
'~~> Use this to check what is stored in the array
'For i = 1 To 5
' Debug.Print InfoAr_A(i)
' Debug.Print InfoAr_B(i)
'Next i
'~~> Add a new sheet
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Statistics"
Set ws = ActiveSheet
End With
Sheets(2).Range("I17").Value = "aaa"
End Sub