在VBA中编写一个简短的脚本来对数据进行排序,主要是计算输入到列表中的名称数量,然后将它们打印到新列中。然而,&在“Set Staff 1”行中由于不匹配导致编译错误。这似乎是由同时设置两个范围引起的。我觉得有一个简单明了的解决方案......
Sub Staffing()
Dim Rng As Range
Dim i As Long
Dim Staff1 As Range
Dim Staff2 As Range
Dim Staff3 As Range
Dim Staff4 As Range
Dim Staff5 As Range
Dim Staff6 As Range
Dim Staff7 As Range
While i <= 300
Set Rng = Range("J" & i)
Set Staff1 = ("X" & i)
Set Staff2 = ("AD" & i)
Set Staff3 = ("AJ" & i)
Set Staff4 = ("AP" & i)
Set Staff5 = ("BB" & i)
Set Staff6 = ("BH" & i)
Set Staff7 = ("BN" & i)
If Staff1 <> "" Then
Rng.FormulaR1C1 = "0"
i = i + 1
If Staff2 <> "" Then
Rng.FormulaR1C1 = "1"
i = i + 1
If Staff3 <> "" Then
Rng.FormulaR1C1 = "2"
i = i + 1
If Staff4 <> "" Then
Rng.FormulaR1C1 = "3"
i = i + 1
If Staff5 <> "" Then
Rng.FormulaR1C1 = "4"
i = i + 1
If Staff6 <> "" Then
Rng.FormulaR1C1 = "5"
i = i + 1
If Staff7 <> "" Then
Rng.FormulaR1C1 = "6"
i = i + 1
Else
Stop
End If
Wend
End Sub
提前致谢!
感谢大家的耐心。我写这个第一个代码时遇到了巨大的错误,但是由于你的帮助以及我自己的一些反复试验而修复了它们。正确的代码如下。我相信你现在能够看到我想要做的事情!
Sub StaffingNumbers()
Dim Rng As Range
Dim i As Long
Dim Staff1 As Range
Dim Staff2 As Range
Dim Staff3 As Range
Dim Staff4 As Range
Dim Staff5 As Range
Dim Staff6 As Range
Dim Staff7 As Range
Dim Staff8 As Range
Dim Staff9 As Range
Dim Staff10 As Range
i = 3
While i <= 300
Set Rng = Range("J" & i)
Set Staff1 = Range("X" & i)
Set Staff2 = Range("AD" & i)
Set Staff3 = Range("AJ" & i)
Set Staff4 = Range("AP" & i)
Set Staff5 = Range("AV" & i)
Set Staff6 = Range("BB" & i)
Set Staff7 = Range("BH" & i)
Set Staff8 = Range("BN" & i)
If Staff1 = "" Then
Rng.FormulaR1C1 = "0"
i = i + 1
ElseIf Staff1 <> "" And Staff2 = "" Then
Rng.FormulaR1C1 = "1"
i = i + 1
ElseIf Staff2 <> "" And Staff3 = "" Then
Rng.FormulaR1C1 = "2"
i = i + 1
ElseIf Staff3 <> "" And Staff4 = "" Then
Rng.FormulaR1C1 = "3"
i = i + 1
ElseIf Staff4 <> "" And Staff5 = "" Then
Rng.FormulaR1C1 = "4"
i = i + 1
ElseIf Staff5 <> "" And Staff6 = "" Then
Rng.FormulaR1C1 = "5"
i = i + 1
ElseIf Staff6 <> "" And Staff7 = "" Then
Rng.FormulaR1C1 = "6"
i = i + 1
ElseIf Staff7 <> "" And Staff8 = "" Then
Rng.FormulaR1C1 = "7"
i = i + 1
ElseIf Staff8 <> "" Then
Rng.FormulaR1C1 = "8"
i = i + 1
Else
Stop
End If
Wend
Set Rng = Nothing
Set Staff1 = Nothing
Set Staff2 = Nothing
Set Staff3 = Nothing
Set Staff4 = Nothing
Set Staff5 = Nothing
Set Staff6 = Nothing
Set Staff7 = Nothing
End Sub
答案 0 :(得分:2)
您错过了该行中的<p:fileUpload>
,第一行的集合Range
是正确的,其他所有行也应该相似。
答案 1 :(得分:1)
您忘记在Range
中添加Set Staff2 = Range("AD" & i)
Set Staff2 = Range("AD" & i)
另外,请勿忘记在触发结束时使用Set Staff2 = Nothing
您还忘了用If
关闭大部分End If
语句,我按原样放弃,因为我不知道您要对代码做什么:
Sub Staffing()
Dim Ws As Worksheet
Dim Rng As Range
Dim i As Long
Dim Staff1 As Range
Dim Staff2 As Range
Dim Staff3 As Range
Dim Staff4 As Range
Dim Staff5 As Range
Dim Staff6 As Range
Dim Staff7 As Range
i = 1
Set Ws = ThisWorkbook.Sheets("SheetNameHere")
While i <= 300
With Ws
Set Rng = .Range("J" & i)
Set Staff1 = .Range("X" & i)
Set Staff2 = .Range("AD" & i)
Set Staff3 = .Range("AJ" & i)
Set Staff4 = .Range("AP" & i)
Set Staff5 = .Range("BB" & i)
Set Staff6 = .Range("BH" & i)
Set Staff7 = .Range("BN" & i)
End With
If Staff1.Value <> "" Then
Rng.FormulaR1C1 = "0"
i = i + 1
If Staff2 <> "" Then
Rng.FormulaR1C1 = "1"
i = i + 1
If Staff3 <> "" Then
Rng.FormulaR1C1 = "2"
i = i + 1
If Staff4 <> "" Then
Rng.FormulaR1C1 = "3"
i = i + 1
If Staff5 <> "" Then
Rng.FormulaR1C1 = "4"
i = i + 1
If Staff6 <> "" Then
Rng.FormulaR1C1 = "5"
i = i + 1
If Staff7 <> "" Then
Rng.FormulaR1C1 = "6"
i = i + 1
Else
Stop
End If
Wend
Set Ws = Nothing
Set Rng = Nothing
Set Staff1 = Nothing
Set Staff2 = Nothing
Set Staff3 = Nothing
Set Staff4 = Nothing
Set Staff5 = Nothing
Set Staff6 = Nothing
Set Staff7 = Nothing
End Sub
答案 2 :(得分:1)
在范围分配后您想要完成的逻辑尚不清楚。也许这就是你希望的目标。
Dim i As Long
Dim Staff1 As Range, Staff2 As Range, Staff3 As Range
Dim Staff4 As Range, Staff5 As Range, Staff6 As Range
Dim Staff7 As Range, Rng As Range
While i <= 300
Set Rng = Range("J" & i)
Set Staff1 = Range("X" & i)
Set Staff2 = Range("AD" & i)
Set Staff3 = Range("AJ" & i)
Set Staff4 = Range("AP" & i)
Set Staff5 = Range("BB" & i)
Set Staff6 = Range("BH" & i)
Set Staff7 = v("BN" & i)
If Staff1 <> "" Then
Rng.FormulaR1C1 = "0"
i = i + 1
ElseIf Staff2 <> "" Then
Rng.FormulaR1C1 = "1"
i = i + 1
ElseIf Staff3 <> "" Then
Rng.FormulaR1C1 = "2"
i = i + 1
ElseIf Staff4 <> "" Then
Rng.FormulaR1C1 = "3"
i = i + 1
ElseIf Staff5 <> "" Then
Rng.FormulaR1C1 = "4"
i = i + 1
ElseIf Staff6 <> "" Then
Rng.FormulaR1C1 = "5"
i = i + 1
ElseIf Staff7 <> "" Then
Rng.FormulaR1C1 = "6"
i = i + 1
Else
Stop
End If
Wend
我真的不清楚为什么Stop statement存在。也许你想退出Do While?那将是Exit Do
。