Excel VBA中每个循环的多个范围
我有一张有以下数据的表格Excel VBA中每个循环的多个范围,vba,excel,Vba,Excel,我有一张有以下数据的表格 Category | Amount | Daily Charges | Misc Charges | Vendor Charges ------------ |-----------| --------------|--------------|------------------- Daily Charges |500,000.00 | | | --------------|---
Category | Amount | Daily Charges | Misc Charges | Vendor Charges
------------ |-----------| --------------|--------------|-------------------
Daily Charges |500,000.00 | | |
--------------|-----------|---------------|--------------|-------------------
Misc Charges | 500.00 | | |
--------------|-----------| --------------|--------------|-------------------
Vendor Charges| 50,000.00 | | |
我需要使用宏填写第3列(每日费用)、第4列(杂项费用)和第5列(供应商费用),如下所示
Category | Amount | Daily Charges | Misc Charges | Vendor Charges
------------ |-----------| --------------|--------------|-------------------
Daily Charges |500,000.00 | 500,000.00 | 0 | 0
--------------|-----------|---------------|--------------|-------------------
Misc Charges | 500.00 | 0 | ₹ 500.00 | 0
--------------|-----------| --------------|--------------|-------------------
Vendor Charges| 50,000.00 | 0 | 0 | 50,000.00
请帮忙。
我尝试了下面的宏函数,但无法正确退出for each循环的范围
Sub LoopInsert()
Dim tgt, final, rng, val, cell, cell2, cell3 As Range
Worksheets("Sheet1").Activate
Set rng = ActiveSheet.Range("A2", ActiveSheet.Range("A2").End(xlDown))
Set val = ActiveSheet.Range("B2", ActiveSheet.Range("B2").End(xlDown))
Set tgt = ActiveSheet.Range("C2", ActiveSheet.Range("C2").End(xlDown))
For Each cell In rng
For Each cell2 In val
If cell.Value = "Daily Charges" Then
Exit For
For Each cell3 In tgt
cell3.Value = cell2.Value
Exit For
Next
Else
For Each cell3 In tgt
cell3.Value = 0
Exit For
Next
End If
Next
Next
End Sub
这不需要宏。 你可以用公式来做这件事
=IF($C$1 = A2, B2, 0)
=IF($D$1 = A2, B2, 0)
=IF($E$1=A2, B2, 0)
将它们粘贴到C2、D2、E2中,然后复制
其余单元格的公式
或者正如YowE3K指出的,您可以使用
=单元格C2中的IF(C$1=$A2,$B2,0),并复制到所有其他单元格(即C2:E4)
坚持使用VBA可以简化代码并减少运行时间
Option Explicit
Sub LoopInsert()
Dim catColumnRng As Range, catRowRng As Range, colRng As Range, cell As Range
With Worksheets("Sheet1")
Set catColumnRng = .Range("A2", .Cells(.Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeConstants, xlTextValues) '<--| store all "Category" not empty cells in column A from row 2 downwards
Set catRowRng = .Range("C1", .Cells(1, .Columns.Count).End(xlToLeft)).SpecialCells(xlCellTypeConstants, xlTextValues) '<--| store all "Category" not empty cells in row 1 from column 3 rightwards
For Each cell In catColumnRng '<--| loop through column A "Category" cells
Set colRng = catRowRng.Find(what:=cell.Value, LookIn:=xlValues, lookat:=xlWhole) '<--| try finding corresponding text in row 1 "Category" cells
If Not colRng Is Nothing Then .Cells(cell.Row, colRng.Column).Value = cell.Offset(, 1) '<--| if found then place the value
Next cell
End With
End Sub
选项显式
子循环插入()
Dim catRowRng As Range、catRowRng As Range、colRng As Range、cell As Range
带工作表(“表1”)
设置catColumnRng=.Range(“A2”,.Cells(.Rows.Count,1).End(xlUp)).SpecialCells(xlCellTypeConstants,xlTextValues)'