如何将Column设置为variables

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

当前代码

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 

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

 Sub dural() Dim bee As String, kolumn As Range bee = "B" Set kolumn = Range(bee & "1").EntireColumn End Sub 

遵循约翰·科尔曼的build议,我将公式转换为R1C1表示法。 使用.Columns(ColNum).Rows("2:146")允许我们为列设置一个variables,并保持行固定。

你的信箱对我没有意义。 您正在格式化行2:146但检查行1:300的格式。 我想也许你正在使用2个不同的工作表? 无论如何,谁想要点击300个消息框? 我也修改了。

在这里输入图像说明

 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 

一列是一个范围,所以你可以设置一个等于它的范围variables。

下面的代码片段可能会告诉你如何在你的情况下有用:

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

该代码成功设置B2:B146所有单元格等于1。