Declare a variable to a cell

vinidis

New Member
Joined
Aug 11, 2017
Messages
8
Hi everyone!

I've been stuck with declaring a variable to a cell value and need some help.
Situation: I have a code that filters a table and transfers the filtered data to another sheet. The filter could be the date value but I can't set it properly. The result is always Error 1004.
I've tried a couple of solutions like set, with etc but non of them worked.

My code:
Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Option Explicit
Sub CopyFilteredData()

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Dim sDate As Variant

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]With Worksheets("Lista")
    sDate = .Range("B1").Value
End With

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Worksheets("GYARTAS").Range("A1:I1").AutoFilter
Worksheets("GYARTAS").Range("A1:I3000").AutoFilter Field:=2, Criteria1:=sDate

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Worksheets("GYARTAS").Range("A2:I25").SpecialCells(xlCellTypeVisible).Copy
Worksheets("Lista").Range("B35").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Worksheets("GYARTAS").AutoFilterMode = False

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]End Sub[/FONT]

Can someone do this to me?
Thank you.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
It works for me as follows:

In the "GYARTAS" sheet the column "B" format must be "dd/mm/yyyy" or "mm/dd/yyyy"

Try the following code:

VBA Code:
Sub CopyFilteredData()
  Dim sDate As Date, sh1 As Worksheet, sh2 As Worksheet, x
  Set sh1 = Sheets("Lista")
  Set sh2 = Sheets("GYARTAS")
  sDate = sh1.Range("B1").Value
  sh2.Range("A1:I1").AutoFilter 2, Format(sDate, "mm/dd/yyyy")
  sh2.AutoFilter.Range.Offset(1).Copy
  sh1.Range("B35").PasteSpecial Paste:=xlPasteValues
  Application.CutCopyMode = False
  sh2.AutoFilterMode = False
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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