do until loop with if statement that applies on different columns (vba)

ddesantis

New Member
Joined
Aug 30, 2014
Messages
14
Hi,
I'm scratching my head around what I think "a very simple piece" of code...but I'm a VBA beginner
I have to perform 2 tasks, and while I've completed the first one, the other is giving me a lot of troubles...

1st step: clear the content of a range of cells when I switch the xls validation list (yes,no). This works

Private Sub WorkSheet_Change(ByVal Target As Range)

If Target.Address = "$E$170" Then Range("$E$178:$E$187").ClearContents

If Target.Address = "$E$174" Then Range("$E$178:$E$187").ClearContents

2nd step:
- I have 2 ranges of value $D$178:$D$187 and $E$178:$E$187
- the range $D$178:$D$187 can assume the following values: Yes, No, Please select
- If any value in the range $D$178:$D$187 assume value "Yes" the corresponding value in the range $E$178:$E$187 has to assume value "Yes"
- If any value in the range $D$178:$D$187 assume value "No" the corresponding value in the range $E$178:$E$187 has to assume value "No"
- If any value in the range $D$178:$D$187 assume value "Please select" the corresponding value in the range $E$178:$E$187 will result in a validation list "Yes" or "No"

It's worth saying that the range $E$178:$E$187 has already a xls data validation list set (Yes,No)

Example
D180="Yes" then E180="Yes"

D183="No" then E183="No"

D185="Please select" then E185= validation list "Yes" or "No"

I tried as follows but xls crashed

Set Rng = Range("$E$178:$E$187")
Range("$D$178").Select
Do Until Selection.Value = "$D$188"
If Selection.Value = "Yes" Then
Rng = "Yes"
Else
Rng = "No"
End If
Loop

End Sub

my assumption is that in all other cases in the range $S$178:$E$187 (i.e. "Please select") the code doesn't apply and I can select "Yes" or "No" in the xls data validation list $E$178:$E$187

I managed to get the code above start working but I always got the following error
run time error '2147417848 (80010108)':
Method 'Value' of object 'Range' failed

even with
Select Case Range("$D$178").Text
Case "Yes"
Range("$E$178").Value = "Yes"
End Select
End Sub


Hope it's clear, I would appreciate any help you can provide!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hiya
Does this do the job?
Code:
Sub ddesantis()

    Dim i As Integer

    For i = 178 To 187
        If Range("D" & i).Value <> "Please select" Then
            Range("E" & i).Value = Range("D" & i).Value
        End If
    Next i
        

End Sub
 
Upvote 0
(y)
Thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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