I am new to using VBA code. I have a workbook with manually created tables as well as pivot tables. For each worksheet in this book, I want to hard-code the data into a new workbook and save it as the name of the worksheet that is being hard-coded. I have the code below. It is saving each workbook properly, but the contents of the workbook are not correct. It is hard-coding the contents of the first worksheet in my original workbook every time. I've tried to set the next ActiveSheet at the end of the code but it fails. Did I mention I am not a programmer? Please help!
Sub Splitbook()
Dim path As String
Dim dt As String
path = CreateObject("WScript.Shell").specialfolders("Desktop") & "\SLF\ "
Call Shell("explorer.exe" & " " & path, vbNormalFocus)
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ActiveSheet.UsedRange.Select
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveWorkbook.SaveAs path & ws.Name, Excel.XlFileFormat.xlOpenXMLWorkbook
ActiveWorkbook.Close SaveChanges:=False
Next ws
End Sub
You already have the code looping through the sheets of your workbook so you do not need to copy from the ActiveSheet but from the ws worksheet variable.
You also do not need to use Select to get data copied.
Dim ws As Worksheet
Dim newBook As Workbook
For Each ws In ThisWorkbook.Worksheets
ws.UsedRange.Copy
Set newBook = Workbooks.Add
With newBook.Worksheets(1).Range("A1")
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End With
newBook.SaveAs Path & ws.Name, Excel.XlFileFormat.xlOpenXMLWorkbook
newBook.Close SaveChanges:=False
Next ws
Related
I have looked thoroughly at the current answers for this problem and none of them have fixed mine.
The operation is simply copying a selection of a sheet and copying to a new book called budget.
Again I have tried multiple different ways of doing the same thing and none of them seem to change this error. The select method works,it only breaks when I try to paste.
Code:
Range("B3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Dim wk As Workbook
Set wk = Workbooks.Add
wk.SaveAs FileName:=ThisWorkbook.path & "\" & "Budget.xlsx"
wk.Activate
wk.Unprotect
wk.Worksheets("Sheet1").Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Thanks,
If you try exactly the steps in the code manually, you will find it also fails. The issue is that inbetween copying the cells and trying to paste them, you are creating a new workbook and saving it. This cancels copy/paste mode (i.e. the "marching ants" around the copied range disappear), so there is nothing to paste.
The solution is to not use Selection at all. In general any time you find yourself writing .Select in VBA you're doing it wrong (see this question for detail). Here is how I would re-write your code:
Dim wk As Workbook
Set wk = Workbooks.Add
wk.SaveAs Filename:=ThisWorkbook.Path & "\" & "Budget.xlsx"
ThisWorkbook.Range("B3").CurrentRegion.Copy
wk.Worksheets("Sheet1").Range("B3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Try the code below (explanation inside the code as comments):
Dim wk As Workbook
Set wk = Workbooks.Add
wk.SaveAs Filename:=ThisWorkbook.Path & "\" & "Budget.xlsx"
wk.Activate
wk.Unprotect
' have the Copy>>Paste section together
Dim LastCol As Long
Dim LastRow As Long
' you never mentioned which sheet to copy from, I used the first index
With ThisWorkbook.Sheets(1)
LastCol = .Range("B3").End(xlToRight).Column
LastRow = .Range("B3").End(xlDown).Row
.Range("B3", .Cells(LastRow, LastCol)).Copy ' <-- Copy without Select
End With
' Paste without Select
wk.Worksheets("Sheet1").Range("B3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I'm attempting to write a macro that will copy a range of cells from a sheet, paste them into a sheet ("Bulksheet") that will contain all pasted data, then move on to the next tab after the first sheet. This needs to be done for 40+ tabs. Luckily, the data is in the same place in each tab, including the Bulksheet tab.
I can easily get this to apply to one tab, but returning to the first active tab and then moving on to the next is giving me no end of trouble.
Ex. code (shortened to the crucial bit). At the bottom where Next is would be where I need to move to the next sheet and do the same function, returning to "Bulksheet" and pasting in the next empty cell in column C.:
Sub
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Range("C100:F103").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Bulksheet").Select
Range("D1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next
End Sub
Try looping through the sheets using an index value instead.
Sub
Dim i as integer
For i = 1 to worksheets.count
sheets(i).Activate
if activesheet.name <> "Bulksheet" then
Range("C100:F103").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Bulksheet").Select
Range("D1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
end if
Next
End Sub
Try this:
Sub CopyToBulksheet()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Bulksheet" Then
ws.Activate
Range("C1:F10").Copy
Sheets("Bulksheet").Select
Range("D" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next
End Sub
I modified a piece of code that opens a .csv file, copies a selected range and pastes it in a selected cell in an excel worksheet.
I am having trouble selecting the next range in the .csv file to paste in the excel file. It only works for the first range (E2:E25).
I want it to select the next range (B2:B25) from the .csv file, copy/paste, but it only selects from the excel file. How do I fix that? Thanks.
Option Explicit
Sub copy2()
Dim FilesToOpen
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim newSheet As Worksheet
FilesToOpen = Application.GetOpenFilename(Title:="Text Files to Open")
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen, Format:=4)
wkbTemp.Sheets(1).Cells.copy
Range("E2:E25").Select
Selection.copy
Windows("Petty Cash Form (test).xls").Activate
Range("H10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B2:B25").Select
Selection.copy
Windows("Petty Cash Form (test).xls").Activate
Range("B10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
wkbTemp.Close
End Sub
In your VBA macro add the line: wkbTemp.Sheets(1).Activate (see 'Activate Source Worksheet):
Option Explicit
Sub copy2()
Dim FilesToOpen
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim newSheet As Worksheet
FilesToOpen = Application.GetOpenFilename(Title:="Text Files to Open")
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen, Format:=4)
wkbTemp.Sheets(1).Cells.copy
Range("E2:E25").Select
Selection.copy
Windows("Petty Cash Form (test).xls").Activate
Range("H10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Activate Source Worksheet
wkbTemp.Sheets(1).Activate
Range("B2:B25").Select
Selection.copy
Windows("Petty Cash Form (test).xls").Activate
Range("B10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
wkbTemp.Close
End Sub
Hope this may help.
This is somewhat of a rudimentary answer but I would suggest following the VBA hierarchy in order to select specific ranges.
workbook.worksheet.range
I think if you defined the worksheets then used
wrktemp.Sheets("sheet name").Range("E2:E25")
I believe this will help in pursuit of your problem
I have a input workbook, from which I will copy first row and paste it in another excel workbook (wbET). This I have to do for the number of rows in my input workbook.
I have code for first row. I have to do it for all the rows. can any one help me out
code:
Option Explicit
Dim wbIP As Workbook
Dim wbJT As Workbook
Dim wbET As Workbook
Dim mypathET As String
Dim mypathJT As String
Dim mypathIP As String
Dim vals As Variant
Sub tool()
mypathET = "C:\Documents and Settings\madinenih\Desktop\PremiumCalcutionTool"
mypathJT = "C:\Documents and Settings\madinenih\Desktop\Japancalculationtool"
mypathIP = "C:\Documents and Settings\madinenih\Desktop\A01"
'
'Set wbJT = Workbooks.Open(Filename:=mypathJT)
Set wbIP = Workbooks.Open(Filename:=mypathIP)
wbIP.Activate
'Rows("1:1").Select
'Selection.Copy
wbIP.Sheets("A01").Range("A1:IU1").Copy
Set wbET = Workbooks.Open(Filename:=mypathET)
wbET.Activate
wbET.Sheets("Input file data").Range("A3:IU3").PasteSpecial
'wbET.Activate
Application.Run (wbET.Name & "!run1")
Call Createexcels
wbIP.Activate
'Rows("1:1").Select
'Selection.Copy
wbIP.Sheets("A01").Range("A1:IU1").Copy
Set wbJT = Workbooks.Open(Filename:=mypathJT)
wbJT.Activate
wbJT.Sheets(2).Range("A5:IU5").PasteSpecial
'Application.Run (wbJT.Name & "!run1")
Call openexcel
Call compare
End Sub
Sub Createexcels()
Dim NewBook As Workbook
vals = "test"
Set NewBook = Workbooks.Add
NewBook.SaveAs Filename:=vals
'Workbooks("Whatever.xlsx").Worksheets("output").Range("A1:K10").Copy
'NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
' NewBook.Worksheets("Sheet1").Activate
wbET.Activate
wbET.Sheets("Calculation").Range("L2:L41").Copy
NewBook.Worksheets("Sheet1").Activate
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wbET.Activate
wbET.Sheets("Calculation").Range("L44:L61").Select
Application.CutCopyMode = False
Selection.Copy
Windows(vals).Activate
Range("A44").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wbET.Activate
wbET.Sheets("Calculation").Range("L64:L69").Select
Application.CutCopyMode = False
Selection.Copy
Windows(vals).Activate
Range("A63").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wbET.Activate
wbET.Sheets("Calculation").Range("L72:L81").Select
Application.CutCopyMode = False
Selection.Copy
Windows(vals).Activate
Range("A70").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("A:A").EntireColumn.AutoFit
NewBook.Save
End Sub
You need to loop through all of the rows in your Input worksheet. To start you need to get the last used row in our input sheet.
' use this in your loop. It looks like you are starting on row 3 of your input sheet.
Dim LastRow as Long
LastRow = Activesheet.Cells(Activesheet.Rows.Count, 2).End(xlUp).Row
Dim i as Long
For i = 3 to LastRow
' Code to copy each row goes here
' You will need to change how you are referencing your range
wbET.Sheets("Input file data").Range("A" & i & ":IU" & i).PasteSpecial
Next i
you need to find the last row and last column
using last row and column as reference, you can make the copy paste method easily
I am trying to write a macro which pastes over the formulae within certain-named worksheets with their values, thereby making them exportable. I have successfully got the macro to dupe and rename the worksheets, but can't get the copy/paste to run on them as I would like.
Currently my macro copies all visible worksheets except one specified ("Dashboard") and then renames them, replacing " (2)" with "_VARIABLES". So far so good. It is then supposed to overwrite formulae in the newly created worksheets with values; this part does not work.
Here is the entire code:
Private Sub testestssss()
Dim ws As Worksheet
'Copy all visible worksheets except "Dashboard" to the end
For Each ws In Sheets
If ws.Name = "Dashboard" Then
Else
If ws.Visible Then ws.Copy after:=Worksheets(Worksheets.Count)
End If
Next
'Rename all "wk * (2)" sheets to "wk *_VARIABLES"
For Each ws In Sheets
If ws.Name Like "* (2)" Then
ws.Name = Replace(ws.Name, " (2)", "_VARIABLES")
End If
Next
'Overwrite all "wk *_VARIABLES" formulae with values
For Each ws In Sheets
If ws.Name Like "*_VARIABLES" Then
Columns("A:B").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next
End Sub
So all the above works up until the 'overwrite all "wk *_VARIABLES" formulae with values' part. That appears to be ineffective.
Any help will be gratefully appreciated!
Thank you.
You keep selecting the column of the active sheet. This should do what you expect:
For Each ws In Sheets
If ws.Name Like "*_VARIABLES" Then
ws.Select
Columns("A:B").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next
Alternatively, you can simply write this (no need to select and it runs a bit faster without them):
For Each ws In Sheets
If ws.Name Like "*_VARIABLES" Then
ws.Columns("A:B").Copy
ws.Columns("A:B").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
Next
And finally to save a few keystrokes:
For Each ws In Sheets
If ws.Name Like "*_VARIABLES" Then
With ws.Columns("A:B")
.Copy
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End With
End If
Next
And don't forget to add the following statement after the copy/paste section:
Application.CutCopyMode = False
to keep things clean.