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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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

Forum statistics

Threads
1,224,813
Messages
6,181,118
Members
453,021
Latest member
Justyna P

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