Hi I am trying to upload the Data from the main excel workbook to another database excel workbook using the upload code(Mentioned below). Apparently it seems that the code is not able to pick the Cell.Value (i.e the date value in excel sheet - format of date is M/D/YYYY) and the userform textbox date value - format of date (M/D/YYYY).
Private Sub Upload()
Dim SourceWB As Workbook
Dim SourceWs As Worksheet
Dim DesWB As Workbook
Dim DesWs As Worksheet
Dim DateRange As Range
Dim DesDataRange As Range
Dim LastRowCount As Long 'Upload Button Value
Dim DesLastRow As Long
Dim Ls As Long
Dim Y As Long
Set SourceWB = ThisWorkbook
'Debug.Print SourceWB
Set SourceWs = SourceWB.Worksheets("Database")
'Debug.Print SourceWs
'Debug.Print DesWB
Set DesWB = ActiveWorkbook
'Debug.Print DesWs
Set DesWs = DesWB.ActiveSheet
Workbooks(FileNameValue).Activate
ActiveWorkbook.Worksheets("Sheet1").Range("A2:T9999").ClearContents
LastRowCount = SourceWs.Range("D" & Rows.count).End(xlUp).Row
DesLastRow = DesWs.Range("D" & Rows.count).End(xlUp).Row
Set DateRange = SourceWs.Range("D2", "D" & LastRowCount)
Set DesDateRange = DesWs.Range("D2", "D" & DesLastRow)
'Paste Similar Date Values to destination file
For Each Cell In DateRange '(frmData.txtdate.value is the textbox value of the userform)
If Cell.Value = frmData.txtdate.Value Then **'(It seems to have problem over here)**
Debug.Print frmData.txtdate.Value
Debug.Print Cell.Value
'Y = Cell.Row 'Cells(y, 1), Cells(y, 20)
SourceWs.Range("A" & 2, "T" & LastRowCount).Copy
Workbooks(FileNameValue).Activate
Ls = ActiveWorkbook.Worksheets("Sheet1").Range("A" & Rows.count).End(xlUp).Row
ActiveWorkbook.Worksheets("Sheet1").Range("A" & Ls + 1).PasteSpecial Paste:=xlPasteValues
Exit For
End If
Next
'My Routine:
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub
ActiveWorkbook
andActiveSheet
(or any form ofActivate
orSelect
for that mater. Have a look at this for more information). Please fully qualify your workbooks and worksheets. Also, once you set your workbooks and worksheets, you then have this statement:Workbooks(FileNameValue).Activate
.. is there a third workbook involved? Otherwise you could just use the workbooks that you have already set – ZacFileNameValue
is a public variable why didn't you make the object itself such a variable, say,Wb3
. Note that ".Range("D2", "D" & LastRowCount)" is wrong syntax. "Range("D2:D"& LastRowCount)" might work orRange(Range("D2"),Range("D"&LastRowCount))
– Variatus