Changing a VBA code to select cells from a range +one other cell outside the range


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Changing a VBA code to select cells from a range +one other cell outside the range

  1. #1
    Join Date
    Aug 2009
    Posts
    37

    Changing a VBA code to select cells from a range +one other cell outside the range

    Hi,

    I have a working VBA code which copies from a range of cells in a row from a worksheet and pastes them into another sheet.

    InvSheet.Range(InvSheet.Cells(iRow, "B"), InvSheet.Cells(iRow, "K")).Copy
    BatchSheet.Cells(oRow, "D").PasteSpecial xlPasteValues
    iRow = iRow + 1
    oRow = oRow + 1
    Loop Until IsEmpty(InvSheet.Cells(iRow, "B")) Or InvSheet.Cells(iRow, "B") = 0

    I need to make a change to this so that it picks one additional cell and copies its contents.

    ie (Range (same row) A to K) + (same Row) Z

    Can anyone help changing the code above?

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,262
    Try using this notation Range("B1:K1, Z1"), here is the new line of code:
    InvSheet.Range("B" & iRow & ":K" & iRow & ", Z" & iRow).Copy

    Be warned that when you paste the new values there will be no gap between value copied from column K and the value copied from column Z. Since you are pasting to column D the value from column K ends up in column M and the value from column Z will end up in column N.

  3. #3
    Join Date
    Aug 2009
    Posts
    37
    Thanks Ron.

    I have now changed that particular code (with someones help) so now it does what I want it to.

    However, it has thromw out another problem with which you might be able to help. Should be simple enough, but not for a novice like me.

    This is the code as amended now.

    --------- ----- VBA Code -----------

    Sub InvoiceToBatch()
    'Copies the current invoice in the INVOICE TEMPLATE sheet of
    'this workbook to stocksbatch.xls
    Dim Account As String
    Dim InvDate As Date
    Dim InvNum As Long 'invoice "FR" number
    Dim InvSheet As Worksheet
    Dim BatchSheet As Worksheet
    Dim NextRow As Long 'the next available invoice row on the batch sheet
    Dim oRow As Long 'row number on BatchSheet
    Dim iRow As Long 'row number on InvSheet

    Set InvSheet = ThisWorkbook.Worksheets("INVOICE TEMPLATE")

    Workbooks.Open Filename:="T:\Amnesty\stocksbatch.xls"
    Set BatchSheet = ActiveWorkbook.Worksheets("Sheet1")
    NextRow = BatchSheet.Range("D65536").End(xlUp).Row + 1
    oRow = NextRow
    iRow = 20

    With InvSheet
    Account = .Range("E5")
    InvNum = .Range("K2")
    InvDate = .Range("F17")



    End With


    With BatchSheet
    .Cells(oRow, "A") = Account
    .Cells(oRow, "B") = InvNum
    .Cells(oRow, "C") = InvDate

    Do

    myRange = _
    InvSheet.Range(InvSheet.Cells(iRow, "B"), InvSheet.Cells(iRow, "K")).Address _
    & "," & InvSheet.Cells(iRow, "AB").Address
    InvSheet.Range(myRange).Copy

    'InvSheet.Range(InvSheet.Cells(iRow, "B"), InvSheet.Cells(iRow, "K")).Copy
    BatchSheet.Cells(oRow, "D").PasteSpecial xlPasteValues
    iRow = iRow + 1
    oRow = oRow + 1
    Loop Until IsEmpty(InvSheet.Cells(iRow, "B")) Or InvSheet.Cells(iRow, "B") = 0
    End With
    Application.CutCopyMode = False
    ActiveWorkbook.Close True 'save changes and close
    End Sub


    -------------END OF CODE -----------------------------


    What I need is for the information from

    .Cells(oRow, "A") = Account
    .Cells(oRow, "B") = InvNum
    .Cells(oRow, "C") = InvDate

    to be repaeated for each of the iRow's from 1-20 or until the loop ends:

    These three values remain the same for each of the invoice lines, whereas the data in each invoice lines differs. I hope I am making sense ....

    I tried changing the code so it repeats, but without success. Hope you will have the time to help out with this one as well. And again thanks for your earlier response. I now know two ways of solving that problem, and I am learning.

    Talāt

  4. #4
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,262
    Code:
    Sub InvoiceToBatch()
       'Copies the current invoice in the INVOICE TEMPLATE sheet of
       'this workbook to stocksbatch.xls
       Dim myRange      As String
       Dim Account       As String
       Dim InvDate       As Date
       Dim InvNum        As Long     'invoice "FR" number
       Dim InvSheet      As Worksheet
       Dim BatchSheet    As Worksheet
       Dim NextRow       As Long     'the next available invoice row on the batch sheet
       Dim oRow          As Long     'row number on BatchSheet
       Dim iRow          As Long     'row number on InvSheet
       
       Set InvSheet = ThisWorkbook.Worksheets("INVOICE TEMPLATE")
       
        Workbooks.Open Filename:="T:\Amnesty\stocksbatch.xls"
        Set BatchSheet = ActiveWorkbook.Worksheets("Sheet1")
       'NextRow = BatchSheet.Range("D65536").End(xlUp).Row + 1
       'oRow = NextRow
       oRow = BatchSheet.UsedRange.Rows.Count + 1
       iRow = 20
     
          
        Do
             
          myRange = InvSheet.Range(InvSheet.Cells(iRow, "B"), InvSheet.Cells(iRow, "K")).Address & _
          "," & InvSheet.Cells(iRow, "AB").Address
          InvSheet.Range(myRange).Copy
    
          'InvSheet.Range(InvSheet.Cells(iRow, "B"), InvSheet.Cells(iRow, "K")).Copy
          BatchSheet.Cells(oRow, "D").PasteSpecial xlPasteValues
        
          InvSheet.Range("E5").Copy  'Account
          BatchSheet.Cells(oRow, "A").PasteSpecial xlPasteValues
           InvSheet.Range("K2").Copy  'InvNum
          BatchSheet.Cells(oRow, "B").PasteSpecial xlPasteValues
           InvSheet.Range("F17").Copy 'InvDate
          BatchSheet.Cells(oRow, "C").PasteSpecial xlPasteValues
        
          iRow = iRow + 1
          oRow = oRow + 1
       Loop Until IsEmpty(InvSheet.Cells(iRow, "B")) Or InvSheet.Cells(iRow, "B") = 0
       Application.CutCopyMode = False
       ActiveWorkbook.Close True           'save changes and close
    End Sub

Similar Threads

  1. VS.Net and Code Behind
    By Chaitanya Marvici in forum ASP.NET
    Replies: 6
    Last Post: 07-21-2003, 09:15 AM
  2. Faster SQL Code
    By Edwin in forum Database
    Replies: 2
    Last Post: 03-06-2002, 11:58 AM
  3. Select Distinct
    By Cary R in forum Database
    Replies: 24
    Last Post: 09-24-2001, 02:40 PM
  4. Replies: 0
    Last Post: 01-16-2001, 05:07 PM
  5. Replies: 2
    Last Post: 04-13-2000, 01:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center