Consulting

Results 1 to 10 of 10

Thread: VBA for copying values with ActiveCell.Offset

  1. #1

    VBA for copying values with ActiveCell.Offset

    Hello all,

    I´ve problem with VBA code. details is below. I want copy values with ActiveCell.Offset, but everytime I´ve error message from Excel. Please help me. Thank you guys in advance.

    Sub CopyValues()

    Dim InputFile As Workbook
    Dim OutputFile As Workbook
    Dim Inputpath As String
    Dim Outputpath As String
    Dim Owner As String
    Dim ClaimDate As String, CustomerNo As String, PartNo As String, Description As String, finalPath As String
    Dim FolderName As String, Folder As String, Path As String
    Dim sDFolder As String

    Path = "P:\Quality\Reklamace\2021"
    Folder = ActiveCell.Offset(0, 4)
    Owner = ActiveCell.Offset(0, 8)
    FolderName = Selection.Value
    sDFolder = Path & Folder & "" & FolderName & ""
    finalPath = sDFolder & FolderName & ".xlsx"

    '## Open both workbooks first:
    Set InputFile = ActiveWorkbook
    Set OutputFile = Workbooks.Open(finalPath)

    InputFile.Activate
    Application.CutCopyMode = False
    InputFile.Selection(Owner).Copy

    OutputFile.Range("AA1").PasteSpecial

    End Sub



  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    Not enough information:
    The macro depends heavily on what is currently selected and which cell is the active cell (If more than one cell is selected a line such as FolderName = Selection.Value might cause problems).
    At least 3 cells are referred to and without knowing what's in those cells we can't determine what's wrong.

    I recommend:
    • You attach a workbook (the Inout file/ActiveWorkbook).
    • You provide the full path and filename of the xlsx file you hope to open and use as your Output file within the text of your message.
    • You tell us what is selected when you run the macro.
    • What line is giving the error and what that error says.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Hi p45cal,

    thank you for your recommendations. I´m sending request documents and informations:

    Path: P:\Quality\Reklamace\2021

    In annex I´m sending line pictures of error and in which line is


    Thank you in advance for your support.
    Attached Images Attached Images
    Attached Files Attached Files

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    OK, so now I have the file what are you wanting to copy from with InputFile.Selection(Owner).Copy?
    Again, could you tell me which cell should be selected before you run the macro?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Selected must be cell D. I want copy more values from more cells, but Active.Offset will be selected from cell "D".

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    And:
    Quote Originally Posted by p45cal View Post
    OK, so now I have the file what are you wanting to copy from with InputFile.Selection(Owner).Copy?
    Be specific - 'more values from more cells' doesn't let me write any code at all.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    I apologize..I want copy values from cells:
    C; D; E; F and L
    More precisely:
    From cell C in ActiveWorkbook to AF1 in InputFile
    From cell D in ActiveWorkbook to S3 in InputFile
    From cell E in ActiveWorkbook to AB5 in InputFile
    From cell F in ActiveWorkbook to B7 in InputFile
    From cell L in ActiveWorkbook to AA1 in InputFile

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    Quote Originally Posted by Shiftyshell View Post
    I apologize..I want copy values from cells:
    C; D; E; F and L
    More precisely:
    From cell C in ActiveWorkbook to AF1 in InputFile
    From cell D in ActiveWorkbook to S3 in InputFile
    From cell E in ActiveWorkbook to AB5 in InputFile
    From cell F in ActiveWorkbook to B7 in InputFile
    From cell L in ActiveWorkbook to AA1 in InputFile
    Output File?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    Yes..sorry

  10. #10
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    Try:
    '## Open both workbooks first:
    Set InputFile = ActiveWorkbook
    
    Set OutputFile = Workbooks.Open(finalPath)
    Set OutputSht = OutputFile.ActiveSheet    'hopefully only one sheet in that file.
    InputFile.Activate
    With ActiveCell.EntireRow
      'From cell C in ActiveWorkbook to AF1 in OutputFile:
      OutputSht.Range("AF1").Value = .Range("C1").Value
      'From cell D in ActiveWorkbook to S3 in OutputFile:
      OutputSht.Range("S3").Value = .Range("D1").Value
      'From cell E in ActiveWorkbook to AB5 in OutputFile;
      OutputSht.Range("AB5").Value = .Range("E1").Value
      'From cell F in ActiveWorkbook to B7 in OutputFile:
      OutputSht.Range("B7").Value = .Range("F1").Value
      'From cell L in ActiveWorkbook to AA1 in OutputFile;
      OutputSht.Range("AA1").Value = .Range("L1").Value
    End With
    'Close InputFile & OutputFile:
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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