+ Reply to Thread
Results 1 to 33 of 33

Copy and Paste cell value in for loop

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-16-2012
    Location
    Ontario Canada
    MS-Off Ver
    Excel 2010
    Posts
    124

    Re: Copy and Paste cell value in for loop

    The variable workbookName was never defined so it will be ""
    You are trying to copy a sheet called "" which is throwing an error.
    Also, I think to copy the Sheet you would have to go Sheets(SheetName).Cells.copy

    Try Sheets(SheetName).Copy first but if that doesnt work, you will need the ".Cells"

  2. #2
    Registered User
    Join Date
    02-11-2013
    Location
    eugene, or
    MS-Off Ver
    Excel 2011
    Posts
    74

    Re: Copy and Paste cell value in for loop

    Hmm, I keep finding new ways to break the script... This might be easier... This is how it was originally. This just copies the entire column instead of cell by cell. The Issue I have with this is that it copies blank cells and cells with "?"'s in them. My goal is to have it copy the invoice number from the cell in column C and past it into a new sheet to be saved as a new workbook, and where that invoice number exists I want it to copy the cell with the total hours in column G of the same row... I know a lot about this code is unnecessary, I am VERY new to VBA and trying to frankenstien this code together from existing scripts and my basic knowledge....

    Function SheetExists(sheetName As String)
       Dim sheet As Worksheet
          For Each sheet In Sheets
             If sheet.Name = sheetName Then
                SheetExists = True
                Exit Function
             Else
                SheetExists = False
             End If
           Next
       End Function
       
       
    Function SaveWorkbook(workbookName As String)
       Dim filePath As String
        Application.DisplayAlerts = False
       filePath = "C:\Users\chrish\Desktop\Job Costing\dump\output.xlsx"
       Sheets(workbookName).Copy
       ActiveWorkbook.SaveAs Filename:=filePath
       ActiveWorkbook.Close
       Application.DisplayAlerts = True
    
    End Function
    
    Sub CreateWorkbooks()
       Dim newSheet As Worksheet, timecodeSheet As Worksheet
       Dim cell As Object
       Dim regionRange As String
    
       Set timecodeSheet = Sheets("Sheet1")
       Application.ScreenUpdating = False
    
       regionRange = "C4:" & timecodeSheet.Range("C4").End(xlDown).Address
    
       For Each cell In timecodeSheet.Range(regionRange)
          If SheetExists(cell.Value) = False Then
             Sheets.Add After:=Sheets(Sheets.Count)
             Set newSheet = ActiveSheet
             timecodeSheet.Range("C4").EntireColumn.Copy newSheet.Range("A1")
             timecodeSheet.Range("G4").EntireColumn.Copy newSheet.Range("B1")
             newSheet.Name = cell.Value
             SaveWorkbook (cell.Value)
             Application.DisplayAlerts = False
             newSheet.Delete
             Application.DisplayAlerts = True
          End If
        Next cell
    
       MsgBox "All workbooks have been created successfully"
    
       Application.ScreenUpdating = True
    
    End Sub
    I attached the file as well.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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