2014-04-28 119 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如果你想。
希望這有助於。