Logic operations in VBA

gdgool

New Member
Joined
Aug 6, 2016
Messages
17
hi, I'm new to vba and im trying to do a OR,NOT, AND operations on a selected range per each row, the way I want to do it get user input to select the columns first through input box then use those columns to do the operations, all the coloumns are consisting of only 0`s and 1's. if the user selects keep column 1-5 this part i'm able to do then select this range and do a OR,NOT, AND operation in each column. So if row one has a 1 is any of the columns then result = 1 and keep going till the end range
I've tried doing this but I'm counting 1 everything it occurs so if theres three 1's in row one its counting as 3 i just want output to be 1. That being said I'm also stopped by a annoying error I cant fix
The next is a NOT operation so look at each column in the selected range per row if all values a 0 increment by 1
then lastly AND which is simply is all columns in the row is 1 count as 1 keep incrementing till end of the range. If im able to do one i can do the others heres what i have so far:

Code:
[/FONT][FONT=arial]Sub inBox()
      
    Dim colrow  As String
    'the below will take the users input
          colrow = Application.InputBox("columns needed seperate with "," e.g: ""1,2,3""", Type:=2)        operations colrow 


End Sub
[/FONT]
[FONT=arial]
[/FONT]
[FONT=arial]

Sub operations (colrow  As String)


    Dim stRng As Range, endRng As Range, newRng As Range, a As Range
   Dim d As Variant
    Dim nxt As String
    Dim myRange As Range
    Dim b As Range
    Dim cell As Range
     
    
 Set stRng = Range("A2").End(xlToRight) '<-- firstcell
    Set endRng = Cells(2, Columns.Count).End(xlToLeft) '<-- lastcell
    Set newRng = Range(stRng, endRng) '<--| IDs range
    For Each d In Split(Replace(colrow, " ", ""), ",") '<--| loop through 
        Set a = newRng.Find(what:=d, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not d Is Nothing Then nxt = d.Address(False) & ","  '<--| if the current input is found then update nxt addresses list
    Next d
    nxt = nxt 
    
    Set myRange = ActiveSheet.Range(nxt).EntireC<wbr>olumn '********
    i = 0
    For Each b In myRange.Rows 'go into each row of selected range
    r = 0 'initalise result
     For Each cell In b.Cells ' go into each cell
     if   cell.Offset(i).Value = result  then
     r = r +1
     Next cell

     i = i + 1
 
Next
  Cells(1, 10) = r ' output result 
End Sub


[/FONT]
[FONT=arial]




I've only tried to do the NOT operation but at a loss im getting an error also where i put the *** it says range not declared and when i look into to it it says myRange is empty. Maybe I'm not declaring my range correctly. Any help will be great thank you
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This line:

Rich (BB code):
        If Not d Is Nothing Then nxt = d.Address(False) & ","

should probably be

Rich (BB code):
        If Not a Is Nothing Then nxt = a.Address(False) & ","
 
Upvote 0
This line:

Rich (BB code):
        If Not d Is Nothing Then nxt = d.Address(False) & ","

should probably be

Rich (BB code):
        If Not a Is Nothing Then nxt = a.Address(False) & ","


yes your correct typo on my part it was 'if Not a' apologies i typed it wrong, the problem still stands to be the same and im still not sure how to do the logic analysis any help would be appreciated thank you
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top