Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

VBA Check if date is valid in a certain format DD.MM.

YYYY
Here's one way:
Code:
Sub DateCheck()
Dim myDate As String
Dim myDateCheck As Boolean
myDate = Format((DateSerial(Right(Range("A1"), 4), Mid(Range("A1"), 4, 2),
Left(Range("A1"), 2))), "dd.mm.yyyy")
myDateCheck = (Range("A1") = myDate)
MsgBox myDateCheck
End Sub

VBA: Find cell value in another workbook and copy row contents

Dim wbPrev As Workbook


Dim wbCurr As Workbook
Dim wsPrev As Worksheet
Dim wsCurr As Worksheet
Dim lrPrev As Long
Dim lrCurr As Long
Dim i As Long
Dim rc As Variant
Set wbPrev =
ReadOnly:=True)
Set wbCurr =
ReadOnly:=False)
Set wsPrev =
Set wsCurr =

Workbooks.Open(Filename:=ThisWorkbook.Path & "\PrevDay.xlsx",


Workbooks.Open(Filename:=ThisWorkbook.Path & "\CurrDay.xlsx",
wbPrev.Worksheets("Sheet1")
wbCurr.Worksheets("Sheet1")

lrPrev = wsPrev.Cells(wsPrev.Rows.Count, 1).End(xlUp).Row


lrCurr = wsPrev.Cells(wsCurr.Rows.Count, 1).End(xlUp).Row
For i = 2 To lrCurr
rc = Application.Match(wsCurr.Cells(i, 1).Value, wsPrev.Range("A1:A" &
lrPrev), 0)
If Not IsError(rc) Then
Debug.Print wsCurr.Range("J1:T1").Offset(i - 1, 0).Address,
wsPrev.Range(wsPrev.Cells(rc, "J"), wsPrev.Cells(rc, "T")).Address
wsCurr.Range("J1:T1").Offset(i - 1, 0).Value =
wsPrev.Range(wsPrev.Cells(rc, "J"), wsPrev.Cells(rc, "T")).Value
End If
Next
End Sub

Sub CopyRowAndBelowToTarget()
Dim wb As Workbook
Dim src As Worksheet
Dim tgt As Worksheet
Dim match As Range
Set wb = ThisWorkbook
Set src = wb.Sheets("Sheet1")
Set tgt = wb.Sheets("Sheet2")
Dim
Dim
Dim
Dim
Dim

lastCopyRow As Long
lastPasteRow As Long
lastCol As Long
matchRow As Long
findMe As String

' specify what we're searching for


findMe = "s"
' find our search string in column A (1)
Set match = src.Columns(1).Find(What:=findMe, After:=src.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
' figure out what row our search string is on
matchRow = match.Row
' get the last row and column with data so we know how much to copy
lastCopyRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
lastCol = src.Cells(1, src.Columns.Count).End(xlToLeft).Column
' find out where on our target sheet we should paste the results
lastPasteRow = tgt.Range("A" & src.Rows.Count).End(xlUp).Row
' use copy/paste syntax that doesn't use the clipboard
' and doesn't select or activate
src.Range(Cells(matchRow, 1), Cells(lastCopyRow, lastCol)).Copy _
tgt.Range("A" & lastPasteRow)
End Sub

You might also like