In your case it would be ".Value" and not ".Range" of course, since you wish to refer to the actual value.
Main Topics
Browse All TopicsHi
I have the following code that checks a range on a spreadsheet and copies the value into a specific cell in another range. This works fine except if I run it more than once. What I need to do is first set the value of O to "" for each cell in the range Results. The range PTable is also very long as it can have a variable number of values in the range. I need the function to stop when it reaches a value on Ptable that's empty ie "".
Sub EnterRisk()
Dim O As Range
For Each cell In Range("PTable")
P = cell
I = cell.Offset(0, 1)
R = cell.Offset(0, -2)
Set O = WorksheetFunction.Index(Ra
If O = "" Then
O = R
Else
O = O & "," & R
End If
Next cell
End Sub
I've managed to set the value of each cell in the Range results to "" by using the following code - but it looks clunky
Sub EnterRisk()
Dim O As Range
For Each cell In Range("PTable3")
P = cell
I = cell.Offset(0, 1)
R = cell.Offset(0, -2)
Set O = WorksheetFunction.Index(Ra
O = ""
Next cell
For Each cell In Range("PTable3")
P = cell
I = cell.Offset(0, 1)
R = cell.Offset(0, -2)
Set O = WorksheetFunction.Index(Ra
If O = "" Then
O = R
Else
O = O & "," & R
End If
Next cell
End Sub
However I'm not having any luck with the syntax to check the value of of p and only execute the code to assign value to O if P is <>"". I also need to run this code across two different worksheets - at the moment it runs off the same worksheet. So - the ranges "Range" and "PTable" will be on separate worksheets.
Can someone help? I hope I've explained that clearly enough.
Thanks
Deb
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thanks - but please assume I'm an idiot with vba - I've already had help with this here
http://www.experts-exchang
Try this. Let me know if it works alright. This will clear everything out of range "Results" at first though, so make sure you do want it all cleared.
Sub EnterRisk()
Dim O As Range
Dim cell as Range
Dim P as Long
Dim I as Long
Dim R as Range
Range("Results").ClearCont
For Each cell In Range("PTable3")
P = cell.Value
I = cell.Offset(0, 1).Value
R = cell.Offset(0, -2).Range
Set O = WorksheetFunction.Index(Ra
If O.Value = "" Then
O.Value = R.Value
Else
O.Value = O.Value & "," & R.Value
End If
Next cell
End Sub
Oops! If at first you don't succeed! Try this.
Sub EnterRisk()
Dim O As Range
Dim cell as Range
Dim P as Long
Dim I as Long
Dim R as Range
Range("Results").ClearCont
For Each cell In Range("PTable3")
P = cell.Value
I = cell.Offset(0, 1).Value
R = cell.Offset(0, -2)
Set O = WorksheetFunction.Index(Ra
If O.Value = "" Then
O.Value = R.Value
Else
O.Value = O.Value & "," & R.Value
End If
Next cell
End Sub
Hi
No it's still not playing - Just returns " Object Variable or With Block variable not set". The code I posted works fine, it just returns a 400 error if not every cell in the range PTable3 contains a value so really I just need to fix this. As I also said - I need the code to reference the range Ptable3 on Worksheet2 when the range "Results" is on worksheet1
Thanks
This should work at last. You'll need to set "Results" to just reference the coloured cells and not include the headings, otherwise you'll lose the headings, and also it won't put it in the right place. The naming of the range should already take care of the fact it's on another worksheet. This all assumes too that range "PTable3" is the single probability column.
Sub EnterRisk()
Dim O As Range
Dim cell As Range
Dim P As Range
Dim I As Range
Dim R As Range
Range("Results").ClearCont
For Each cell In Range("PTable3")
Set P = cell
Set I = cell.Offset(0, 1)
Set R = cell.Offset(0, -2)
If P.Value <> "" and I.Value <> "" Then
Set O = Range("Results").Offset(6 - I.Value, P.Value - 1)
If O.Value = "" Then
O.Value = R.Value
Else
O.Value = O.Value & "," & R.Value
End If
End If
Next cell
End Sub
Tested this one to death, if this doesn't work I'm shooting myself.
Sub EnterRisk()
Dim O As Range
Dim cell As Range
Dim P As Range
Dim I As Range
Dim R As Range
Range("Results").ClearCont
For Each cell In Range("PTable3")
Set P = cell
Set I = cell.Offset(0, 1)
Set R = cell.Offset(0, -2)
If P.Value <> "" And I.Value <> "" Then
Set O = Range("Results").Cells(1, 1).Offset(6 - I.Value, P.Value - 1)
If O.Value = "" Then
O.Value = R.Value
Else
O.Value = O.Value & "," & R.Value
End If
End If
Next cell
End Sub
To get it run when the workbook is opened, put the below code into the "ThisWorkbook" section of you VBA project, not in the Modules.
Private Sub Workbook_Open()
Dim O As Range
Dim cell As Range
Dim P As Range
Dim I As Range
Dim R As Range
Range("Results").ClearCont
For Each cell In Range("PTable3")
Set P = cell
Set I = cell.Offset(0, 1)
Set R = cell.Offset(0, -2)
If P.Value <> "" And I.Value <> "" Then
Set O = Range("Results").Cells(1, 1).Offset(6 - I.Value, P.Value - 1)
If O.Value = "" Then
O.Value = R.Value
Else
O.Value = O.Value & "," & R.Value
End If
End If
Next cell
End Sub
Thanks - this is running off a query to an access database that automatically updates the data when the sheet loads. When the macro runs when the workbook opens it's just returning a set of blank cells - the data refresh and the running of this code are out of sink maybe - which as I've spent ages trying to get this sorted and it's so close - is really annoying. I know it's pushing the remit of this question slightly - butI'm not far off shooting myself either - this is being a massive pain. The trouble is that two sheets need to open in separate windows - like a split view as the format varies on these sheets - otherwise I'd run it all on one sheet. Any ideas before I close this out?
Thanks again
Deb
One on the worksheet itself.
Goto View - Toolbars - Control Toolbox within Excel on the sheet you want the button. Click on Command Button and then click where you want the button to be. Right click on the button and select Properties. Then change the "Caption" property to whatever you want the button to say on it (You can also rename the button if you wish). Double click the button and between the sub declaration and "End Sub" put:
EnterRisk
You'll need to make sure the code is back in the Module and not in the ThisWorkbook section now, make sure it's the one headed "Sub EnterRisk()"
Back in Excel click the "Exit Design Mode" on the Control Toolbox bar and save, and you should be good to go!
Business Accounts
Answer for Membership
by: ManAlivePosted on 2006-09-18 at 04:46:30ID: 17542513
As a starting point I'd define each variable in the way variable "O" has been, implicit definition can only lead to problems. In the same way, rather than specifying "Cell.offset(0,1)", I'd be far more inclined to have this as "Cell.Offset(0,1).Range", again for clarity and to avoid confusion.