2014-04-28 122 views
1

我有问题,使用VBA我的项目。我有这个列有成千上万的行。对于VBA循环。宏Excel程序

而且这些列有像0.05, 1.3等价值观..它也比标志<更大像<0.05, <0.02这是我真正的问题复杂化值。我想用If ElseLooping此解决方案。但我不知道如何。我是VBA宏观excel的初学者。

我想从这些行发生的事情是,如果宏检测到一个值有<它会自动除以2,所以我不会有复杂的值来获得这些行的最大值和最小值。

EDIT1:上传图片

sample image

我希望你把我这件事一点。对不起我的英语不好。谢谢你的帮助。

+0

首先在VBA使用'如果则IsNumeric(范围)Then'陷阱例 “<” 在细胞中值,然后'Range.Value = CDbl(Replace(Range.Value,“<”,“”))/ 2',假设覆盖该值。 – PatricK

回答

1

下面是使用Autofilter更快的方法。使用Autofilter将确保你不会通过每一个细胞都循环。

样本屏幕截图

enter image description here

代码

Sub Sample() 
    Dim ws As Worksheet 
    Dim rng As Range, aCell As Range, fltrdRng As Range 
    Dim LRow As Long 

    '~~> Change this to the relevant sheet 
    Set ws = ThisWorkbook.Sheets("Sheet1") 

    With ws 
     '~~> Remove any filters 
     .AutoFilterMode = False 

     '~~> Get the last row of Col H where your data is 
     LRow = .Range("H" & .Rows.Count).End(xlUp).Row 

     '~~> Set your range 
     Set rng = .Range("H1:H" & LRow) 

     With rng 
      .AutoFilter Field:=1, Criteria1:="=<*" 
      Set fltrdRng = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow 
     End With 

     If Not fltrdRng Is Nothing Then 
      For Each aCell In fltrdRng.Cells 
       If aCell.Row > LRow Then Exit For 

       '~~> 8 is for Col H 
       If aCell.Column = 8 Then 
        aCell.Value = Replace(aCell.Value, "<", "") 
        aCell.Value = Val(Trim(aCell.Value))/2 
       End If 
      Next aCell 
     End If 
     '~~> Remove any filters 
     .AutoFilterMode = False 
    End With 
End Sub 
+0

哇,这是快速的答案,那确实有效。谢谢你@Siddhart Rout。它有很多帮助。也感谢那些回答我的问题的人。我很喜欢这个网站和周围的人这样的天才人物。上帝保佑。 – user3400194

1

试试这个,然后:

Sub RemoveComplicatedValues() 
Dim rng As Range, cel As Range 
Dim x 

Set rng = Range("H2", Range("H" & Rows.Count).End(xlUp).Address) 

For Each cel In rng 
    If InStr(1, cel, "<") <> 0 Then 
     x = CSng(Split(cel, "<")(UBound(Split(cel, "<")))) 
     cel = x/2 
    End If 
Next  
End Sub 

您也可以完全限定您WorkbookSheet如果你想。
希望这有助于。