Тёмный

Excel VBA Macro: Autofilter And Copy to New Workbook (Dynamic Range) 

greggowaffles
Подписаться 6 тыс.
Просмотров 17 тыс.
50% 1

Опубликовано:

 

1 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 35   
@weiyu1098
@weiyu1098 3 года назад
How to add extra codes so that we can produce 200 new workbooks if the list has 200 regions using the macro instead we click the drop down list 200 times?
@greggowaffles
@greggowaffles 3 года назад
You can create a loop that will loop through the list; creating a new workbook each time. I’ll make a video on that asap
@44bthoj44
@44bthoj44 2 года назад
@@greggowaffles I'd like to see this tutorial as well, can you please link the new video if you published it already? Thanks man!
@melissagarcia8921
@melissagarcia8921 2 года назад
Can this possible to Save Active Sheet As Multiple Workbook like you did in Excel VBA Macro: Save Active Sheet As Multiple PDFs (Based on List of Cell Values)?
@melissaobrien3812
@melissaobrien3812 2 года назад
Hi - thanks for this. This is what I need but I want to loop through the drop down instead of selecting each one individually. I looked at the other loop for video you posted but that creates PDF. So I basically need a merge of this code and the loop one to auto create all files at once for excel. Then attach that to an email per 'client' (region in your example). I tried to do this myself but got errors. Can you assist please?
@greggowaffles
@greggowaffles 2 года назад
No prob. So you want code that auto creates all files from a list and sends each file to a different email address?
@johnlaymon3
@johnlaymon3 Год назад
Thank you so much for your help! I have been watching your videos and playing with code the past two days and was able to use this and the other code you have for PDF to make a loop for this but export to Excel. The only thing different is I made the data validation list in A1 on sheet 1 and then my column for region was named organization and I changed it to be under Column A(2). Sub copy_data_2_new_book() Dim count_col As Integer Dim count_row As Integer Dim og As Worksheet Dim wb As Workbook Dim organization As String Dim i As Long Set Data = ThisWorkbook.Sheets(1) Set List = ThisWorkbook.Sheets(2) 'count numnber of regions List.Activate Count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlDown))) Data.Activate For i = 1 To Count 'updating the region name organization = List.Cells(i, 1).Text Data.Cells(1, 1) = organization Set og = Sheet1 organization = og.Cells(1, 1).Value Set wb = Workbooks.Add wb.Sheets("Sheet1").Name = organization og.Activate count_col = WorksheetFunction.CountA(Range("A2", Range("A2").End(xlToRight))) count_row = WorksheetFunction.CountA(Range("A2", Range("A2").End(xlDown))) ActiveSheet.Range("A2").AutoFilter Field:=1, Criteria1:=organization 'copies data from sheet to workbook og.Range(Cells(1, 1), Cells(count_row, count_col)). _ SpecialCells(xlCellTypeVisible).Copy wb.Sheets(organization).Cells(1, 1).PasteSpecial xlPasteValues wb.Sheets(organization).Cells(1, 1).PasteSpecial xlPasteFormats Application.CutCopyMode = False og.ShowAllData og.AutoFilterMode = False wb.Activate Cells.Select Cells.EntireColumn.AutoFit Range("A1").Select 'save and close On Error Resume Next Application.DisplayAlerts = False wb.SaveAs "/Users/To Be Sorted/" & _ organization & " " & Format(Date, "mm-dd-yyyy") & ".xlsx" Application.DisplayAlerts = True wb.Close 'savechanges:=False Next i End Sub
@kennethjensen5985
@kennethjensen5985 Год назад
I am getting an error with your code the error occurs at wb.Sheets("Sheet1").Name = region Run-time error 'error': Application-defined or object-defined error Is it possible you can post your excel
@greggowaffles
@greggowaffles Год назад
You need to use quotations around region: “region”. Im working on adding the code to the descriptions of all of my videos now
@kennethjensen5985
@kennethjensen5985 Год назад
Thanks it helped me BUT Again further down in programming there come an new error wb.Sheets(region).Cells(1, 1).PasteSpecial xlPasteValues I then again set quatations around region ug.Sheets("region").Cells(1, 1).PasteSpecial xlPasteValues again it help me and all ok BUT why do your script in the video "with no use of quotation around region" not failed ? is is because of a new VBA statement or ?
@greggowaffles
@greggowaffles Год назад
My region is a variable that’s equal to a cell on the worksheet. Can you post your entire code here? I can take a look and see what’s happening
@kennethjensen5985
@kennethjensen5985 Год назад
my code is ok now; thanks for help
@greggowaffles
@greggowaffles Год назад
@@kennethjensen5985 no prob! Glad to hear that!
@kevinpather8150
@kevinpather8150 2 года назад
Hi Greg, thanks for the video Very clear and I learn a lot and make me more interested with macro. I m trying to do the same thing as your video. That is, taking data filter it according to specific shop and save to its respective files in another folder but I need to the files to be remained in the folder. They should be updated instead and not deleted and replaced by new ones. This is so because the access of each file is shared with a list of sales people and they can access the file on their mobiles. If I do it the same way as you, when they will click on the file, they will not able to access. I am thinking that it will need to create a loop so that each is opened and each one is updated or overwrite and then the each file is closed. Can you please help me with the code as I m not able to make it work? 😥
@weiyu1098
@weiyu1098 2 года назад
add the loop , below is the code Dim i As Integer Dim og As Worksheet Dim wb As Workbook Dim tin As String Dim count_col As Integer Dim count_row As Integer i = 1 Do Until i = 2 i = i + 1 Sheets("Template").Select Set og = Sheets("Template") og.Cells(2, 3).Value = i Calculate tin = og.Cells(2, 4).Value Set wb = Workbooks.Add wb.Sheets("Sheet1").Name = tin og.Activate count_col = WorksheetFunction.CountA(Range("A6", Range("A6").End(xlToRight))) count_row = WorksheetFunction.CountA(Range("A6", Range("A6").End(xlDown))) ActiveSheet.Range("A6").AutoFilter Field:=6, Criteria1:=tin 'copies data from sheet to workbook og.Range(Cells(6, 1), Cells(count_row, count_col)). _ SpecialCells(xlCellTypeVisible).Copy wb.Sheets(tin).Cells(1, 1).PasteSpecial xlPasteValues Application.CutCopyMode = False og.ShowAllData og.AutoFilterMode = False wb.Activate Cells.Select Cells.EntireColumn.AutoFit Range("A1").Select 'save and close 'On Error Resume Next Application.DisplayAlerts = False wb.SaveAs "C:\Users\wyu\Documents\Test_2021.12\" & _ tin & " " & Format(Date, "mm-dd-yyyy") & ".xlsx" wb.Close 'savechanges:=False Application.DisplayAlerts = False Loop End Sub
@johnlaymon3
@johnlaymon3 Год назад
This didn't work for me created some errors
@qqcoisa891
@qqcoisa891 9 дней назад
Thanks
@greggowaffles
@greggowaffles 9 дней назад
Thank you so much!
@Yaktahbay
@Yaktahbay Год назад
Thanks for this, Greg! One enhancement I need is a prompt for the "region" instead of either preselecting it from a dropdown or looping through the entire list. Hopefully that's not too difficult to add.
@yazihernandez5166
@yazihernandez5166 Год назад
combined it with an Excel VBA Form where the textbox prompts for 'region' works too
@pankajkumar-ri5wv
@pankajkumar-ri5wv 2 года назад
Unable to understand as code is not visible
@karanc360
@karanc360 Год назад
Great Video Greg! Helped me automate many weekly reports! Thank you so much!
@greggowaffles
@greggowaffles Год назад
So glad to hear that! No problem!
@mayurrajude
@mayurrajude Год назад
share the code, please
@hashimmavval2956
@hashimmavval2956 3 года назад
Could I get the code. thanks
@greggowaffles
@greggowaffles 3 года назад
Sub copy_data_2_new_book() Dim count_col As Integer Dim count_row As Integer Dim og As Worksheet Dim wb As Workbook Dim region As String Set og = Sheet1 region = og.Cells(2, 6).Value Set wb = Workbooks.Add wb.Sheets("Sheet1").Name = region og.Activate count_col = WorksheetFunction.CountA(Range("A4", Range("A4").End(xlToRight))) count_row = WorksheetFunction.CountA(Range("A4", Range("A4").End(xlDown))) ActiveSheet.Range("A4").AutoFilter Field:=2, Criteria1:=region 'copies data from sheet to workbook og.Range(Cells(4, 1), Cells(count_row, count_col)). _ SpecialCells(xlCellTypeVisible).Copy wb.Sheets(region).Cells(1, 1).PasteSpecial xlPasteValues Application.CutCopyMode = False og.ShowAllData og.AutoFilterMode = False wb.Activate Cells.Select Cells.EntireColumn.AutoFit Range("A1").Select 'save and close On Error Resume Next Application.DisplayAlerts = False wb.SaveAs "C:\Users\greggowaffles\Documents\Test\" & _ region & " " & Format(Date, "mm-dd-yyyy") & ".xlsx" Application.DisplayAlerts = True wb.Close 'savechanges:=False End Sub
@marcod7393
@marcod7393 2 года назад
I have a list of regions and would like to run a macro for about 20 regions out of the data set. What code can I add to do this versus individually selecting the regions and clicking run. Thank you for the help this video was great!
@greggowaffles
@greggowaffles 2 года назад
You want to create a new tab for each value in the list in another workbook all at once?
@marcod7393
@marcod7393 2 года назад
@@greggowaffles I want to create a new workbook for 1 region but I don't want to select the region from the dropdown list and run each time (in cases where I need 20 regions/20 separate workbooks). Is it possible to add code so it will automatically run for Region 1, 5, 11, 15, etc.) and create a separate workbook for each specific one I identify in the code.
@greggowaffles
@greggowaffles 2 года назад
Yeah, you can create a list and have the macro go down the list with a loop. I’ll make a video on this
@greggowaffles
@greggowaffles 2 года назад
@@marcod7393 you can use the logic for the loop from this video: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-CtAgAlJjleU.html hope it helps!
@shyjukt
@shyjukt 3 года назад
very nice .....
@greggowaffles
@greggowaffles 3 года назад
Thanks!
@MrBumbum2812
@MrBumbum2812 3 года назад
Thank you.
@greggowaffles
@greggowaffles 3 года назад
No problem!
Далее
Excel VBA Filter Range and Copy
12:34
Просмотров 10 тыс.
Copy Filtered Data To New Sheets Excel VBA Macro
5:03
VBA Macro to Copy Data from Another Workbook in Excel
13:39
VBA Macro to Copy Data to Another Workbook
10:30
Просмотров 71 тыс.
VBA Copy Specific Column From Another Workbook
12:41
Просмотров 13 тыс.