+ Reply to Thread
Results 1 to 5 of 5

VBA to copy and paste information from c4:c25 in worksheet to specific tabs based on C4

Hybrid View

  1. #1
    Registered User
    Join Date
    08-23-2019
    Location
    UK
    MS-Off Ver
    Mac
    Posts
    9

    Unhappy VBA to copy and paste information from c4:c25 in worksheet to specific tabs based on C4

    Hi

    1. I have previously managed to copy and paste cells from one worksheet (named: Form) to another Worksheet (selected in Cell C4). I have managed to get it to copy the column into a row on the correct sheet, but I can't get it to go to the next available line.

    2. I also want it to go to another tab, where all the data will be stored (in the same workbook, tab = "Data")

    3. If one of the Cells (C28) = a specific word I want the data to also be copy and pasted into the tab i.e "B"

    this is my current code:

    Sub Copy_Data()
      Dim hh As Worksheet, exist As Boolean, h As Worksheet, sh As Worksheet
      Dim f As Range
      Set hh = Sheets("Form")
      
     If hh.Range("C4") = "" Then
        MsgBox "Enter Hospital", vbCritical
        Exit Sub
      End If
      
      If hh.Range("c5") = "" Then
        MsgBox "Enter Ward", vbCritical
        Exit Sub
      End If
      
      exist = False
      For Each h In Sheets
        If LCase(h.Name) = LCase(hh.Range("C4").Value) Then
          Set sh = h
          exist = True
          Exit For
        End If
        
      Next
      If exist = False Then
        MsgBox "The sheet does not exist", vbCritical
        Exit Sub
      End If
      
      Set f = sh.Range("A1").Find(hh.Range("c4").Value, , xlValues, xlWhole)
      If f Is Nothing Then
        MsgBox "This does not exist", vbCritical
    
      Else
        'Copy data to Site Sheet'
        'cell destination             cell origin
        sh.Cells(Rows.Count, 1, "a:u").Value = hh.Range("C4:c23").Value
       
     End If
     End Sub
    Any help is much appreciated.
    Attached Files Attached Files
    Last edited by k.stevens; 08-19-2022 at 05:24 AM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,657

    Re: VBA to copy and paste information from c4:c25 in worksheet to specific tabs based on C

    The code in the attachment is different than the listed one.

    Try instead of:
        sh.Cells(Rows.Count, 1, "a:u").Value = hh.Range("C4:c23").Value
    such code
           sh.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(1, 19).Value = Application.Transpose(hh.Range("C5:C23").Value)
    Check if column names in Ward1 are the same as rownames in Form, (at some point one item is skipped)
    So treat above as a idea not a ready made solution (I started from C5 because c4 describes sheet name and is not present in a sheet
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    08-23-2019
    Location
    UK
    MS-Off Ver
    Mac
    Posts
    9

    Re: VBA to copy and paste information from c4:c25 in worksheet to specific tabs based on C

    Thank you, do you know if there is a way to have the other 2 tabs to go from C4 to C23?

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,657

    Re: VBA to copy and paste information from c4:c25 in worksheet to specific tabs based on C

           sh.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(1, 19).Value = Application.Transpose(hh.Range("C5:C23").Value)
           sheets("Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(1, 19).Value = Application.Transpose(hh.Range("C5:C23").Value)
           if hh.Range("C18")="Urinary tract" then  sheets("Urinary Tract").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(1, 19).Value = Application.Transpose(hh.Range("C5:C23").Value)

  5. #5
    Registered User
    Join Date
    08-23-2019
    Location
    UK
    MS-Off Ver
    Mac
    Posts
    9

    Re: VBA to copy and paste information from c4:c25 in worksheet to specific tabs based on C

    Thank you all fixed now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy And Paste From One Tab To Other Tabs Based On Tab Name
    By Ummy2021 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-11-2021, 08:58 AM
  2. Replies: 6
    Last Post: 06-05-2020, 08:36 AM
  3. Replies: 0
    Last Post: 05-14-2019, 03:53 AM
  4. Copy a row from master sheet and paste it to a specific worksheet based on tab name
    By obrie499 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-31-2013, 10:45 AM
  5. Find specific cell, copy row from several tabs and paste in summary sheet
    By helnil in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-11-2012, 08:16 AM
  6. [SOLVED] Copy table information and paste sorted based on specific field.
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-05-2005, 12:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1