how to get the selected range address in the form of a string value - cells

I am writing a simple macro where I have selected a range by using EndDownArrow and now want the VB to give me back the selected range. Can anyone please help?
My codes so far are:-
Range("B4:C4").Select
Range(Selection, Selection.End(xlDown)).Select

Sub G()
Dim rng As Range
With Range("B4:C4")
Set rng = Range(.Cells, .End(xlDown))
End With
End Sub

Related

Application.Caller / Storing worksheet name as string

Im trying to store the CurrentWorksheet name in order to reference it in a different Sub routine.
The code I currently have is as follows:
Private Sub InsertNewBill_Click()
Dim rng As Range
Set rng = Worksheets(CurrentWorksheet).Range("A30:L30")
rng.Insert Shift:=xlDown
End Sub
Current Worksheet Function:
Function CurrentWorksheet()
CurrentSheet = Application.Caller.Worksheet.Name
End Function
I need to try to reference the "CurrentSheet" variable in the "InsertNewBill" Sub routine.
The function of this is to insert a new line of cells between "A30:L30" on the currently selected worksheet.
Thanks in advance
I didn't plan to write this as answer, but to explain to Batteredburrito how to deal with objects rather than names:
Option Explicit
Private Sub InsertNewBill_Click()
Dim rng As Range
Set rng = currentWorksheet.Range("A31:AC31")
rng.Insert Shift:=xlDown
End Sub
Current Worksheet Function
Function currentWorksheet() As Worksheet
set currentWorksheet = ActiveSheet
End Function
This is doing exactly the same as your version (so you can stick with that), it's just to show how to deal with objects. But there are situations where it is much better to deal with objects that with name - especially if you are dealing with multiple Workbooks (that might have the same sheet names).
And, of course, in the end you could get rid of the function completely by simply writing
...
Set rng = ActiveSheet.Range("A31:AC31")
...
Thank you all for your help and direction.
I have resolved the issue with the following
Insert new cells between a range of cells on button press:
Private Sub InsertNewBill_Click()
Dim rng As Range
Set rng = Worksheets(CurrentWorksheet).Range("A31:AC31")
rng.Insert Shift:=xlDown
End Sub
Current Worksheet Function:
Function CurrentWorksheet()
CurrentWorksheet = ActiveSheet.Name
End Function

Run time error 13 on For loop

I am trying to use a combobox in my user interface, but if none of the options are good for the user they can type it in but after if they have entered something I want to save it so next time it appears in the list. I have tried the following approach:
For i = Range("O3") To Range("O3").End(xlDown)
If Not i.Value = ComboType.Value Then
Range("O3").End(xlDown) = ComboType.Value
End If
Next i
But this gives the above error on the first line. I am not very familiar with For loops in VBA so I am hoping somebody can help me.
This is how to make the for-each loop from O3 to the last cell with value after O3:
Public Sub TestMe()
Dim myCell As Range
Dim ws As Worksheet
Set ws = Worsheets(1)
With ws
For Each myCell In .Range("O3", .Range("O3").End(xlDown))
Debug.Print myCell.Address
Next myCell
End with
End Sub
It is a good practise to declare the worksheet as well, because otherwise you will always work with the ActiveSheet of the ActiveWorkbook.

PasteSpecial not pasting, but code does not error

At this point there are two problems, but the first one i want to deal with is that i cannot get the paste function to work. When I run through the code the specific cells are highlighted to copy (the cell border is b&w flashing) and the cells where they are to end up are now highlighted, but nothing pastes.
Sub OtherTask()
Dim DRng As Range
ActiveSheet.Range("g2:ah2").find(Date).Select
ActiveCell.Resize(5).Offset(5).Select
Selection.AutoFilter field:=1, Criteria1:="1", Operator:=xlFilterValues
Set DRng = ThisWorkbook.ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
DRng.Copy
ActiveSheet.Range("r12").PasteSpecial xlPasteAll
If ActiveSheet.AutoFilterMode = "True" Then
ActiveSheet.AutoFilterMode = "False"
End If
End Sub
I should bring up the second problem. When I execute this from the macro button it performs as per the description above, but when I am in the editor and I press the play button I get error 91 that the object is not set. Not sure why I would get the error with one form of execution and not the other?? Looking through similar perhaps I should be using value instead of copy? Thanks for any help.
I had to make some assumptions with your code because there are some things that are not that clear. The assumptions should be easy to see and to change according to your needs.
Sub OtherTask()
Dim ws as Worksheet
Dim DRng As Range
Set ws = Worksheets("mySheet")
With ws
Dim rFound as Range
Set rFound = .Range("g2:ah2").find(Date)
rFound.Resize(5).Offset(5).AutoFilter field:=1, Criteria1:="1", Operator:=xlFilterValues
'declare this range explicitly, whatever it is
Set DRng = .Range("A1:B5000").AutoFilter.Range.SpecialCells(xlCellTypeVisible)
DRng.Copy .range("R12") 'since you paste everything just do straight from copy method
If .AutoFilterMode = "True" Then .AutoFilterMode = "False"
End With
End Sub

Why does ActiveSheet.FilterMode returns False when sheet has filter?

I'm using the following code in an attempt to detect a filter applied to a column in a table and then clear the filter:
If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
According to Microsoft documentation:
This property is true if the worksheet contains a filtered list in which there are hidden rows.
This doesn't seem to be the case since ActiveSheet.Filtermode only returns True if a cell inside the table where the filter is applied is selected.
First question: Is the documentation wrong? Documentation
Second question: Is my only option to select a cell inside the table to get the expression to return True?
PS I am using Excel 2010
Edit: Answer to Question 2, Non-select based methods to clear filters...
If ActiveSheet.ListObjects(1).Autofilter.FilterMode Then ActiveSheet.ListObjects(1).Autofilter.Showalldata
I can replicate both your issues on Excel 2013: both the buggy False on FilterMode and the error on ShowAllData.
In response to whether the documentation is wrong, I would say that it is missing a qualification to say that the ActiveCell should be in the ListObjects DataBodyRange. Perhaps the documentation is correct but that this is a bug that has not been addressed. Maybe you can update your question with a link to the documentation?
Re your second question - I agree that using this workaround is the most obvious solution. It seems a bit unpleasant to use Select but sometimes I guess this cannot be avoided.
This is how I did it using the Intersect function to check it the ActiveCell is currently in the area of the DataBodyRange of the ListObject:
Option Explicit
Sub Test()
Dim rng As Range
Dim ws As Worksheet
Dim lst As ListObject
'get ActiveCell reference
Set rng = ActiveCell
'get reference to Worksheet based on ActiveCell
Set ws = rng.Parent
'is there a Listobject on the ActiveCells sheet?
If ws.ListObjects.Count > 0 Then
Set lst = ws.ListObjects(1)
Else
Debug.Print "No table found"
Exit Sub
End If
'is cell is in the DataBodyRange of ListObject?
If Intersect(rng, lst.DataBodyRange) Is Nothing Then
'set the ActiveCell to be in the DataBodyRange
lst.DataBodyRange.Cells(1, 1).Select
End If
'now you can safely call ShowAllData
If ws.FilterMode = True Then
ws.ShowAllData
End If
End Sub
Edit
Further to #orson's comment:
What happens if you skip the If Intersect(rng, lst.DataBodyRange) Is Nothing Then and use If lst.AutoFilter.FilterMode Then lst.AutoFilter.ShowAllData End If ?
So, you can check the FilterMode of the ListObject itself and then as long as you have a reference to the ListObject you can use his code:
If lst.AutoFilter.FilterMode Then
lst.AutoFilter.ShowAllData
End If
An easier alternative can be to just AutoFit all rows:
Rows.AutoFit
The issue with that is that it will un-hide and auto fit all rows on the active sheet.
Update from http://www.contextures.com/excelautofilterlist.html
Dim list As ListObject
For Each list ActiveSheet.ListObjects
If list.AutoFilter.FilterMode Then
list.AutoFilter.ShowAllData
End If
Next

How to tell if range is on activesheet or on a fixed/certain sheet?

if I
Dim rng As Range
Set rng = Range("A1")
i know rng is actually ActiveSheet.Range("A1")
then if I
Dim rngsht1 As Range
Set rngsht1 = Worksheets("Sheet1").Range("A1")
then i know rngsht1 is always on Sheet1
So now say I have a range object called "somerange", if I want to know what sheet this range is on I'll probably do this
somerange.worksheet.name
but if it gives me "Sheet1", i can't tell if it's because i have sheet1 active or because somerange is always on sheet1, without having to switch between different sheets and try again.
My question is, is there a simple way to tell whether a range object is on activesheet, or on a fixed/certain sheet? Thanks.
UPDATE: So this question is invalid. Thanks to GSerg I realized that a range object, once created, is ALWAYS on a fixed worksheet which is the worksheet the range object was created on.
Please try to use Is operator (object comparison):
If rangeObject.Worksheet Is ActiveSheet Then
' (... your code ...)
End If
Question the range's Parent
Sub MAIN()
Dim rng As Range
Set rng = Sheets(1).Range("A1")
Call IsItOnTheActiveSheet(rng)
End Sub
Sub IsItOnTheActiveSheet(r As Range)
If r.Parent.Name = ActiveSheet.Name Then
MsgBox "its on the activesheet"
Else
MsgBox "its not on the active sheet"
End If
End Sub