1

I have been trying to create code for the following.

  1. Data is captured on a excel spreadsheet designed as a form. The date is captured in column range B1-B48

  2. I want to transfer that to our summary spreadsheet where we hold the data for each form completed in rows. (summary sheet-testing).

  3. The data should be pasted transposed and find the next blank line and not over write any other entries.

Problems with my code are:-

  1. in my summary sheet testing the data is writing to row 49. So its usng the last cell in the 'form' to determine where to paste the data. Rather than the next empty row.

  2. the data is not being pasted transposed to the next empty line it is always over writing row 49

This is my first go at this be gentle.

Paul


Private Sub CommandButton1_Click()

ActiveSheet.Range("B1:B48").Copy
Workbooks.Open Filename:="https://catsprotection.sharepoint.com/sites/Crawley/FinanceDocuments/Adoptions/Testing Area/summary sheet - testing.xlsx"
eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
If eRow >= 1 Then eRow = eRow + 1
ActiveSheet.Cells(eRow, 1).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, Skipblanks:=False, Transpose:=True
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False

End Sub
Webbp19
  • 11
  • 3
  • Hi Webbp19. Your problem is just that the code is getting the "last line" from the form sheet and the not from the summary sheet as you intend. So the code needs to switch to the summarysheet temporarily, get the last row there using code similar to that which you already have and then switch back to the form sheet and do the copying. You may benefit from using "explicit" sheet names rather than "activesheet" to make sure the code is doing what you think its doing. – Paul Jun 28 '18 at 07:56
  • Any chance of helping out Paul. How do I add in the code to switch to the summary sheet please. Also to make the sheet names explict if I cnafe the name of the worksheet to say 'datainput' do I then change the code to say datainput or datainputsheet? Appreciate the help – Webbp19 Jun 29 '18 at 10:52
  • Thanks for the advice. but could I be cheeky and ask you to write the like of code that i need to switch to the summary sheet please. – Webbp19 Jun 29 '18 at 11:27

1 Answers1

1

The issue is that you don't specify a concrete workbook or worksheet. Avoid using ActiveWorkbook and ActiveSheet this is not very reliable and can bring up different results. Instead always define a concrete sheet (eg by its name). Also see How to avoid using Select in Excel VBA.

Also I recommend to use Option Explicit and declare all your variables properly to avoid any issues.

Option Explicit

Private Sub CommandButton1_Click()
    Dim DestWb As Workbook 'define destination workbook
    Set DestWb = Workbooks.Open(Filename:="https://catsprotection.sharepoint.com/sites/Crawley/FinanceDocuments/Adoptions/Testing Area/summary sheet - testing.xlsx")

    Dim DestWs As Worksheet 'define destination worksheet
    Set DestWs = DestWb.Sheet1

    Dim eRow As Long 'determine last row in destination worksheet
    eRow = DestWs.Cells(DestWs.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    If eRow >= 1 Then eRow = eRow + 1

    ThisWorkbook.Worksheets("MyFormSheet").Range("B1:B48").Copy 'copy directly before paste
    DestWs.Cells(eRow, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, Skipblanks:=False, Transpose:=True

    Application.CutCopyMode = False

    DestWb.Close SaveChange:=True 'close and save destination workbook
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • Peh thanks for your help and perhaps I dont know enough about VB Excel programming. I am sure this is very simple to you but not for me. Perhaps 61 year olds should not attempt this. (smile). Can I be cheeky and say. the destination work book file is https://catsprotection.sharepoint.com/sites/Crawley/FinanceDocuments/Adoptions/Testing Area/summary sheet - testing.xlsx. – Webbp19 Jun 29 '18 at 11:18
  • @Webbp19 was this a question? If yes, then I didn't get the point here what your issue is. – Pᴇʜ Jun 29 '18 at 11:21
  • The worksheet is called - Summary. The form where the data is coming from is https://catsprotection.sharepoint.com/sites/Crawley/FinanceDocuments/Adoptions/Testing Area/Copy of HFQ Questionnaire_crawley_13-02-18 - debug.xls and the worksheet is called Datainput. How would your recommended code look please. I will get better at this and can see the potential of knowing more VB – Webbp19 Jun 29 '18 at 11:26
  • @Webbp19 use your filename after `Filename:=` and `Set DestWs = DestWb.Worksheets("Datainput")` – Pᴇʜ Jun 29 '18 at 11:28
  • If your question is solved please mark this answer as solution. – Pᴇʜ Jun 29 '18 at 16:19