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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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