+ Reply to Thread
Results 1 to 11 of 11

Function call returns actual code instead of copied range

Hybrid View

  1. #1
    Registered User
    Join Date
    03-22-2012
    Location
    united states
    MS-Off Ver
    Excel 2007
    Posts
    9

    Function call returns actual code instead of copied range

    I am baffled with this. I've never seen vba code pasted in the actual excel spreadsheet before. Here's the code I'm calling:
    Sub GetInputAndCopyData(strSheetName As String, strColName As String)
        Dim rngInp As Range
        
        If IsValidCell(strSheetName, strColName) Then
            Set rngInp = ActiveWorkbook.Worksheets(strSheetName).Range(strColName & "6")
            Set rngInp = Range(rngInp, rngInp.End(xlDown))
            rngInp.copy Destination:=Selection
        End If
    End Sub
    ' check that cell is valid
    ' this first checks that the sheet name is valid, then checks that cell
    ' has data in rows 1 and 2
    Function IsValidCell(ByVal wks As String, ByVal cell As String) As Boolean
    Dim r As Range
    On Error GoTo NotValid
    If IsValidSheet(wks) Then
    ' error if column does not exist
      Set r = ThisWorkbook.Worksheets(wks).Range(cell)
    ' check that column has data, must have data in at least first two rows
      If r.Value = "" And r.Offset(1, 0).Value = "" Then
        IsValidColumn = False
      Else
        IsValidColumn = True
      End If
    End If
      Exit Function
    NotValid:
      IsValidColumn = False
    End Function
    Function IsValidSheet(ByVal str As String) As Boolean
    Dim wks As Worksheet
    On Error GoTo NotValid
    ' error if sheet does not exist
      Set wks = ThisWorkbook.Worksheets(str)
      IsValidSheet = True
      Exit Function
    NotValid:
      IsValidSheet = False
    End Function
    Here's how I'm calling it:
    Call GetInputAndCopyData("Analog", "EMS Composite Name")
    Sheets("Analog").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Here's what is returned and pasted in the spreadsheet:
    Sheets("Analog").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

    Any suggestions on how to copy and paste the range instead of the actual code would be great.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Function call returns actual code instead of copied range

    Hello rlg50,

    Welcome to the Forum!

    Here is the corrected and working code. Copy and Paste this code into a standard Module, and delete the old code.
    Sub GetInputAndCopyData(ByVal strSheetName As String, ByVal strCell As String)
    
        Dim rngInp As Range
        
            If IsValidCell(strSheetName, strCell) Then
                Set rngInp = ActiveWorkbook.Worksheets(strSheetName).Cells(6, Range(strCell).Column)
                Set rngInp = Range(rngInp, rngInp.End(xlDown))
                rngInp.Copy Destination:=Selection
            End If
            
    End Sub
    
    ' check that cell is valid
    ' this first checks that the sheet name is valid, then checks that cell
    ' has data in rows 1 and 2
    Function IsValidCell(ByVal wks As String, ByVal cell As String) As Boolean
    
        Dim r As Range
        
             On Error GoTo NotValid
                If IsValidSheet(wks) Then
                  ' error if cell does not exist
                    Set r = ThisWorkbook.Worksheets(wks).Range(cell).EntireColumn
                  ' check that column has data, must have data in at least first two rows
                    If r.Cells(1, 1).Value = "" Or r.Cells(2, 1).Value = "" Then
                        IsValidCell = False
                    Else
                        IsValidCell = True
                    End If
                End If
                
      Exit Function
      
    NotValid:
      IsValidCell = False
      
    End Function
    
    Function IsValidSheet(ByVal str As String) As Boolean
    
        Dim wks As Worksheet
        
             On Error GoTo NotValid
              ' error if sheet does not exist
                Set wks = ThisWorkbook.Worksheets(str)
                IsValidSheet = True
                
      Exit Function
      
    NotValid:
      IsValidSheet = False
      
    End Function
    Last edited by Leith Ross; 03-22-2012 at 01:19 PM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-22-2012
    Location
    united states
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Function call returns actual code instead of copied range

    Is the only thing that you changed was the arguments for GetInputAndCopyData?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Function call returns actual code instead of copied range

    Hello rlg50,

    I made a few other changes to the Sub as well. Also, I changed the line below which is in GetInputAndCopyData...
    Set rngInp = ActiveWorkbook.Worksheets(strSheetName).Cells(6, Range(strCell).Column)

  5. #5
    Registered User
    Join Date
    03-22-2012
    Location
    united states
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Function call returns actual code instead of copied range

    OK. I see that now. I ran the code and I'm getting the following error: Paste method of worksheet class failed using the following code to call the above corrected code.

    all GetInputAndCopyData("Analog", "EMS Composite Name")
            Sheets("Analog").Select
            Range("D4").Activate
            ActiveSheet.Paste
            Application.CutCopyMode = False

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Function call returns actual code instead of copied range

    Hello rlg50,

    That is because ""EMS Composite Name"" is not a valid Range name. To me it looks like a column header.

  7. #7
    Registered User
    Join Date
    03-22-2012
    Location
    united states
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Function call returns actual code instead of copied range

    I may need to clarify something. What I'm doing is, adding worksheets from another workbook into my workbook, say I import a worksheet named Sheet2. From "Sheet 2", I want to copy all data in column "EMS Composite Name" and paste it in "Analog". If I run the following, again, it pastes the actual code in my worksheet.

    Call GetInputAndCopyData("Sheet2", "EMS Composite Name")
            Sheets("Analog").Select
            Range("D4").Activate
            ActiveSheet.Paste
            Application.CutCopyMode = False

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Function call returns actual code instead of copied range

    Hello rlg50 ,

    Is "EMS Composite Name" a named range, column header, what?

  9. #9
    Registered User
    Join Date
    03-22-2012
    Location
    united states
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Function call returns actual code instead of copied range

    It is a column header. So I pass the worksheet name and header name, then search for that column and then copy the data under it, return the copied data and paste it into another worksheet.

  10. #10
    Registered User
    Join Date
    03-22-2012
    Location
    united states
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Function call returns actual code instead of copied range

    The jpg attached shows a quick illustration of what I'd like to do. Also, if it's easier to just go ahead and paste the copied data instead of returning it and pasting it, that will be fine too.

  11. #11
    Registered User
    Join Date
    03-22-2012
    Location
    united states
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Function call returns actual code instead of copied range

    It might help if I told you that the header is located on row 6 and there are about 15 other names on that row, so I need to select the correct column with "EMS Composite Name" in it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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