If Then statement help for visual basic - Excel

Wmbtby

New Member
Joined
Apr 27, 2016
Messages
1
Good afternoon. I am brand new at Visual Basic. I have written a few simple macros but now I have something that is a bit more challenging and I am having difficulty. The primary worksheet of my workbook - let's call it 'sheet A' consists of 21 columns and 250+ rows. It has a column where the data consists of 3 choices. Let's call the choices 'G', 'H' and 'W'. If the value is 'H' or 'W' the macro needs to go to sheet B and return two values to the last two columns. If the value is 'G' the macro needs to return two values from sheet C.

The choices are strings and the returned values are numbers/dollar amounts. I attempted to code this but realized I am in too deep for my current skill level. I am thinking an If Then statement perhaps combined with a loop but I don't really know where to start. Any assistance provided is greatly appreciated.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
The following might get you started...

Code:
Sub test()
Application.ScreenUpdating = False
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim LastRow As Long
Dim r As Range

Set ws1 = Sheets("SheetA")
Set ws2 = Sheets("SheetB")
Set ws3 = Sheets("SheetC")
LastRow = ws1.Cells(Rows.Count, "[COLOR=#0000ff]A[/COLOR]").End(xlUp).Row

For Each r In ws1.Range("[COLOR=#0000ff]A2:A[/COLOR]" & LastRow)
    If r = "H" Or r = "W" Then
        r.Offset(0, 21) = ws2.Range("[COLOR=#0000ff]A1[/COLOR]")
        r.Offset(0, 22) = ws2.Range("[COLOR=#0000ff]A2[/COLOR]")
    ElseIf r = "G" Then
        r.Offset(0, 21) = ws3.Range("[COLOR=#0000ff]A1[/COLOR]")
        r.Offset(0, 22) = ws3.Range("[COLOR=#0000ff]A2[/COLOR]")
    End If
Next r

MsgBox "Done"
Application.ScreenUpdating = True
End Sub
The code is written with the expectation that Column A of SheetA contains the G, H and W criteria; you'll need to adjust the column as necessary.
There was no indication where the data to be returned is located on SheetB or SheetC, so it's assumed to be Range("A1") and Range("A2"); again, you'll need to adjust as necessary.

Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

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