ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Update Range from User Form (https://www.excelbanter.com/excel-programming/357768-update-range-user-form.html)

fybar[_2_]

Update Range from User Form
 
It seems that this shouldn't be too hard but I cannot figure it out. I
select a range of cells that may have values or may be blank. I want to
cycle through the cells and give the user a chance to accept the current
value or enter their own. I have a user form with the following code:

Private Sub UserForm_Initialize()
Dim Puzzle As Range
Dim temp As Range

Set Puzzle = Sheets("Puzzle").Range("A1:P16")

For Each temp In Puzzle
'Highlight the cell to show the user where they are
temp.Cells.Interior.ColorIndex = 6
With UserForm2
.StartUpPosition = 0
.Top = 400
.Left = 400
.TextBox1.Text = temp.Value
.TextBox1.SelStart = 0
.TextBox1.SelLength = Len(.TextBox1.Text)
.Show
End With
Next temp
End Sub

Private Sub CommandButton1_Click()
'What to put in here?
End Sub

I had the Range setting code and For loop in the Sheet1 section and I got
the loop to work. It would bring up a form and show the value and I would
cycle through the forms by closing them with the X. Now, I moved this to
the userform as I thought it would need to be there to get a value from the
user to update the cell. How do I cycle through the range and accept or
change values by hitting the Enter button? Also, I want to just use one
form to do this, it seems more sensible.

Is there a better or more acceptable way to do this?

Thanks,

fybar

fybar[_2_]

Update Range from User Form
 
On Sat, 01 Apr 2006 21:50:21 GMT, fybar wrote:

I had the Range setting code and For loop in the Sheet1 section and I got
the loop to work. It would bring up a form and show the value and I would
cycle through the forms by closing them with the X. Now, I moved this to
the userform as I thought it would need to be there to get a value from the
user to update the cell. How do I cycle through the range and accept or
change values by hitting the Enter button? Also, I want to just use one
form to do this, it seems more sensible.

Is there a better or more acceptable way to do this?


Ok, following up on my own post. I figured out how to do this. In the
Sheet1 code area I have this for a button on the sheet:
Private Sub cmdClearPuzzle_Click()
UserForm2.Show
Dim Puzzle As Range
Dim temp As Range

Set Puzzle = Sheets("Puzzle").Range("A1:P16")
For Each temp In Puzzle
temp.Cells.Interior.ColorIndex = 6
temp.Cells.Activate
With UserForm2
.StartUpPosition = 0
.Top = 400
.Left = 400
.TextBox1.Text = temp.Cells.Value
.TextBox1.SelStart = 0
.TextBox1.SelLength = Len(.TextBox1.Text)
.Show
End With
Next temp

End Sub

And in the userform code area:

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Then
ActiveCell.Value = Me.TextBox1.Value
ActiveCell.Interior.ColorIndex = 0
Me.Hide
End If

End Sub

This works exactly as I want it to. Does this seem like the best way to
accomplish this? If anyone has any suggestion I would welcome them.

Thanks,

fybar


All times are GMT +1. The time now is 02:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com