第一次潜伏,因为这是我第一个真正的VBA宏,所以我第一次遇到了海报,而且遇到了我无法克服的障碍。
我有一本Excel工作簿,其工作表的格式如下:
我一直在尝试编写一些代码,用一个公式将“%Weight”列填充到该列中,该公式将“ Weight”列中的相邻单元格的值除以包含每个函数下方的sum函数的单元格的值细胞范围。
我在一张纸上有几十张桌子,被几行空白隔开,所有表格的格式都是垂直的。我需要这些单元格来确定正确的求和单元格,然后将偏移单元格除以该值。
我尝试了以下代码。
基本上,我尝试通过“%Weight”列运行For Each循环,并确定“ Weight”中的相邻单元格何时不为空。然后,它将通过设置范围变量来标识偏移量单元格,然后设置另一个变量以标识范围内的最终单元格,从而标识包含求和公式的单元格。
我确实知道我的If逻辑是否可以正常工作,因为如果存在相邻的单元格并且可以正常工作,那么我必须在“%Weight”列中填充值为“ 1”。
我不断收到错误424或键入不匹配。
提供问题的代码块:
Dim cell As Range, rng2 As Range, sideweight As Range, TargetWeight As Range
Dim TargetWeightr As Long, Dim TargetWeightc As Long
rng2 = Range("D1:D" & LR)
For Each cell In rng2
If cell.Offset(0, -1).Value <> "" Then
Set sidewight = cell.Offset(0, -1)
Set TargetWeight = sideweight.End(xlDown)
Set TargetWeightr = TargetWeight.Address.Row
Set TargetWeightc = TargetWeight.Address.Column
'cell.FormulaR1C1 = "=RC[-1]/R[" & TargetWeightr & "]C[" & TargetWeightc & "]"
End If
Next cell
整个上下文宏
Sub WeightCalculations2()
Application.ScreenUpdating = False
Dim rng As Range, cell As Range, rng2 As Range, rA As Range, totalweight As Range, totalweightrng As Range
Dim sideweight As Range, TargetWeight As Range
Dim LR As Long, TargetWeightr As Long, TargetWeightC As Long
Dim ws As Worksheet
Set ws = ActiveSheet
With ActiveSheet
LR = Cells(Rows.Count, "A").End(xlUp).Row
End With
Set rng = ws.Range("I2:I" & LR)
Set rng2 = ws.Range("J2:J" & LR)
For Each cell In rng
If cell.Offset(0, -1).Value = "EA" Then cell.FormulaR1C1 = "=RC[-2]*RC[3]"
If cell.Offset(0, -1).Value = "LB" Then cell.FormulaR1C1 = "=RC[-2]*1"
Next cell
For Each cell In rng
If WorksheetFunction.IsError(cell) Then cell.Formula = "=1*0"
Next cell
For Each rA In Columns("I").SpecialCells(xlFormulas).Areas
rA.Cells(rA.Cells.Count + 1).Formula = "=SUM(" & rA.Address & ")"
Next rA
For Each cell In rng2
If cell.Offset(0, -1).Value <> "" Then
Set sidewight = cell.Offset(0, -1)
Set TargetWeight = sideweight.End(xlDown)
Set TargetWeightr = TargetWeight.Address.Row
Set TargetWeightC = TargetWeight.Address.Column
'cell.FormulaR1C1 = "=RC[-1]/R[" & totalweightrn & "]C[" & totalweightcn & "]"
End If
Next cell
End Sub
预期输出: 该程序使用公式将“重量”列中相应偏移量单元格的值除以包含对应单元格范围总和的单元格值,来填充“%重量”列中的单元格。
实际输出: 错误424和/或错误不匹配。
请让我知道其他什么信息对回答这个问题会有所帮助,并尽我所能使内容更全面。
答案 0 :(得分:0)
TargetWeight.Address.Row
应该是TargetWeight.Row
TargetWeight.Address.Column
应该是TargetWeight.Column
[n]
内的 n 是相对的行或列调整。 RC[-1]
表示同一行,左一列。您需要一个绝对地址,并且行和列的绝对长度都是长整数,所以R" & totalweightr & "C" & totalweightc
您没有Set
整数值,而是给它们分配了=
。您仅Set
个对象,例如范围,单元格,工作表等。
For Each cell In rng2
If cell.Offset(0, -1).Value <> "" Then
Set sidewight = cell.Offset(0, -1)
Set TargetWeight = sideweight.End(xlDown)
TargetWeightr = TargetWeight.Row
TargetWeightc = TargetWeight.Column
cell.FormulaR1C1 = "=RC[-1]/R" & TargetWeightr & "C" & TargetWeightc
End If
Next cell
您可能还想忘记所有操作,而只使用xlR1C1样式的TargetWeight.Address。
For Each cell In rng2
If cell.Offset(0, -1).Value <> "" Then
Set sideweight = cell.Offset(0, -1)
Set TargetWeight = sideweight.End(xlDown)
cell.FormulaR1C1 = "=RC[-1]/" & TargetWeight.Address(referencestyle:=xlR1C1)
End If
Next cell