Declare a variable to a cell

vinidis

New Member
Joined
Aug 11, 2017
Messages
7
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.
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,192
Office Version
2007
Platform
Windows
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,192
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,089,376
Messages
5,407,903
Members
403,169
Latest member
Luna17

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top