Selection Range

G2K

Active Member
Joined
May 29, 2009
Messages
355
Hi All.

i need to check values in the selection and perform some calculation accoringly

Selection can be vertical as -
A1 P
A2 P
A3 P
A4 P
A5 P
A6 P
A7 P
A8 P
A9 P
A10 P

or Horiz. as -

A1 A2 A3 A4 A5 A6 A7
CL CL CL CL CL CL CL

i want to set range accordingly and do the calculation using for loop.

Pls help.....

thanks in advance
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi, Select range Horizontal or Vertical, Run code.
The Msgbox will show the address and value of each cell in range.
Modify code for your purpose.
Code:
[COLOR=navy]Sub[/COLOR] MG07Oct19
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Set[/COLOR] Rng = Selection
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    MsgBox Dn.Address & " / " & Dn.Value
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Upvote 0
You can also just refer to the Selection
Code:
Sub Something()
Dim cl as Range
For Each cl in Selection
  'some code
Next cl
End Sub

lenze
 
Upvote 0
thanks for your reply.

but i am using spreadsheet contorl here and it generate an error -

'Run Time Error '13'
Type Mismatch

Regards
 
Upvote 0
Dim Rng As Range
Dim cl As Range
Call dbConnect
Set Rng = Spreadsheet1.Selection' Generate Error - " Type Mismatch"

For Each cl In Rng
Sql = "Update tblAttendance Set Status = 'CL' where EmpId = " & Spreadsheet1.Cells(Spreadsheet1.ActiveCell.Row, 1).Value & " and WorkingDate = #" & VBA.Format(Spreadsheet1.Cells(1, Spreadsheet1.ActiveCell.Column).Value, "dd - mmm - yyyy") & "#"

Con.BeginTrans
Con.Execute Sql
Con.CommitTrans

Next cl

Set Rng = Spreadsheet1.Selection' Generate Error - " Type Mismatch"
For Each cl In Rng - " Type Mismatch"
where Spreadsheet1 is Spreadsheet Object.

thanks
 
Last edited:
Upvote 0
thnaks Mick/Lenze

finally i got the solution -

Dim Rng As Range
Dim i As Integer
Set Rng = Spreadsheet1.Selection
For i = 1 To Rng.Count

Spreadsheet1.Selection(i).Activate
'Code
Next i

thanks a ton for your precious time and suggestion
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,183
Members
448,872
Latest member
lcaw

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