Tuesday, April 5, 2016

My stupid version of Sumifs(without the greater than or less than feature) ,vba macro

Sub test1()



Dim colnum2 As Integer

Dim col2, col3 As String

Dim ws As Worksheet

Set ws = ActiveSheet

Dim colname As String
Dim i As String
Dim j As Integer

Dim sum As Integer
Dim k As Integer
Dim colnum As Integer
Dim rownum As Integer
rownum = 0
Dim truth As Integer
truth = 0
Dim signal As Integer
signal = 0

k = 0

Dim criterias() As String
Dim trutharr() As Integer


sum = 0


j = 0


 i = ActiveCell.Value
' MsgBox (i)
  
   While i <> ""
 j = j + 1

  ActiveCell.Offset(0, 1).Select
  
    i = ActiveCell.Value
    
 ' MsgBox (i)
  
  
  
  
  
  Wend
  
  
  
 ' MsgBox ("the number of columns are " & j)
  colnum = j
  
  
  ReDim criterias(1 To j) As String
  ReDim trutharr(1 To j) As Integer
  
For k = 1 To j - 1

criterias(k) = InputBox("Give me some input")


Next k

  
  
  
  
  

  
  
  For k = 1 To j
  
  ActiveCell.Offset(0, -1).Select
  
  Next k
' MsgBox (ActiveCell.Address)
 i = ActiveCell.Value
 j = 0

  While i <> ""
 j = j + 1

  ActiveCell.Offset(1, 0).Select
  
    i = ActiveCell.Value
    
 ' MsgBox (i)
  
  
  
  
  
  Wend

' MsgBox ("The selection has " & j & " rows")

 rownum = j

 MsgBox (ActiveCell.Address)

 For k = 1 To rownum
  
  ActiveCell.Offset(-1, 0).Select
  
  Next k
'MsgBox (ActiveCell.Address)



 For j = 1 To rownum

 For k = 1 To colnum




 ActiveCell.Offset(0, 1).Select
 If IsNumeric(criterias(k)) And IsNumeric(ActiveCell.Value) Then
' MsgBox ("criterias(k): " & criterias(k) & " ActiveCell.Value:" & ActiveCell.Value)

 If CInt(ActiveCell.Value) = CInt(criterias(k)) Then
' MsgBox ("criterias(k): " & criterias(k) & " ActiveCell.Value:" & ActiveCell.Value & " are matching")
 truth = 1
 Else
 truth = 0
 End If
 Else
 If ActiveCell.Value = criterias(k) Then
'MsgBox ("criterias(k): " & criterias(k) & " ActiveCell.Value:" & ActiveCell.Value & " are matching")
 truth = 1
 Else
 truth = 0
 End If

 End If
 trutharr(k) = truth


 Next k

 For k = 1 To colnum
 ActiveCell.Offset(0, -1).Select

 Next k
 'MsgBox (ActiveCell.Address)
 For k = 1 To UBound(trutharr)



 If trutharr(k) = 0 Then
 signal = 1

 Exit For


 End If
 Next k
 If signal = 0 Then
 sum = sum + ActiveCell.Value
 End If
 signal = 0

 truth = 0

 ActiveCell.Offset(1, 0).Select




 Next j

 For k = 1 To rownum

 ActiveCell.Offset(-1, 0).Select


 Next k

 MsgBox ("the result is " & sum)

  colname = ActiveCell.Address
  col2 = Replace(colname, "$", "")
 For j = 1 To Len(col2)
        If Not IsNumeric(Mid(col2, j, 1)) Then
            col3 = col3 & Mid(col2, j, 1)
        End If
    Next j
  colnum2 = (Range(col3 & 1).Column)
  
  For Each cell In ws.Columns(colnum2).Cells
  
  
  If IsEmpty(cell) = True Then cell.Select: Exit For
Next cell
  ActiveCell.Value = "hello the result is " & sum
  
  ActiveCell.EntireColumn.AutoFit
End Sub

No comments:

Post a Comment