简体   繁体   中英

Access-VBA check if a cell in an opened excel workbook is empty before copying the row

this is my first time asking a question here.

I have run into an issue at work where I need to copy certain rows from one workbook to another. The copying part I believe I may have solved but my issue is with something extremely simple.

To determine which rows to copy I have to look at 4 dates within the first workbook, the one check with them I am having trouble with is checking if the cell is empty of not. As every time I run my code a mismatch error 13 occurs at the check.

    Dim XX As Excel.Application    ----secondary workbook
    Dim ws1 As Object
    Dim ws As Object

    Set XX = New Excel.Application
    XX.DisplayAlerts = False

    Set ws1 = XX.Workbooks.Add
    ws1.Activate
    Set ws = ws1.ActiveSheet

    Set xl = New Excel.Application   ----primary workbook
    xl.DisplayAlerts = False

    Set wb = xl.Workbooks.Open(FilePath)

For i = 2 To wb.Sheets("SHEET1").Range("Q65536").End(xlUp).Row  --- i have the code looping through all rows of the primary workbook

        Date1 = wb.Sheets("SHEET1").Cells(i, 12).Text
        Date2= wb.Sheets("SHEET1").Cells(i, 14).Text
        Date3= wb.Sheets("SHEET1").Cells(i, 13)
        Date4 = wb.Sheets("SHEET1").Cells(i, 15)    -----  everything up to here has no errors

        If ((CDate(Date1) < Date) & (IsEmpty(Date3))) Then ---- error is always here, at Date3

            wb.Rows(i).Copy ws.Rows(ws.Cells(ws.Rows.Count, 2).End(xlUp).Row + 1)

        end if

wb is the primary workbook I am taking data from (hopefully) and ws is the workbook I am copying into.

I know the error is specifically at the empty check in the if statement because I have taken it out and left only the less than check and the code as progressed as intended. I just cannot figure out how to fix it.

I have declared the date variables for everything from string, integer, date, and variant. And have tried importing the cell values as a range and cells, with .text or .value or nothing. Then within the if statement I have tried IsEmpty, Isnull, = "", = 0, = null, added in CDate. I have literally tried everything thing in every possible combination I can think of.

You could try:

 Date3 = trim(wb.Sheets("SHEET1").Cells(i, 13))
 ....IsNull(Date3)

The idea here is to trim whitespace first then check for null values.

Hope this helps :)

Brain M Stafford 在评论中给出了答案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM