Transpose not adding to next empty row

Babynod

Board Regular
Joined
Aug 10, 2022
Messages
56
Office Version
  1. 365
Platform
  1. Windows
Hi All

i have a simple spreedsheet setup for my daily work priority list.

you enter data on sheet1 (Data Entry) hit submit and it goes to Sheet 2(DataBase)


VBA Code:
Private Sub CmbSubmit_Click()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim srcRng As Range, destRng As Range
    
    Set srcSht = Worksheets("Data Entry")
    Set destSht = Worksheets("Database")
    With destSht
        Set destRng = .Range("A" & Rows.Count).End(xlUp).Offset(1)
    End With
    
    With srcSht
        Set srcRng = .Range("B1:B" & .Cells(Rows.Count, "B").End(xlUp).Row)
    End With
    
    srcRng.Copy
    destRng.PasteSpecial Paste:=xlPasteValues, Transpose:=True
    
    Application.CutCopyMode = False
    
    Call Macro3
    
    Range("B1").Clear
    Range("B3", "B5").Clear

End Sub

this then has a custom sort macro which runs to organise
VBA Code:
Sub Macro3()
'
' Macro3 Macro
'
' Keyboard Shortcut: Ctrl+p
'
    ActiveWorkbook.Worksheets("Database").ListObjects("Table2").Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("Database").ListObjects("Table2").Sort.SortFields.Add _
        (Range("Table2[Status]"), xlSortOnFontColor, xlAscending, , xlSortNormal). _
        SortOnValue.Color = RGB(255, 192, 0)
    ActiveWorkbook.Worksheets("Database").ListObjects("Table2").Sort.SortFields.Add _
        (Range("Table2[Status]"), xlSortOnFontColor, xlAscending, , xlSortNormal). _
        SortOnValue.Color = RGB(255, 0, 0)
    ActiveWorkbook.Worksheets("Database").ListObjects("Table2").Sort.SortFields.Add _
        (Range("Table2[Status]"), xlSortOnFontColor, xlAscending, , xlSortNormal). _
        SortOnValue.Color = RGB(112, 173, 71)
    ActiveWorkbook.Worksheets("Database").ListObjects("Table2").Sort.SortFields.Add _
        (Range("Table2[Priority]"), xlSortOnFontColor, xlAscending, , xlSortNormal). _
        SortOnValue.Color = RGB(255, 0, 0)
    ActiveWorkbook.Worksheets("Database").ListObjects("Table2").Sort.SortFields.Add _
        (Range("Table2[Priority]"), xlSortOnFontColor, xlAscending, , xlSortNormal). _
        SortOnValue.Color = RGB(255, 192, 0)
    ActiveWorkbook.Worksheets("Database").ListObjects("Table2").Sort.SortFields.Add _
        (Range("Table2[Priority]"), xlSortOnFontColor, xlAscending, , xlSortNormal). _
        SortOnValue.Color = RGB(112, 173, 71)
    With ActiveWorkbook.Worksheets("Database").ListObjects("Table2").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

this (i think) is the code that pastes in the next blank row
VBA Code:
Sub LaptopRegisterTranspose()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim srcRng As Range, destRng As Range
    
    Set srcSht = Worksheets("Data Entry")
    Set destSht = Worksheets("Database")
    With destSht
        Set destRng = .Range("A" & Rows.Count).End(xlUp).Offset(1)
    End With
    
    With srcSht
        Set srcRng = .Range("B1:B" & .Cells(Rows.Count, "B").End(xlUp).Row)
    End With
    
    srcRng.Copy
    destRng.PasteSpecial Paste:=xlPasteValues, Transpose:=True
    
    Application.CutCopyMode = False

End Sub

my issue is when i started it was working fine, it would paste to the next empty row in my table. but now its pasting to the next empty row below my table instead.
1662692603109.png

1662692629919.png

1662692646125.png
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
my issue is when i started it was working fine, it would paste to the next empty row in my table. but now its pasting to the next empty row below my table instead.
The xlup method takes you to the bottom of the table not to the last row of the Data if there are any empty rows in the table.

Replace this:
VBA Code:
    Set destSht = Worksheets("Database")
    With destSht
        Set destRng = .Range("A" & Rows.Count).End(xlUp).Offset(1)
    End With

With this:
VBA Code:
    Set destSht = Worksheets("Database")
    Dim destLastRow As Long
    With destSht
        destLastRow = .ListObjects("Table2").ListColumns("Task").Range.Find( _
                        What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Set destRng = .Range("A" & destLastRow).Offset(1)
    End With

You can move the Dim statement to the top where the other Dim statements are (optional)
 
Upvote 0
Solution
Champion, i replaced this in both my CmbSubmit_click and my LaptopRegisterTranspose module and it works like a charm
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,139
Members
453,021
Latest member
Justyna P

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