Pressing a button to cycle through a range's values
I'm trying to create a button to automatically fill in cell B5
with information from another sheet's cells A1:A10
.
When the button is pressed I want B5
to contain the info from cell A1
. Then, when the button is pressed again, it should contain the info from A2
and so on.
microsoft-excel vba
add a comment |Â
I'm trying to create a button to automatically fill in cell B5
with information from another sheet's cells A1:A10
.
When the button is pressed I want B5
to contain the info from cell A1
. Then, when the button is pressed again, it should contain the info from A2
and so on.
microsoft-excel vba
2
So, what have you tried so far?
â music2myear
Jun 13 at 23:54
add a comment |Â
I'm trying to create a button to automatically fill in cell B5
with information from another sheet's cells A1:A10
.
When the button is pressed I want B5
to contain the info from cell A1
. Then, when the button is pressed again, it should contain the info from A2
and so on.
microsoft-excel vba
I'm trying to create a button to automatically fill in cell B5
with information from another sheet's cells A1:A10
.
When the button is pressed I want B5
to contain the info from cell A1
. Then, when the button is pressed again, it should contain the info from A2
and so on.
microsoft-excel vba
microsoft-excel vba
edited Dec 8 at 13:55
robinCTS
4,00941527
4,00941527
asked Jun 13 at 21:57
d123
261
261
2
So, what have you tried so far?
â music2myear
Jun 13 at 23:54
add a comment |Â
2
So, what have you tried so far?
â music2myear
Jun 13 at 23:54
2
2
So, what have you tried so far?
â music2myear
Jun 13 at 23:54
So, what have you tried so far?
â music2myear
Jun 13 at 23:54
add a comment |Â
3 Answers
3
active
oldest
votes
Here is short simple method.
You will need a counter to increment with each button press. You will need to put this somewhere on your spreadsheet, for this example it's below the button.
- First Decide where your counter will go, for this example it will be directly below the button.
- Insert your Button.
- Assign Macro to your Button, make sure you save it to the workbook.
- Paste below code into VBA Editor for your Button Click.
Code
Sub Button1_Click()
Dim CopySheet As Worksheet, PasteSheet As Worksheet
Dim xFrom As Integer, xTo As Integer, i As Integer
Dim pasteCell As String, cCell As String
'Sheets
Set CopySheet = Worksheets("Sheet2") 'Sheet you are copying from.
Set PasteSheet = Worksheets("Sheet1") 'Sheet you are pasting into.
'Rows, range of rows start from row rStart to rEnd
rStart = 1 'Start of Row you want to copy from.
rEnd = 10 'End of Row you want to copy from.
'Cells
pasteCell = "B5" 'Cell we will paste data from CopySheet.
'Counter will increments with each button press.
cCell = "E5" 'Change "E5" to reference cell on your spreadsheet.
i = Range(cCell).Value
Application.ScreenUpdating = False 'We disable Screen Updating to prevent interruption.
'Update Counter
i = i + 1
If (i > rEnd) Then
i = rStart
End If
Range(cCell).Value = i
'Copy/Paste Functions
CopySheet.Select
Range("A" & i).Select
Selection.Copy
PasteSheet.Select
Range(pasteCell).Select
ActiveSheet.Paste
Application.ScreenUpdating = True 'Enable Screen Updating at end of operation.
End Sub
The button will copy based on the counter number plus 1, so if the number is 0 on button press the macro will add get the 0 + 1 then begin the copy and paste functions.
add a comment |Â
There's no need to store a counter in a cell in the workbook. You can use a static variable instead.
Paste the following code into any non-class module:
'============================================================================================
' Module : <any non-class module>
' Version : 0.1.1
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Public Sub Next_Click()
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -1
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + 1) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Then assign it to your button.
The only issue with this code is that it doesn't remember which cell it was up to when you re-open the workbook, and restarts from the first cell. This can be worked around if desired.
Addendum:
If you also wish to have a "Previous" button to cycle backwards, it gets slightly trickier - you need a generalised Previous/Next subroutine with a parameter to determine the direction. Then, each button needs to be assigned to separate subroutines that call the main routine with the appropriate argument:
'============================================================================================
' Module : <any non-class module>
' Version : 0.2.0
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Private Sub Next_or_Previous( _
ByRef direction As Long _
)
Dim plngDirection As Long: plngDirection = direction
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -plngDirection
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + plngDirection + .Cells.Count) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Public Sub Previous_Click()
Next_or_Previous -1
End Sub
Public Sub Next_Click()
Next_or_Previous 1
End Sub
add a comment |Â
My approach is quit different to solve the issue.
I would like to suggest Worksheet Selection Change Event instead of Button Click , since it avoids the tedious Loop method task.
Worksheet Selection Change almost works like Button Click, since for 10 items 10 Clicks are required, as well as Selection Change Event also needs similar Clicks, and the best part is the order, it could be Ascending/Descending or even Random.
Below written code will copy cells from designated Data Range A1:A10
on Mouse Click, to destination Sheet's Cell.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
With Sheets("Sheet2")
.Select
.Range("B5").Value = Target.Value
End With
End If
End Sub
How the Macro works:
- Click any Cell between
A1:A10
at Source Sheet to Copy to Destination Sheet's CellB5
.
Note, Source Range A1:A10
, destination Sheet's name Sheet2
& Cell B5
are editable.
Write your concern to Down vote the post !!
â Rajesh S
Dec 9 at 9:15
1
Your post does not provide a solution to the OP. And responding to an old question that shows no effort is not good either.
â AJD
Dec 10 at 7:15
@AJD,, did you ever tried the Code I've posted, and find the Text on the TOP of my answer, I've mentioned that my approach is different and after that I've explained the reason behind opting the different approach!! Don't you think that a problem could have many Solutions and the way to solve also may different !!
â Rajesh S
Dec 10 at 7:35
1
The OP specifically asked for a button press which indicates a particular user exchange with the application. A selection change is a different action. Having said that, this is a very poor question with no attempts at a solution in the first place and shouldn't have been answered (the O is seeking a code writing service)..
â AJD
Dec 10 at 18:24
1
You are thinking like a coder, not a user. Yes, the underlying code is the same, but the user experience is different. The OP asks for a function that cycles on the click of a button, not a function that adapts to where the user clicks on the sheet.
â AJD
Dec 11 at 18:44
 |Â
show 1 more comment
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is short simple method.
You will need a counter to increment with each button press. You will need to put this somewhere on your spreadsheet, for this example it's below the button.
- First Decide where your counter will go, for this example it will be directly below the button.
- Insert your Button.
- Assign Macro to your Button, make sure you save it to the workbook.
- Paste below code into VBA Editor for your Button Click.
Code
Sub Button1_Click()
Dim CopySheet As Worksheet, PasteSheet As Worksheet
Dim xFrom As Integer, xTo As Integer, i As Integer
Dim pasteCell As String, cCell As String
'Sheets
Set CopySheet = Worksheets("Sheet2") 'Sheet you are copying from.
Set PasteSheet = Worksheets("Sheet1") 'Sheet you are pasting into.
'Rows, range of rows start from row rStart to rEnd
rStart = 1 'Start of Row you want to copy from.
rEnd = 10 'End of Row you want to copy from.
'Cells
pasteCell = "B5" 'Cell we will paste data from CopySheet.
'Counter will increments with each button press.
cCell = "E5" 'Change "E5" to reference cell on your spreadsheet.
i = Range(cCell).Value
Application.ScreenUpdating = False 'We disable Screen Updating to prevent interruption.
'Update Counter
i = i + 1
If (i > rEnd) Then
i = rStart
End If
Range(cCell).Value = i
'Copy/Paste Functions
CopySheet.Select
Range("A" & i).Select
Selection.Copy
PasteSheet.Select
Range(pasteCell).Select
ActiveSheet.Paste
Application.ScreenUpdating = True 'Enable Screen Updating at end of operation.
End Sub
The button will copy based on the counter number plus 1, so if the number is 0 on button press the macro will add get the 0 + 1 then begin the copy and paste functions.
add a comment |Â
Here is short simple method.
You will need a counter to increment with each button press. You will need to put this somewhere on your spreadsheet, for this example it's below the button.
- First Decide where your counter will go, for this example it will be directly below the button.
- Insert your Button.
- Assign Macro to your Button, make sure you save it to the workbook.
- Paste below code into VBA Editor for your Button Click.
Code
Sub Button1_Click()
Dim CopySheet As Worksheet, PasteSheet As Worksheet
Dim xFrom As Integer, xTo As Integer, i As Integer
Dim pasteCell As String, cCell As String
'Sheets
Set CopySheet = Worksheets("Sheet2") 'Sheet you are copying from.
Set PasteSheet = Worksheets("Sheet1") 'Sheet you are pasting into.
'Rows, range of rows start from row rStart to rEnd
rStart = 1 'Start of Row you want to copy from.
rEnd = 10 'End of Row you want to copy from.
'Cells
pasteCell = "B5" 'Cell we will paste data from CopySheet.
'Counter will increments with each button press.
cCell = "E5" 'Change "E5" to reference cell on your spreadsheet.
i = Range(cCell).Value
Application.ScreenUpdating = False 'We disable Screen Updating to prevent interruption.
'Update Counter
i = i + 1
If (i > rEnd) Then
i = rStart
End If
Range(cCell).Value = i
'Copy/Paste Functions
CopySheet.Select
Range("A" & i).Select
Selection.Copy
PasteSheet.Select
Range(pasteCell).Select
ActiveSheet.Paste
Application.ScreenUpdating = True 'Enable Screen Updating at end of operation.
End Sub
The button will copy based on the counter number plus 1, so if the number is 0 on button press the macro will add get the 0 + 1 then begin the copy and paste functions.
add a comment |Â
Here is short simple method.
You will need a counter to increment with each button press. You will need to put this somewhere on your spreadsheet, for this example it's below the button.
- First Decide where your counter will go, for this example it will be directly below the button.
- Insert your Button.
- Assign Macro to your Button, make sure you save it to the workbook.
- Paste below code into VBA Editor for your Button Click.
Code
Sub Button1_Click()
Dim CopySheet As Worksheet, PasteSheet As Worksheet
Dim xFrom As Integer, xTo As Integer, i As Integer
Dim pasteCell As String, cCell As String
'Sheets
Set CopySheet = Worksheets("Sheet2") 'Sheet you are copying from.
Set PasteSheet = Worksheets("Sheet1") 'Sheet you are pasting into.
'Rows, range of rows start from row rStart to rEnd
rStart = 1 'Start of Row you want to copy from.
rEnd = 10 'End of Row you want to copy from.
'Cells
pasteCell = "B5" 'Cell we will paste data from CopySheet.
'Counter will increments with each button press.
cCell = "E5" 'Change "E5" to reference cell on your spreadsheet.
i = Range(cCell).Value
Application.ScreenUpdating = False 'We disable Screen Updating to prevent interruption.
'Update Counter
i = i + 1
If (i > rEnd) Then
i = rStart
End If
Range(cCell).Value = i
'Copy/Paste Functions
CopySheet.Select
Range("A" & i).Select
Selection.Copy
PasteSheet.Select
Range(pasteCell).Select
ActiveSheet.Paste
Application.ScreenUpdating = True 'Enable Screen Updating at end of operation.
End Sub
The button will copy based on the counter number plus 1, so if the number is 0 on button press the macro will add get the 0 + 1 then begin the copy and paste functions.
Here is short simple method.
You will need a counter to increment with each button press. You will need to put this somewhere on your spreadsheet, for this example it's below the button.
- First Decide where your counter will go, for this example it will be directly below the button.
- Insert your Button.
- Assign Macro to your Button, make sure you save it to the workbook.
- Paste below code into VBA Editor for your Button Click.
Code
Sub Button1_Click()
Dim CopySheet As Worksheet, PasteSheet As Worksheet
Dim xFrom As Integer, xTo As Integer, i As Integer
Dim pasteCell As String, cCell As String
'Sheets
Set CopySheet = Worksheets("Sheet2") 'Sheet you are copying from.
Set PasteSheet = Worksheets("Sheet1") 'Sheet you are pasting into.
'Rows, range of rows start from row rStart to rEnd
rStart = 1 'Start of Row you want to copy from.
rEnd = 10 'End of Row you want to copy from.
'Cells
pasteCell = "B5" 'Cell we will paste data from CopySheet.
'Counter will increments with each button press.
cCell = "E5" 'Change "E5" to reference cell on your spreadsheet.
i = Range(cCell).Value
Application.ScreenUpdating = False 'We disable Screen Updating to prevent interruption.
'Update Counter
i = i + 1
If (i > rEnd) Then
i = rStart
End If
Range(cCell).Value = i
'Copy/Paste Functions
CopySheet.Select
Range("A" & i).Select
Selection.Copy
PasteSheet.Select
Range(pasteCell).Select
ActiveSheet.Paste
Application.ScreenUpdating = True 'Enable Screen Updating at end of operation.
End Sub
The button will copy based on the counter number plus 1, so if the number is 0 on button press the macro will add get the 0 + 1 then begin the copy and paste functions.
answered Jun 14 at 0:09
angelofdev
64417
64417
add a comment |Â
add a comment |Â
There's no need to store a counter in a cell in the workbook. You can use a static variable instead.
Paste the following code into any non-class module:
'============================================================================================
' Module : <any non-class module>
' Version : 0.1.1
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Public Sub Next_Click()
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -1
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + 1) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Then assign it to your button.
The only issue with this code is that it doesn't remember which cell it was up to when you re-open the workbook, and restarts from the first cell. This can be worked around if desired.
Addendum:
If you also wish to have a "Previous" button to cycle backwards, it gets slightly trickier - you need a generalised Previous/Next subroutine with a parameter to determine the direction. Then, each button needs to be assigned to separate subroutines that call the main routine with the appropriate argument:
'============================================================================================
' Module : <any non-class module>
' Version : 0.2.0
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Private Sub Next_or_Previous( _
ByRef direction As Long _
)
Dim plngDirection As Long: plngDirection = direction
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -plngDirection
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + plngDirection + .Cells.Count) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Public Sub Previous_Click()
Next_or_Previous -1
End Sub
Public Sub Next_Click()
Next_or_Previous 1
End Sub
add a comment |Â
There's no need to store a counter in a cell in the workbook. You can use a static variable instead.
Paste the following code into any non-class module:
'============================================================================================
' Module : <any non-class module>
' Version : 0.1.1
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Public Sub Next_Click()
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -1
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + 1) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Then assign it to your button.
The only issue with this code is that it doesn't remember which cell it was up to when you re-open the workbook, and restarts from the first cell. This can be worked around if desired.
Addendum:
If you also wish to have a "Previous" button to cycle backwards, it gets slightly trickier - you need a generalised Previous/Next subroutine with a parameter to determine the direction. Then, each button needs to be assigned to separate subroutines that call the main routine with the appropriate argument:
'============================================================================================
' Module : <any non-class module>
' Version : 0.2.0
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Private Sub Next_or_Previous( _
ByRef direction As Long _
)
Dim plngDirection As Long: plngDirection = direction
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -plngDirection
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + plngDirection + .Cells.Count) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Public Sub Previous_Click()
Next_or_Previous -1
End Sub
Public Sub Next_Click()
Next_or_Previous 1
End Sub
add a comment |Â
There's no need to store a counter in a cell in the workbook. You can use a static variable instead.
Paste the following code into any non-class module:
'============================================================================================
' Module : <any non-class module>
' Version : 0.1.1
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Public Sub Next_Click()
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -1
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + 1) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Then assign it to your button.
The only issue with this code is that it doesn't remember which cell it was up to when you re-open the workbook, and restarts from the first cell. This can be worked around if desired.
Addendum:
If you also wish to have a "Previous" button to cycle backwards, it gets slightly trickier - you need a generalised Previous/Next subroutine with a parameter to determine the direction. Then, each button needs to be assigned to separate subroutines that call the main routine with the appropriate argument:
'============================================================================================
' Module : <any non-class module>
' Version : 0.2.0
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Private Sub Next_or_Previous( _
ByRef direction As Long _
)
Dim plngDirection As Long: plngDirection = direction
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -plngDirection
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + plngDirection + .Cells.Count) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Public Sub Previous_Click()
Next_or_Previous -1
End Sub
Public Sub Next_Click()
Next_or_Previous 1
End Sub
There's no need to store a counter in a cell in the workbook. You can use a static variable instead.
Paste the following code into any non-class module:
'============================================================================================
' Module : <any non-class module>
' Version : 0.1.1
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Public Sub Next_Click()
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -1
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + 1) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Then assign it to your button.
The only issue with this code is that it doesn't remember which cell it was up to when you re-open the workbook, and restarts from the first cell. This can be worked around if desired.
Addendum:
If you also wish to have a "Previous" button to cycle backwards, it gets slightly trickier - you need a generalised Previous/Next subroutine with a parameter to determine the direction. Then, each button needs to be assigned to separate subroutines that call the main routine with the appropriate argument:
'============================================================================================
' Module : <any non-class module>
' Version : 0.2.0
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1331173/763880
'============================================================================================
Option Explicit
Private Sub Next_or_Previous( _
ByRef direction As Long _
)
Dim plngDirection As Long: plngDirection = direction
Const s_DestSheet As String = "Sheet1"
Const s_DestRange As String = "B5"
Const s_SrcSheet As String = "Sheet2"
Const s_SrcCell As String = "A1:A10"
Static sidxCurrentCell As Variant: If IsEmpty(sidxCurrentCell) Then sidxCurrentCell = -plngDirection
With Worksheets(s_SrcSheet).Range(s_SrcCell)
sidxCurrentCell = (sidxCurrentCell + plngDirection + .Cells.Count) Mod .Cells.Count
.Cells(sidxCurrentCell + 1).Copy Destination:=Worksheets(s_DestSheet).Range(s_DestRange)
End With
End Sub
Public Sub Previous_Click()
Next_or_Previous -1
End Sub
Public Sub Next_Click()
Next_or_Previous 1
End Sub
edited Dec 8 at 13:55
answered Jun 14 at 4:22
robinCTS
4,00941527
4,00941527
add a comment |Â
add a comment |Â
My approach is quit different to solve the issue.
I would like to suggest Worksheet Selection Change Event instead of Button Click , since it avoids the tedious Loop method task.
Worksheet Selection Change almost works like Button Click, since for 10 items 10 Clicks are required, as well as Selection Change Event also needs similar Clicks, and the best part is the order, it could be Ascending/Descending or even Random.
Below written code will copy cells from designated Data Range A1:A10
on Mouse Click, to destination Sheet's Cell.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
With Sheets("Sheet2")
.Select
.Range("B5").Value = Target.Value
End With
End If
End Sub
How the Macro works:
- Click any Cell between
A1:A10
at Source Sheet to Copy to Destination Sheet's CellB5
.
Note, Source Range A1:A10
, destination Sheet's name Sheet2
& Cell B5
are editable.
Write your concern to Down vote the post !!
â Rajesh S
Dec 9 at 9:15
1
Your post does not provide a solution to the OP. And responding to an old question that shows no effort is not good either.
â AJD
Dec 10 at 7:15
@AJD,, did you ever tried the Code I've posted, and find the Text on the TOP of my answer, I've mentioned that my approach is different and after that I've explained the reason behind opting the different approach!! Don't you think that a problem could have many Solutions and the way to solve also may different !!
â Rajesh S
Dec 10 at 7:35
1
The OP specifically asked for a button press which indicates a particular user exchange with the application. A selection change is a different action. Having said that, this is a very poor question with no attempts at a solution in the first place and shouldn't have been answered (the O is seeking a code writing service)..
â AJD
Dec 10 at 18:24
1
You are thinking like a coder, not a user. Yes, the underlying code is the same, but the user experience is different. The OP asks for a function that cycles on the click of a button, not a function that adapts to where the user clicks on the sheet.
â AJD
Dec 11 at 18:44
 |Â
show 1 more comment
My approach is quit different to solve the issue.
I would like to suggest Worksheet Selection Change Event instead of Button Click , since it avoids the tedious Loop method task.
Worksheet Selection Change almost works like Button Click, since for 10 items 10 Clicks are required, as well as Selection Change Event also needs similar Clicks, and the best part is the order, it could be Ascending/Descending or even Random.
Below written code will copy cells from designated Data Range A1:A10
on Mouse Click, to destination Sheet's Cell.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
With Sheets("Sheet2")
.Select
.Range("B5").Value = Target.Value
End With
End If
End Sub
How the Macro works:
- Click any Cell between
A1:A10
at Source Sheet to Copy to Destination Sheet's CellB5
.
Note, Source Range A1:A10
, destination Sheet's name Sheet2
& Cell B5
are editable.
Write your concern to Down vote the post !!
â Rajesh S
Dec 9 at 9:15
1
Your post does not provide a solution to the OP. And responding to an old question that shows no effort is not good either.
â AJD
Dec 10 at 7:15
@AJD,, did you ever tried the Code I've posted, and find the Text on the TOP of my answer, I've mentioned that my approach is different and after that I've explained the reason behind opting the different approach!! Don't you think that a problem could have many Solutions and the way to solve also may different !!
â Rajesh S
Dec 10 at 7:35
1
The OP specifically asked for a button press which indicates a particular user exchange with the application. A selection change is a different action. Having said that, this is a very poor question with no attempts at a solution in the first place and shouldn't have been answered (the O is seeking a code writing service)..
â AJD
Dec 10 at 18:24
1
You are thinking like a coder, not a user. Yes, the underlying code is the same, but the user experience is different. The OP asks for a function that cycles on the click of a button, not a function that adapts to where the user clicks on the sheet.
â AJD
Dec 11 at 18:44
 |Â
show 1 more comment
My approach is quit different to solve the issue.
I would like to suggest Worksheet Selection Change Event instead of Button Click , since it avoids the tedious Loop method task.
Worksheet Selection Change almost works like Button Click, since for 10 items 10 Clicks are required, as well as Selection Change Event also needs similar Clicks, and the best part is the order, it could be Ascending/Descending or even Random.
Below written code will copy cells from designated Data Range A1:A10
on Mouse Click, to destination Sheet's Cell.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
With Sheets("Sheet2")
.Select
.Range("B5").Value = Target.Value
End With
End If
End Sub
How the Macro works:
- Click any Cell between
A1:A10
at Source Sheet to Copy to Destination Sheet's CellB5
.
Note, Source Range A1:A10
, destination Sheet's name Sheet2
& Cell B5
are editable.
My approach is quit different to solve the issue.
I would like to suggest Worksheet Selection Change Event instead of Button Click , since it avoids the tedious Loop method task.
Worksheet Selection Change almost works like Button Click, since for 10 items 10 Clicks are required, as well as Selection Change Event also needs similar Clicks, and the best part is the order, it could be Ascending/Descending or even Random.
Below written code will copy cells from designated Data Range A1:A10
on Mouse Click, to destination Sheet's Cell.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
With Sheets("Sheet2")
.Select
.Range("B5").Value = Target.Value
End With
End If
End Sub
How the Macro works:
- Click any Cell between
A1:A10
at Source Sheet to Copy to Destination Sheet's CellB5
.
Note, Source Range A1:A10
, destination Sheet's name Sheet2
& Cell B5
are editable.
edited Dec 10 at 6:28
answered Dec 9 at 7:57
Rajesh S
3,7041522
3,7041522
Write your concern to Down vote the post !!
â Rajesh S
Dec 9 at 9:15
1
Your post does not provide a solution to the OP. And responding to an old question that shows no effort is not good either.
â AJD
Dec 10 at 7:15
@AJD,, did you ever tried the Code I've posted, and find the Text on the TOP of my answer, I've mentioned that my approach is different and after that I've explained the reason behind opting the different approach!! Don't you think that a problem could have many Solutions and the way to solve also may different !!
â Rajesh S
Dec 10 at 7:35
1
The OP specifically asked for a button press which indicates a particular user exchange with the application. A selection change is a different action. Having said that, this is a very poor question with no attempts at a solution in the first place and shouldn't have been answered (the O is seeking a code writing service)..
â AJD
Dec 10 at 18:24
1
You are thinking like a coder, not a user. Yes, the underlying code is the same, but the user experience is different. The OP asks for a function that cycles on the click of a button, not a function that adapts to where the user clicks on the sheet.
â AJD
Dec 11 at 18:44
 |Â
show 1 more comment
Write your concern to Down vote the post !!
â Rajesh S
Dec 9 at 9:15
1
Your post does not provide a solution to the OP. And responding to an old question that shows no effort is not good either.
â AJD
Dec 10 at 7:15
@AJD,, did you ever tried the Code I've posted, and find the Text on the TOP of my answer, I've mentioned that my approach is different and after that I've explained the reason behind opting the different approach!! Don't you think that a problem could have many Solutions and the way to solve also may different !!
â Rajesh S
Dec 10 at 7:35
1
The OP specifically asked for a button press which indicates a particular user exchange with the application. A selection change is a different action. Having said that, this is a very poor question with no attempts at a solution in the first place and shouldn't have been answered (the O is seeking a code writing service)..
â AJD
Dec 10 at 18:24
1
You are thinking like a coder, not a user. Yes, the underlying code is the same, but the user experience is different. The OP asks for a function that cycles on the click of a button, not a function that adapts to where the user clicks on the sheet.
â AJD
Dec 11 at 18:44
Write your concern to Down vote the post !!
â Rajesh S
Dec 9 at 9:15
Write your concern to Down vote the post !!
â Rajesh S
Dec 9 at 9:15
1
1
Your post does not provide a solution to the OP. And responding to an old question that shows no effort is not good either.
â AJD
Dec 10 at 7:15
Your post does not provide a solution to the OP. And responding to an old question that shows no effort is not good either.
â AJD
Dec 10 at 7:15
@AJD,, did you ever tried the Code I've posted, and find the Text on the TOP of my answer, I've mentioned that my approach is different and after that I've explained the reason behind opting the different approach!! Don't you think that a problem could have many Solutions and the way to solve also may different !!
â Rajesh S
Dec 10 at 7:35
@AJD,, did you ever tried the Code I've posted, and find the Text on the TOP of my answer, I've mentioned that my approach is different and after that I've explained the reason behind opting the different approach!! Don't you think that a problem could have many Solutions and the way to solve also may different !!
â Rajesh S
Dec 10 at 7:35
1
1
The OP specifically asked for a button press which indicates a particular user exchange with the application. A selection change is a different action. Having said that, this is a very poor question with no attempts at a solution in the first place and shouldn't have been answered (the O is seeking a code writing service)..
â AJD
Dec 10 at 18:24
The OP specifically asked for a button press which indicates a particular user exchange with the application. A selection change is a different action. Having said that, this is a very poor question with no attempts at a solution in the first place and shouldn't have been answered (the O is seeking a code writing service)..
â AJD
Dec 10 at 18:24
1
1
You are thinking like a coder, not a user. Yes, the underlying code is the same, but the user experience is different. The OP asks for a function that cycles on the click of a button, not a function that adapts to where the user clicks on the sheet.
â AJD
Dec 11 at 18:44
You are thinking like a coder, not a user. Yes, the underlying code is the same, but the user experience is different. The OP asks for a function that cycles on the click of a button, not a function that adapts to where the user clicks on the sheet.
â AJD
Dec 11 at 18:44
 |Â
show 1 more comment
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1331120%2fpressing-a-button-to-cycle-through-a-ranges-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
So, what have you tried so far?
â music2myear
Jun 13 at 23:54