Excel宏仅格式化重复值的第一个实例
我有一个像这样的Excel makro 我希望它格式化并左对齐Streckkod和Återlämningsdatam的所有实例,但它只适用于forst事件。我做错了什么 我只是一个复制粘贴程序员,这是我第二次尝试用vba做一些事情,所以任何帮助都将不胜感激Excel宏仅格式化重复值的第一个实例,excel,vba,Excel,Vba,我有一个像这样的Excel makro 我希望它格式化并左对齐Streckkod和Återlämningsdatam的所有实例,但它只适用于forst事件。我做错了什么 我只是一个复制粘贴程序员,这是我第二次尝试用vba做一些事情,所以任何帮助都将不胜感激 Sub Transform() Dim wshS As Worksheet Dim wshT As Worksheet Dim s As Long Dim m As Long Dim t As Long
Sub Transform()
Dim wshS As Worksheet
Dim wshT As Worksheet
Dim s As Long
Dim m As Long
Dim t As Long
Dim c As Long
Dim d As Double
Application.ScreenUpdating = False
Set wshS = ActiveSheet
wshS.Range("A1").CurrentRegion.Sort Key1:=wshS.Range("A1"), Header:=xlYes
m = wshS.Range("A" & wshS.Rows.Count).End(xlUp).Row
For s = 2 To m
If s > 2 Then
t = t + 2
wshT.Cells(t, 1).Value = "total price"
wshT.Cells(t, 2).Value = d
End If
If wshS.Range("A" & s).Value <> wshS.Range("A" & s - 1).Value Then
Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))
For c = 1 To 1
wshT.Cells(c, 1).Value = wshS.Cells(1, c).Value
wshT.Cells(c, 2).Value = wshS.Cells(s, c).Value
wshT.Cells(c, 2).NumberFormat = "0000000000"
wshT.Cells(c, 2).HorizontalAlignment = xlLeft
'Personnummer
Next c
t = 1
d = 0
For c = 2 To 5
wshT.Cells(c, 1).Value = wshS.Cells(1, c).Value
wshT.Cells(c, 2).Value = wshS.Cells(s, c).Value
Next c
t = 5
d = 0
End If
t = t + 1
For c = 6 To 8
t = t + 1
wshT.Cells(t, 1).Value = wshS.Cells(1, c).Value
wshT.Cells(t, 2).Value = wshS.Cells(s, c).Value
wshT.Cells(c, 1).EntireColumn.AutoFit
wshT.Cells(c, 2).EntireColumn.AutoFit
Next c
For c = 9 To 9
t = t + 1
wshT.Cells(t, 1).Value = wshS.Cells(1, c).Value
wshT.Cells(t, 2).Value = wshS.Cells(s, c).Value
wshT.Cells(c, 2).NumberFormat = "0000000000000"
wshT.Cells(c, 2).HorizontalAlignment = xlLeft
'Streckkod
Next c
For c = 10 To 10
t = t + 1
wshT.Cells(t, 1).Value = wshS.Cells(1, c).Value
wshT.Cells(t, 2).Value = wshS.Cells(s, c).Value
wshT.Cells(c, 2).NumberFormat = "yyyy-mm-dd"
wshT.Cells(c, 2).HorizontalAlignment = xlLeft
'återlämningsdatum
Next c
d = d + wshS.Cells(s, 10).Value
Next s
' Last total
t = t + 2
wshT.Cells(t, 1).Value = "total price"
wshT.Cells(t, 2).Value = d
Application.ScreenUpdating = True
End Sub
子变换()
将wshS设置为工作表
将wshT设置为工作表
他和我一样长
我和你一样长
我没那么长
尺寸c与长度相同
d为双色
Application.ScreenUpdating=False
设置wshS=ActiveSheet
wshS.Range(“A1”).CurrentRegion.Sort关键字1:=wshS.Range(“A1”),标头:=xlYes
m=wshS.Range(“A”&wshS.Rows.Count).End(xlUp).Row
对于s=2到m
如果s>2,则
t=t+2
wshT.Cells(t,1).Value=“总价”
wshT.单元(t,2).值=d
如果结束
如果wshS.Range(“A”&s).Value为wshS.Range(“A”&s-1).Value,则
设置wshT=Worksheets.Add(之后:=Worksheets(Worksheets.Count))
对于c=1到1
wshT.Cells(c,1).Value=wshS.Cells(1,c).Value
wshT.Cells(c,2).Value=wshS.Cells(s,c).Value
wshT.Cells(c,2).NumberFormat=“0000000000”
wshT.单元格(c,2).水平对齐=xlLeft
“佩森努默
下一个c
t=1
d=0
对于c=2到5
wshT.Cells(c,1).Value=wshS.Cells(1,c).Value
wshT.Cells(c,2).Value=wshS.Cells(s,c).Value
下一个c
t=5
d=0
如果结束
t=t+1
对于c=6到8
t=t+1
wshT.Cells(t,1).Value=wshS.Cells(1,c).Value
wshT.Cells(t,2).Value=wshS.Cells(s,c).Value
wshT.Cells(c,1).EntireColumn.AutoFit
wshT.Cells(c,2).EntireColumn.AutoFit
下一个c
对于c=9到9
t=t+1
wshT.Cells(t,1).Value=wshS.Cells(1,c).Value
wshT.Cells(t,2).Value=wshS.Cells(s,c).Value
wshT.Cells(c,2).NumberFormat=“0000000000000”
wshT.单元格(c,2).水平对齐=xlLeft
“斯特里克科
下一个c
对于c=10到10
t=t+1
wshT.Cells(t,1).Value=wshS.Cells(1,c).Value
wshT.Cells(t,2).Value=wshS.Cells(s,c).Value
wshT.Cells(c,2).NumberFormat=“yyyy-mm-dd”
wshT.单元格(c,2).水平对齐=xlLeft
återlämningsdatam
下一个c
d=d+wshS.单元格(s,10).值
下一个s
“最后总数
t=t+2
wshT.Cells(t,1).Value=“总价”
wshT.单元(t,2).值=d
Application.ScreenUpdating=True
端接头
您是否可以将Streckkod用于c=9到10,并删除第二个“återlämningsdatam”部分?这将为第一个循环提供更清晰的代码,但不会影响以下实例