1

我想复制以下四个公式并将其粘贴到相邻的四列中,列引用每次更改四。我的意思是复制 F4:I4 并粘贴到 J4:M4,N4:Q4 ......将“F”转换为“J”,然后是“N”,然后是“Q”,依此类推,直到结束工作表中的列。

=IF(AND(F2>=$C$4,F2<=$D$4),TRUE, FALSE)
=IF(AND((F2+6)>=$C$4,(F2+6)<=$D$4),TRUE,FALSE)
=IF(AND((F2+12)>=$C$4,(F2+12)<=$D$4),TRUE,FALSE)
=IF(AND((F2+18)>=$C$4,(F2+18)<=$D$4),TRUE,FALSE)    

我是否能够以某种方式循环遍历每一列,并在第四列之后将四添加到单元格引用的数值?所以不是 F2 和 J2 我有 Col_ID,Col_ID+4 ...不知道如何在 VBA 中编写它。任何帮助将不胜感激。

我用它来合并上面的每四个单元格以制作“标签”,我想我可以重复使用它,但不确定如何。

Dim Rng As Range
Dim ws As Worksheet
Dim R1 As Long, C1 As Long
Dim R2 As Long, C2 As Long
Dim lastCol As Long

Set ws = ThisWorkbook.Sheets("Dashboard")

R1 = 3: C1 = 6
R2 = 3: C2 = C1 + 3

lastCol = 1

While lastCol < 256
    With ws
        Set Rng = .Range(.Cells(R1, C1), .Cells(R2, C2))
        Application.DisplayAlerts = False
        Rng.Merge
        Application.DisplayAlerts = True
        C1 = C2 + 1
        C2 = C1 + 3
        lastCol = lastCol + 1
    End With
Wend
4

1 回答 1

0

这会将源范围复制到您指定的四列组中NumberOfCopies。你没有说你从哪个范围复制,所以我假设 G3:J3:

Sub CopyCols()
Dim rngSource As Excel.Range
Dim rngTarget As Excel.Range
Dim NumberOfCopies As Long

NumberOfCopies = 12
With ActiveSheet
    Set rngSource = .Range("G3:J3")
    Set rngTarget = .Range("K3").Resize(rngSource.Rows.Count, NumberOfCopies * 4)
    rngSource.Copy Destination:=rngTarget
End With
End Sub
于 2013-10-21T20:19:05.390 回答