如何将列设置为变量

时间:2016-12-09 03:45:48

标签: excel vba

是否可以将列设置为变量?像setcolumn = B?因为我有一个vba代码可以帮助我创建条件格式,但是,我总是需要更改列,但范围保持不变。基于我下面的代码,我需要将I2:I146更改为J2:J146等等...每天...因此,我想知道是否可以将列设置为变量。

当前代码

Sub Button6_Click()
Dim ws As Worksheet
Dim i As Integer
Set ws = Sheets("COMPARISON")
i = 1

With Range("I2:I146").FormatConditions.Add( _
    Type:=xlExpression, _
    Formula1:="=((($I2-$E2)/$E2)*100) > 20")
    .Interior.Color = RGB(255, 0, 0)

End With

With Range("I2:I146").FormatConditions.Add( _
    Type:=xlCellValue, _
    Operator:=xlEqual, _
    Formula1:="0")
    .Interior.Color = RGB(0, 0, 0)
    .Font.Color = RGB(255, 255, 255)
End With

With Range("I2:I146").FormatConditions.Add( _
    Type:=xlExpression, _
    Formula1:="=AND($I2<$E2, $I2<>0)")
    .Interior.Color = RGB(255, 255, 0)
End With

Do Until i = 300
   If ws.Range("I" & i).DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
       msg = "I" & i & " -" & " Data has INCREASED"
       MsgBox msg
   ElseIf ws.Range("I" & i).DisplayFormat.Interior.Color = RGB(0, 0, 0) Then
       msg1 = "I" & i & " -" & " Data is ZERO"
       MsgBox msg1
   ElseIf ws.Range("I" & i).DisplayFormat.Interior.Color = RGB(255, 255, 0)       Then
   msg2 = "I" & i & " -" & " Data is DECREASED"
   MsgBox msg2
End If
i = i + 1
Loop

End Sub

3 个答案:

答案 0 :(得分:1)

这里我们从一个String开始,然后创建一个Range:

Sub dural()
    Dim bee As String, kolumn As Range

    bee = "B"
    Set kolumn = Range(bee & "1").EntireColumn
End Sub

答案 1 :(得分:1)

根据John Coleman的建议,我将公式转换为R1C1符号。使用.Columns(ColNum).Rows("2:146")允许我们为列设置变量并保持行固定。

你的留言箱对我没用。您要格式化行2:146,但要检查行1:300中的格式。我想也许你正在使用2种不同的工作表?在任何情况下,谁想要点击300个消息框?我也修改了它。

enter image description here

代码

Sub Button6_Click()
    Dim ColNum As Long
    'Assuming there is a header row and you want to format the last Column
    'ColNum = Sheets("COMPARISON").Cells(1, 5).End(xlToRight).Column

    ColNum = Sheets("COMPARISON").Cells(1, Columns.Count).End(xlToLeft).Column

    AddFormatConditions ColNum
    CreateMessage ColNum
End Sub

Sub AddFormatConditions(ColNum As Long)
    With Sheets("COMPARISON").Columns(ColNum).Rows("2:146")
        .FormatConditions.Delete
        With .FormatConditions.Add( _
             Type:=xlExpression, _
             Formula1:="=(((RC-RC5)/RC5)*100) > 20")
            .Interior.Color = RGB(255, 0, 0)
        End With

        With .FormatConditions.Add( _
             Type:=xlCellValue, _
             Operator:=xlEqual, _
             Formula1:="0")
            .Interior.Color = RGB(0, 0, 0)
            .Font.Color = RGB(255, 255, 255)
        End With

        With .FormatConditions.Add( _
             Type:=xlExpression, _
             Formula1:="=AND(RC<RC5, RC<>0)")
            .Interior.Color = RGB(255, 255, 0)
        End With
    End With
End Sub

Sub CreateMessage(ColNum As Long)
    Dim msg As String
    Dim i As Long
    Dim clip As Object
    Dim change As Double
    Set clip = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    With Sheets("COMPARISON")

        For i = 2 To 300
            change = .Cells(i, ColNum) - .Cells(i, "E")

            If change > 0 Then
                msg = msg & .Cells(i).Address(False, False) & " -  Data has INCREASED By " & change & vbCrLf
            ElseIf change = 0 Then
                msg = msg & .Cells(i).Address(False, False) & " - Data is ZERO" & vbCrLf
            ElseIf change < 0 Then
                msg = msg & .Cells(i).Address(False, False) & " - Data is DECREASED By " & change & vbCrLf
            End If
        Next
    End With
    clip.SetText msg
    clip.PutInClipBoard
    Shell "NOTEPAD.EXE", vbNormalFocus
    SendKeys "^v"
    SendKeys "^{Home}"
End Sub

答案 2 :(得分:0)

列是一个范围,因此您可以设置一个等于它的范围变量。

以下代码段可能会向您展示它在您的情况下如何有用:

Sub test()
    Dim Col As Range
    Set Col = Range("B:B")
    Range(Col(2), Col(146)).Value = 1
End Sub

代码成功地将B2:B146中的所有单元格设置为等于1。