Finding last column, pasting data

John.McLaughlin

Board Regular
Joined
Jul 19, 2011
Messages
169
I am reusing/converting old code that found the last row.

The old code is used on my List of goods and services. When the quantity column is greater than zero, it summarizes several columns on the worksheet “invoice”

I am trying to modify that code to instead summarize one column from the list of goods and services (ITEM NO/COL I) and paste it transposed across row A of the new worksheet “packlist”.

Specifically, the code stops at;

Code:
 LastColumn = DstWks.Cells(Column.Count, "1").End(xlUp).Column


Code:
Sub PostPackList()
 
    Dim DstRng As Range
    Dim DstWks As Worksheet
 
'    Dim LastRow As Long
    Dim LastColumn As Long
 
    Dim N As Long, r As Long
    Dim SrcRng As Range
    Dim SrcWks As Worksheet
   
    Application.ScreenUpdating = False
    Sheets("PACKLIST").Select
     
   
      ' Assign the Worksheets
        Set SrcWks = Worksheets("SERVICES")
        Set DstWks = Worksheets("PACKLIST")
       
      ' Get all cells in the Source Range starting with row 2
       
        Set SrcRng = SrcWks.Range("I2:I2")
        LastRow = SrcWks.Cells(Rows.Count, "I").End(xlUp).Row
        If LastRow < SrcRng.Row Then Exit Sub Else Set SrcRng = SrcRng.Resize(LastRow - SrcRng.Row + 1, 1)
       
      ' Find the next empty column in the Destination Range starting at column A
        Set DstRng = DstWks.Range("A1")
        LastColumn = DstWks.Cells(Column.Count, "1").End(xlUp).Column
        Set DstRng = IIf(LastColumn < DstRng.Column, DstRng, DstRng.Offset(LastColumn - DstRng.Column + 1, 0))
       
          ' Copy the Source cells to the next empty Destination column if the Source Cell in "A" is not empty
            For r = 1 To SrcRng.Rows.Count
                If SrcRng.Cells(r, "A") <> "" Then
                   SrcRng.Rows(r).Copy DstRng.Offset(N, 0)
                   N = N + 1
                End If
            Next r
  
 Sheets("PACKIST").Select
       

ActiveWorkbook.Save
Application.CutCopyMode = False
Application.ScreenUpdating = True
 
End Sub

Thanks in advance, I am barely above newbie, but a really good neighbor ?
 
Even though Fluff has given you your answer, as I said I would post something your line should read

Code:
LastColumn = DstWks.Cells(1, DstWks.Columns.Count).End(xlToLeft).Column
or
Code:
LastColumn = DstWks.Cells(1, Columns.Count).End(xlToLeft).Column

The DstWks.Columns.Count in the first code above is really a bit OTT as it would only come into play if DstWks referenced a different workbook and sheet (and really only if the workbook was an old version of Excel).

Just to try and clear up your confusion the syntax for Cells is
Rich (BB code):
Cells(row number (without quotes), Column number (without quotes) or column letter (with quotes))
 
Last edited:
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Thanks gentlemen, the code works fine. I mis-stated my objective in the subject line, my apology.

I am trying to transpose the data found in my vertical column, and paste to a new worksheet horizontally in Row 1 at column A.

excel-diagram-001.png


* Note to self, make image smaller next time.
 
Upvote 0
Try changing
Code:
SrcRng.Rows(r).Copy DstRng.Offset(N, 0)
to
Code:
SrcRng.Rows(r).Copy DstRng.Offset(,N)
 
Upvote 0
Ugh, It pasted #ref ! error when encountering a formula. I've tried adding

Code:
 For r = 1 To SrcRng.Rows.Count                If SrcRng.Cells(r, "A") <> "" Then
                   SrcRng.Rows(r).Copy DstRng.Offset(, N).value
                   N = N + 1

and

Code:
For r = 1 To SrcRng.Rows.Count
                If SrcRng.Cells(r, "A") <> "" Then
                   SrcRng.Rows(r).Copy DstRng.Offset(, N).PasteSpecial(Paste:=xlValues)
                   N = N + 1


Going back and removing the dozen or so formulas on the source worksheet will create double entries that will be more confusing, instead of using the established "formulated" prefix to the item number. (The formulated item number denotes package deals we advertise)

Is there a work around? Or do I start over with new item numbers?

Ignorance is NOT bliss!
 
Last edited:
Upvote 0
Try
Code:
SrcRng.Rows(r).Copy 
DstRng.Offset(,N).PasteSpecial xlValues
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,595
Members
452,656
Latest member
earth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top