excel - 使用 If 语句循环特定工作表时出现空错误
问题描述
我收到“无法在数据透视表报告中输入空值作为项目或字段名称”的错误消息
Sub loopthruallsheets()
Dim Shts() As Variant
Dim Sheet As Worksheet
Shts = Array("1", "3", "5", "7")
For Each Sheet In ActiveWorkbook.Worksheets
With Sheet
If .Range("G2").Value = 1 Then
.Range("H10:H11").Value = .Range("N10:N11").Value
.Range("H14:H22").Value = .Range("N14:N22").Value
.Range("H27:H29").Value = .Range("N27:N29").Value
End If
If .Range("G2").Value = 2 Then
.Range("H10:H11").Value = .Range("O10:O11").Value
.Range("H14:H22").Value = .Range("O14:O22").Value
.Range("H27:H29").Value = .Range("O27:O29").Value
End If
If .Range("G2").Value = 3 Then
.Range("H10:H11").Value = .Range("P10:P11").Value
.Range("H14:H22").Value = .Range("P14:P22").Value
.Range("H27:H29").Value = .Range("P27:P29").Value
End If
If .Range("G2").Value = 4 Then
.Range("H10:H11").Value = .Range("Q10:Q11").Value
.Range("H14:H22").Value = .Range("Q14:Q22").Value
.Range("H27:H29").Value = .Range("Q27:Q29").Value
End If
If .Range("G2").Value = 5 Then
.Range("H10:H11").Value = .Range("R10:R11").Value
.Range("H14:H22").Value = .Range("R14:R22").Value
.Range("H27:H29").Value = .Range("R27:R29").Value
End If
If .Range("G2").Value = 6 Then
.Range("H10:H11").Value = .Range("S10:S11").Value
.Range("H14:H22").Value = .Range("S14:S22").Value
.Range("H27:H29").Value = .Range("S27:S29").Value
End If
If .Range("G2").Value = 7 Then
.Range("H10:H11").Value = .Range("T10:T11").Value
.Range("H14:H22").Value = .Range("T14:T22").Value
.Range("H27:H29").Value = .Range("T27:T29").Value
End If
If .Range("G2").Value = 8 Then
.Range("H10:H11").Value = .Range("U10:U11").Value
.Range("H14:H22").Value = .Range("U14:U22").Value
.Range("H27:H29").Value = .Range("U27:U29").Value
End If
If .Range("G2").Value = 9 Then
.Range("H10:H11").Value = .Range("V10:V11").Value
.Range("H14:H22").Value = .Range("V14:V22").Value
.Range("H27:H29").Value = .Range("V27:V29").Value
End If
If .Range("G2").Value = 10 Then
.Range("H10:H11").Value = .Range("W10:W11").Value
.Range("H14:H22").Value = .Range("W14:W22").Value
.Range("H27:H29").Value = .Range("W27:W29").Value
End If
If .Range("G2").Value = 11 Then
.Range("H10:H11").Value = .Range("X10:X11").Value
.Range("H14:H22").Value = .Range("X14:X22").Value
.Range("H27:H29").Value = .Range("X27:X29").Value
End If
If .Range("G2").Value = 12 Then
.Range("H10:H11").Value = .Range("Y10:Y11").Value
.Range("H14:H22").Value = .Range("Y14:Y22").Value
.Range("H27:H29").Value = .Range("Y27:Y29").Value
End If
End With
Next Sheet
End Sub
我需要代码将值复制并粘贴到特定位置,但每个不同时期(months1-12)都有不同的数据列。我的代码在第 4 期出错(这是我本财政年度的当前期)
解决方案
没有真正需要有那么多If
语句(或使用Select Case
)。这只是您可以使用的模式,Cells()
而不仅仅是Range()
:
Sub t()
Dim sheet As Worksheet
Dim startCol As Long, celVal As Long, i As Long
Dim shts() As Variant
startCol = 13 ' 14 is Column M
shts = Array(1, 3, 5, 7)
For i = LBound(shts) To UBound(shts)
With ActiveWorkbook.Worksheets(shts(i))
celVal = .Range("G2").Value
.Range("H10:H11").Value = .Range(.Cells(10, startCol + celVal), .Cells(11, startCol + celVal)).Value
.Range("H14:H22").Value = .Range(.Cells(14, startCol + celVal), .Cells(22, startCol + celVal)).Value
.Range("H27:H29").Value = .Range(.Cells(27, startCol + celVal), .Cells(29, startCol + celVal)).Value
End With
Next i
End Sub
根据您的评论,我还调整了For
循环,而不是仅循环浏览您想要的特定工作表。(我还假设这是您要使用1, 3, 5, 7
的工作表索引1
,而不是实际工作表名称。如果工作表确实命名为,3
等,则使用shts = Array("1", "3", "5", "7")
)
推荐阅读
- r - 用 plotly 创建一个稍微重叠的条形图
- firebase - 如何返回未来
- > 从列表
>? - mysql - 如何选择列的每个不同值的最新记录
- kdb - 如何在 KDB 中将列表转换为矩阵?
- npm - 我可以在一个项目中独立安装两个 packages.json 吗?
- scala - 通过修改其私有方法来自定义 Spark ML 估计器(例如 GaussianMixture)的正确方法?
- python - 从 pikepdf 发送 pdf 作为 BytesIO 流-flask send_file 内容长度不匹配
- laravel - CORS 预检请求错误:预检请求不允许重定向
- java - 创建一个有效的“输入代码”字段(Java)
- docker - 在wsl模式和cmd模式下运行docker命令有什么区别吗?