简体   繁体   中英

How to prevent a row from copying into another workbook when the range has no data while using 'LastRow' 'xlUp'

I have a Workbook called " INVOICE.xls " with Sheet " INVOICE " and another workbook called " DATABASE.xls " with sheet " DATABASE ".

I have two Ranges of data in Workbook " INVOICE.xls " Sheet " INVOICE " which is assuming rngA-(A13 to I29) and rngB-(B23 to I29) both of which have headers above them, which I transfer to Workbook " DATABASE.xls " Sheet " DATABASE " using VBA code. The range rngB have data occasionally. The code I have now transfers successfully only if there is a row with data in rngB . On occasions when there is no data in rngB , it copies the row above the specified range ie the header labels. Pasting the code below. I'm not an expert, I have just pasted codes from various forums to get it to work until now. Screenshot-Invoice.xls Screenshot of Database.xls

EDIT - There's another error where I need some help. When both the ranges rngA & rngB are full of data, it doesn't paste that range. Instead, it pastes the range A3:I3 from the "INVOICE.xls" sheet "INVOICE" onto the "DATABASE.xls" sheet "DATABASE" column ranging J:R . Please help.

Sub SavingData()
Dim rngA As Range
Dim rngB As Range
  Dim i As Long
  Dim a As Long
  Dim b As Long
  Dim rng_dest As Range

  Application.ScreenUpdating = False
  Windows("DATABASE.xls").Activate
  'Check if invoice # is found on sheet "DATABASE"
  i = 2
  Do Until Sheets("DATABASE").Range("A" & i).Value = ""
    If ActiveWorkbook.Sheets("DATABASE").Range("A" & i).Value = Workbooks("INVOICE").Sheets("INVOICE").Range("H8").Value Then
      'Ask overwrite invoice #?
      If MsgBox("Invoice Number Already Exists - Do you want to overwrite?", vbYesNo) = vbNo Then
        Exit Sub
      Else
        Exit Do
      End If
    End If
    i = i + 1
  Loop
  i = 1
    Windows("INVOICE.xls").Activate
  Windows("DATABASE.xls").Activate
  Set rng_dest = Sheets("DATABASE").Range("J:R")
  'Delete rows if invoice # is found
  Do Until Sheets("DATABASE").Range("A" & i).Value = ""
    If Workbooks("DATABASE").Sheets("DATABASE").Range("A" & i).Value = Workbooks("INVOICE").Sheets("INVOICE").Range("H8").Value Then
      Workbooks("DATABASE").Sheets("DATABASE").Range("A" & i).EntireRow.Delete
      i = 1
    End If
    i = i + 1
  Loop
' Find first empty row in columns B:I on sheet Sales

Windows("INVOICE").Activate
    Do Until WorksheetFunction.CountA(rng_dest.Rows(i)) = 0
    i = i + 1
  Loop
    'Copy range A13:I20 on sheet Invoice
  With Sheets("INVOICE")
    Dim lastRowA As Long
    Dim lastRowB As Long
    lastRowA = .Cells(20, 1).End(xlUp).Row
    lastRowB = .Cells(29, 1).End(xlUp).Row
    Set rngA = .Range(.Cells(13, 1), .Cells(lastRowA, 9))
    Set rngB = .Range(.Cells(23, 1), .Cells(lastRowB, 9))
End With
  ' Copy rows containing values to sheet Sales
 For a = 1 To rngA.Rows.Count
    If WorksheetFunction.CountA(rngA.Rows(a)) <> 0 Then
      rng_dest.Rows(i).Value = rngA.Rows(a).Value
      'Copy Field 1
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("A" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("H8").Value
      'Copy Field 2
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("B" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("C9").Value
      'Copy Field 3
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("C" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("B10").Value
       'Copy Field 4
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("D" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("E8").Value
      'Copy Field 5
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("E" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("G10").Value
      'Copy Field 6
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("F" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("C11").Value
      'Copy Field 7
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("G" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("E11").Value
      'Copy Field 8
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("H" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("H11").Value
      'Copy Field 9
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("I" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("I11").Value
      i = i + 1
    End If
  Next a
   For b = 1 To rngB.Rows.Count
    If WorksheetFunction.CountA(rngB.Rows(b)) <> 0 Then
      rng_dest.Rows(i).Value = rngB.Rows(b).Value
      'Copy Field 1
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("A" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("H8").Value
      'Copy Field 2
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("B" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("C9").Value
      'Copy Field 3
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("C" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("B10").Value
       'Copy Field 4
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("D" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("E8").Value
      'Copy Field 5
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("E" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("G10").Value
      'Copy Field 6
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("F" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("C11").Value
      'Copy Field 7
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("G" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("E11").Value
      'Copy Field 8
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("H" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("H11").Value
      'Copy Field 9
      Workbooks("DATABASE.xls").Sheets("DATABASE").Range("I" & i).Value = Workbooks("INVOICE.xls").Sheets("INVOICE").Range("I11").Value
      i = i + 1
    End If
  Next b
  Application.ScreenUpdating = True
End Sub

you could check for lastRowB to be greater then 23 before staring the rngB copy/pasting:

If lastRowB > 23 Then
       For b = 1 To rngB.Rows.Count

            ' your code

       Next b
End If

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