旋转值的宏

时间:2016-03-30 19:22:47

标签: excel vba excel-vba macros

我目前正在开发一个电子表格,其中包含多列,每列中的行10,11和12表示您是否相应地编号为1,2或3。更具体地说,见下图。

http://i.imgur.com/QezrzqW.png

我需要构建一个在每次使用时移动值的宏,因此A列中的单元格将更改为A10 = 1,A11和A12 =空白,B列更改为B12 = 3,B10和B12 =空白,C列到C11 = 2,C10和C12 =空白等。宏将一直运行,直到到达AC列。

这有可能吗?我必须每天更改这些值,以便对多列进行繁琐的处理。

亲切的问候, 托尔

2 个答案:

答案 0 :(得分:1)

关于A列如何填写的问题不清楚,但是这样的事情可以解决问题:

Public Sub MoveRight()
    Dim sh As Worksheet
    Dim area As Range

    Set sh = ActiveSheet
    With sh
        Set area = .Range(.Cells(10, 1), .Cells(12, 29))
    End With

    Dim r As Long, c As Long

    With area
        For c = .Columns.Count To 2 Step -1
            For r = 1 To .Rows.Count
                .Cells(r, c).Value = .Cells(r, c - 1).Value
            Next r
        Next c
        For r = 1 To .Rows.Count
            If .Cells(r, 4).Value = r Then
                .Cells(r, 1).Value = r
            Else
                .Cells(r, 1).Value = vbNullString
            End If
        Next r
    End With
End Sub

答案 1 :(得分:0)

不是很优雅,但这就是你所描述的......

Option Explicit
Option Base 1

Sub Simple()
Dim iloop As Integer

For iloop = 1 To 29
    If Sheets("Sheet1").Cells(10, iloop) = 1 Then
        Sheets("Sheet1").Cells(10, iloop) = ""
        Sheets("Sheet1").Cells(11, iloop) = 2
    Else
        If Sheets("Sheet1").Cells(11, iloop) = 2 Then
            Sheets("Sheet1").Cells(11, iloop) = ""
            Sheets("Sheet1").Cells(12, iloop) = 3
        Else
            If Sheets("Sheet1").Cells(12, iloop) = 3 Then
                Sheets("Sheet1").Cells(12, iloop) = ""
                Sheets("Sheet1").Cells(10, iloop) = 1
            End If
        End If
    End If

Next iloop
End Sub