need some help in VBA coding

SKV

Active Member
Joined
Jan 7, 2009
Messages
257
Hi, I am very novice in excel VBA and trying to put a code where based on values in various cells (say col A, col B, Col E) for each row, I want to use a logic and put a value in another cell say col X. High level Example

If Col A val = A, Then "1" Elseif val = B then "h" &"-"& if col B <0, then "Neg" ELSE if B=0 then "Zer" Els "Pos" &"-"& if Col E val = "Male" then "M" Else "F"

so the output in col X = 1-Zer-M

I am struggling to refer to the cells properly as I want to make a resuable code where in future I can change col A , B etc and also change the logic and resuse the code. So please advise as how to begin or if you can put a simple code then I can try to build upon that.

Thanks in advance for your help and guidance.

Regards
Sachin
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You can refer to cells with things like:

sheets(1).range("A"&x).value
sheets(1).cells(row,column).value
 
Last edited:
Upvote 0
Code:
Sub Test

Dim Aval as string, Bval as string, Eval as string, x as long

For x = 1 to 1000

Aval=Range("A"&x).value
Bval=Range("B"&x).value
Eval=Range("E"&x).value


Range("X"&x).value  = Iif(Aval="A","1","B") & " - " & iif (Bval<0,"Neg","Pos")  & " - " & iif(Eval="Male","M"<"F")

next x

end sub
 
Upvote 0
Code:
Sub m()
Dim rng As Range, cel As Range, str$
Set rng = Range([A2], Cells(Rows.Count, "A").End(xlUp))
For Each cel In rng
    Select Case cel
        Case "A": str = "1-"
        Case "B": str = "h-"
        Case Else: str = "?-"
    End Select
    Select Case cel(1, 2)
        Case Is < 0: str = str & "Neg-"
        Case Is = 0: str = str & "Zer-"
        Case Else: str = str & "Pos-"
    End Select
    Select Case cel(1, 5)
        Case "Male": str = str & "M"
        Case Else: str = str & "F"
    End Select
    cel(1, 24) = str
    str = ""
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

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