Need a little help with vba: Please advice on range selection till last row from given address.

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, Last line in the code is where i need help...

After finding and setting found adderss as address i want vba to select that range and copy it to sheet2.range("A1") as values.


Thanks alot for helping


Code:
[/FONT]
[FONT=Courier New]Dim startdate As String
Dim enddate As String
Dim myfirstadd, mysecondadd As String[/FONT]
[FONT=Courier New]startdate = Me.DTPicker1.Value
enddate = Me.DTPicker2.Value[/FONT]
[FONT=Courier New]Dim c As Range
With Sheet1.Range("a1:as1")
Set c = .Find(startdate, LookIn:=xlValues)
If c Is Nothing Then
MsgBox startdate & " not found"
Exit Sub
Else
myfirstadd = c.Address
End If
End With[/FONT]
[FONT=Courier New]With Sheet1.Range("a1:as1")
Set c = .Find(enddate, LookIn:=xlValues)
If c Is Nothing Then
MsgBox startdate & " not found"
Exit Sub
Else
mysecondadd = c.Address
End If
End With[/FONT]
[FONT=Courier New]dim lr as long[/FONT]
[FONT=Courier New]lr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row[/FONT]
[FONT=Courier New][B]Range(myfirstadd & ":" & mysecondadd & Lr).Select[/B][/FONT]
[FONT=Courier New]
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
No problem.:)

You could actually make it a little simpler by not using the addresses.

This worked for me with 'real' dates in row 1.
Code:
Sub test()
Dim startdate As Date
Dim enddate As Date
Dim myfirstadd As Range, mysecondadd As Range
Dim lr As Long

    startdate = Me.DTPicker1.Value
    enddate = Me.DTPicker2.Value
 
'        startdate = DateSerial(2011, 5, 1)
'        enddate = DateSerial(2011, 5, 4)
    
    With Sheet1.Range("A1:AS1")
        Set myfirstadd = .Find(startdate, LookIn:=xlValues)
        If myfirstadd Is Nothing Then
            MsgBox startdate & " not found"
            Exit Sub
        End If
    End With
 
    With Sheet1.Range("A1:AS1")
        Set mysecondadd = .Find(enddate, LookIn:=xlValues)
        If mysecondadd Is Nothing Then
            MsgBox startdate & " not found"
            Exit Sub
        End If
    End With
 
    lr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
 
    Range(myfirstadd, mysecondadd).Resize(lr).Copy
    
End Sub
 
Upvote 0
Peter, what does CLng function do? I am not sure....
Norie, thats much better....!


Thanks again guys!
 
Upvote 0
Peter, thanks for explaining...
No, it says not found even though the dates are there.
so i first set the format to general, and then enter date picker values in sheet cell and then format the same to general then run the code..then it finds it...:)
Thats kinda long way, yea? :D

(This is not the first time..this things has happend to me many a times...)

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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