I have the code below and what I'm trying to accomplish is:
Remove all the filtered results and create a new table without the filtered results. The problem with this code is that every time when including a new sheet I have to add this line of code for it. Is there any way to workaround this? I've tried using For Each...Next statement but didn't have any success with it.
Edit: the Sheet1, Sheet2 etc. are created earlier in the macro only for this purpose.
Sub macro
Worksheets("cat").ListObjects(1).Range.SpecialCells(xlCellTypeVisible).Copy
Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Worksheets("Sheet1").Paste
Worksheets("cat").Delete
Worksheets("Sheet1").Name = "cat"
Worksheets("dog").ListObjects(1).Range.SpecialCells(xlCellTypeVisible).Copy
Worksheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Worksheets("Sheet2").Paste
Worksheets("dog").Delete
Worksheets("Sheet2").Name = "dog"
Worksheets("meow").ListObjects(1).Range.SpecialCells(xlCellTypeVisible).Copy
Worksheets("Sheet3").Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Worksheets("Sheet3").Paste
Worksheets("meow").Delete
Worksheets("Sheet3").Name = "meow"
Worksheets("bark").ListObjects(1).Range.SpecialCells(xlCellTypeVisible).Copy
Worksheets("Sheet4").Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Worksheets("Sheet4").Paste
Worksheets("bark").Delete
Worksheets("Sheet4").Name = "bark"
End Sub
Please, try the next code:
Sub macroIterateSh()
Dim sh As Worksheet, shNew As Worksheet, shName As String
For Each sh In ActiveWorkbook.Sheets
If sh.ListObjects.Count > 0 Then 'for the case of some other sheets not containing a list object...
Set shNew = Worksheets.Add(Before:=sh)
sh.ListObjects(1).Range.SpecialCells(xlCellTypeVisible).Copy
shNew.Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
shNew.Paste
shName = sh.Name: sh.Delete
shNew.Name = shName
End If
Next
End Sub
I'm extremely new to VBAs and cannot figure out how to add a value to the next row if there's already data previous row. I'm sure I'm overthinking it, but I cannot seem to figure it out. Any help would be appreciated.
Below is the macro I'm using. Not sure if I need to offset the data or maybe add an if then statement of some sort.
Sub Archive_2()
Range("A2").Select
Selection.Copy
Sheets("Campaign Rate").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Use below sub-
Sub CopyPaste()
Dim sh As Worksheet
Dim lRng As Range
Set sh = Worksheets("Campaign Rate")
Set lRng = sh.Cells(sh.Rows.Count, 1).End(xlUp)
Range("A2").Copy
lRng.Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set sh = Nothing
Set lRng = Nothing
End Sub
I am trying to copy the range of value from one sheet in excel to another. I have copied this formula from another part of my sheet that works however i cam coming up with the run time error 9.
Sub SaveJambStudEC()
'
' SaveCalcsJambEC Macro
'
Dim page As Integer
page = Cells(4, "T").Value
Range("A70:AN70").Select
Selection.Copy
Range("A71").Select
ActiveCell.Offset(page, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1:O63").Select
Selection.Copy
Sheets("10.3 JambCalcs EC").Select
Range("A1").Select
ActiveCell.Offset((page - 1) * 63, 0).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats
Sheets("9.3 Jamb Design EC").Select
Range("T5").Select
Selection.Copy
Range("N9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("T31").Value = 0
Call JambECsetDesignOptions
Call CopyJambOptiValues
Range("J9").Activate
End Sub
using the below code you will check if there is a sheet with that name. if you dont receive any message box means that there is no sheet with such name
Option Explicit
Sub test()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "10.3 JambCalcs EC" Then
MsgBox "Sheet Appears"
Exit Sub
End If
Next ws
End Sub
Note
'ThisWorkbook' refer to the workbook that the code included. If you want to clearly declare the workbook you could declare a variable 'Dim wb as Workbook' and then set the workbook 'Set wb=Workbooks("workbook name")'
im trying to create a code that can help me to paste my formulas in a range.
the range is always changing as i have to include information on a daily basis at the en of my list
the existing code almost gets the job done but6 instead of recognizing the value on cell a1 (a count of the amount of records to calculate) it pastes the information on range e1:k1
Sub Run_Formulas()
'
' Run_Formulas Macro
'
Dim As Variant
x1 As Variant
x1 = Worksheets("Raw Data").Range("A1")
'
Range("E4:K4").Select
Selection.Copy
Range("E8" & ":" & "K" & "a1").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End Sub
how can i get the macro to recognize the contents of cell a1 and to use it for my final range/
I have the following code shown below that allows me to save a copy of a workbook as an XLSX file with a file name that is different each time the code is ran. The code works to save the file correctly with the correct name. However, when it comes to the part where it should be copying and pasting the data in the new file as values it doesn't do it to the new file, only the original. My goal is to have a copy of the original file that does not have any macros or queries in it.
Can someone help create a way for the code to realize that it needs to do the copy and pasting in the new file?
Sub Macro1()
Dim PathName As String
Dim FileName As String
Dim AWorkbook As String
AWorkbook = "Operational Dashboard Worksheet"
PathName = Sheet4.Range("B7").Value
FileName = Sheet4.Range("B5").Value
Workbooks(AWorkbook).Save
Workbooks(AWorkbook).Sheets(Array("Dashboard", "Extra Details", "Worksheet", "Occupancy", "Shrinkage", _
"SL Impact", "VBA Codes")).Copy
ActiveWorkbook.SaveAs PathName & FileName & ".xlsx", FileFormat:=51
Workbooks(FileName).Activate
Sheet2.Range("Q:AD").Copy
Workbooks(FileName).Activate
Sheet2.Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet3.Range("B:AI").Copy
Workbooks(FileName).Activate
Sheet3.Range("B:AI").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet7.Range("N:AQ").Copy
Workbooks(FileName).Activate
Sheet7.Range("N:AQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet5.Range("A:G").Copy
Workbooks(FileName).Activate
Sheet5.Range("A:G").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet5.Range("AB:AS").Copy
Workbooks(FileName).Activate
Sheet5.Range("AB:AS").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet5.Range("AX:CQ").Copy
Workbooks(FileName).Activate
Sheet5.Range("AX:CQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(AWorkbook).Activate
Application.CutCopyMode = False
ActiveWorkbook.Close savechanges:=False
End Sub
Using Worksheet.Activate works, but it can get confusing swapping back and forth. Creating a reference to your Source and Destination Workbooks and sheets makes it easier (imo) to keep track of things, and it can also speed things up a little since you're just dealing with the data and not the gui.
' eg
Dim SourceBook As Wokbook
Set SourceBook = ThisWorkbook
I think your problem could be using SheetN.Range in your code to copy the data. SheetN probably looks to ThisWorkbook and not ActiveWorkbook for the data. So you are doing stuff to your original workbook.
The sheet index used in my code might not match up with your original code. You can replace the Index Sheets(Index) with the name of the sheet your trying to copy data on.
Sub Macro1()
Dim PathName As String
Dim FileName As String
Dim AWorkbook As String
AWorkbook = "Operational Dashboard Worksheet"
PathName = Sheet4.Range("B7").Value
FileName = Sheet4.Range("B5").Value
Workbooks(AWorkbook).Save
Workbooks(AWorkbook).Sheets(Array("Dashboard", "Extra Details", "Worksheet", "Occupancy", "Shrinkage", _
"SL Impact", "VBA Codes")).Copy
ActiveWorkbook.SaveAs PathName & FileName & ".xlsx", FileFormat:=51
Dim Book As Workbook
Set Book = Workbooks(FileName)
Book.Sheets(2).Range("Q:AD").Copy
Book.Sheets(2).Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(3).Range("B:AI").Copy
Book.Sheets(3).Range("B:AI").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(7).Range("N:AQ").Copy
Book.Sheets(7).Range("N:AQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(5).Range("A:G").Copy
Book.Sheets(5).Range("A:G").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(5).Range("AB:AS").Copy
Book.Sheets(5).Range("AB:AS").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(5).Range("AX:CQ").Copy
Book.Sheets(5).Range("AX:CQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Save
Application.CutCopyMode = False
ActiveWorkbook.Close savechanges:=False
End Sub
Update
I don't really use the SheetN (code name) so my understanding was not quite right. It's the name defined in the VBA editor for the sheet and when used does directly reference the sheet. (you can edit them in the VBA editor too so Sheet1->Dashboard?)
After testing it seems that when you use Workbooks(..).Worksheets(...).Copy like in your code and mine too it also copies those name to the new workbook. This is good, but you cant directly reference code names from another workbook.
Below is a modified version of my code that indirectly references them using some code I found. (not tested and not very pretty)
Sub Macro1()
Dim PathName As String
Dim FileName As String
Dim AWorkbook As String
AWorkbook = "Operational Dashboard Worksheet"
PathName = Sheet4.Range("B7").Value
FileName = Sheet4.Range("B5").Value
Workbooks(AWorkbook).Save
Workbooks(AWorkbook).Sheets(Array("Dashboard", "Extra Details", "Worksheet", "Occupancy", "Shrinkage", _
"SL Impact", "VBA Codes")).Copy
ActiveWorkbook.SaveAs PathName & FileName & ".xlsx", FileFormat:=51
Dim Book As Workbook
Set Book = Workbooks(FileName)
Dim Sheet2N As Worksheet
Set Sheet2N = GetWsFromCodeName(Book, "Sheet2")
Dim Sheet3N As Worksheet
Set Sheet3N = GetWsFromCodeName(Book, "Sheet3")
Dim Sheet5N As Worksheet
Set Sheet5N = GetWsFromCodeName(Book, "Sheet4")
Dim Sheet7N As Worksheet
Set Sheet7N = GetWsFromCodeName(Book, "Sheet7")
Sheet2N.Range("Q:AD").Copy
Sheet2N.Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet3N.Range("B:AI").Copy
Sheet3N.Range("B:AI").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet7N.Range("N:AQ").Copy
Sheet7N.Range("N:AQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet5N.Range("A:G").Copy
Sheet5N.Range("A:G").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet5N.Range("AB:AS").Copy
Sheet5N.Range("AB:AS").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet5N.Range("AX:CQ").Copy
Sheet5N.Range("AX:CQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Save
Application.CutCopyMode = False
ActiveWorkbook.Close savechanges:=False
End Sub
'http://yoursumbuddy.com/using-worksheet-codenames-in-other-workbooks/
Function GetWsFromCodeName(wb As Workbook, CodeName As String) As Excel.Worksheet
Dim ws As Excel.Worksheet
For Each ws In wb.Worksheets
If ws.CodeName = CodeName Then
Set GetWsFromCodeName = ws
Exit For
End If
Next ws
End Function
I haven't fully tested this but when I try to execute Workbooks("Stores").Activate in Immediate Pane when I know that Stores is open throws a Run-time error '9': Subscript out of range error.
If I add the file extension Workbooks("Stores.xlsx").Activate it works fine and executing ?ActiveWorkbook.Name returns Stores.xlsx.
So your:
Workbooks(FileName).Activate
Should be:
Workbooks(FileName & ".xlsx").Activate
Or you could add the extension when defining your FileName:
FileName = Sheet4.Range("B5").Value & "xlsx"
That said, you rarely need to Activate anything in VBA. For example:
Workbooks(FileName).Activate
Sheet2.Range("Q:AD").Copy
Workbooks(FileName).Activate
Sheet2.Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
could be replaced with:
Workbooks(FileName).Sheet2.Range("Q:AD").Copy
Workbooks(FileName).Sheet2.Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False