combine two codes

Ali M

Active Member
Joined
Oct 10, 2021
Messages
288
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi
I want combining two codes
first
VBA Code:
Sub test()
   
    Dim OUTrng As Range
    Set wsOUT = Sheets("OUT")
    activesheet.Range("A1").CurrentRegion.Copy wsOUT.Range("A1")
    Set OUTrng = activesheet.Range("A1").CurrentRegion
    With OUTrng
        .AutoFilter 5, ""
        .AutoFilter 6, ""
        Application.DisplayAlerts= False
        .Offset(1).SpecialCells(xlCellTypeVisible).Delete
        Application.DisplayAlerts= True
        .AutoFilter
    End With
End Sub
second
Code:
Sub copysheet()
    Dim ws As Worksheet, StrPath As String
    Application.DisplayAlerts = False
    StrPath = ActiveWorkbook.Path & "\"
    Set ws = MONTHLY
        ws.Copy
        wb_name = ws.Name
        ActiveWorkbook.SaveAs Filename:= _
            StrPath & " RTP " & Format(Date, "MM-DD-yyyy") & ".xlsx", FileFormat:=51
        ActiveWorkbook.Close
    Application.DisplayAlerts = True
End Sub
so the open file should search for the empty or zero value for columns E,F together if so then should delete it and save file as xlsx with same formatting and borders.
any help will be appreciated .
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Perhaps something like this (not tested)
VBA Code:
Sub combined()
    Dim OUTrng As Range
    Dim ws As Worksheet, StrPath As String
    '    Set wsOUT = Sheets("OUT")
    '    ActiveSheet.Range("A1").CurrentRegion.Copy wsOUT.Range("A1")
    Set OUTrng = ActiveSheet.Range("A1").CurrentRegion
    With OUTrng
        .AutoFilter 5, ""
        .AutoFilter 6, ""
        Application.DisplayAlerts = False
        .Offset(1).SpecialCells(xlCellTypeVisible).Delete
        Application.DisplayAlerts = True
        .AutoFilter
    End With

    Application.DisplayAlerts = False
    StrPath = ActiveWorkbook.Path & "\"
    Set ws = ActiveSheet                              'MONTHLY
    ws.Copy
    'wb_name = ws.Name
    ActiveWorkbook.SaveAs Filename:=StrPath & " RTP " & Format(Date, "MM-DD-yyyy") & ".xlsx", FileFormat:=51
    ActiveWorkbook.Close False
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
thanks for this trying . it just saves as in orginal data in open file without filter as in first code.
 
Upvote 0
Your code is really unclear, making it difficult to determine what's going where and what should be acted on.

You've got data being copied from active sheet to wsOUT, then action happening on active sheet.
You set ws to MONTHLY then take action on active sheet.

Try to be explicit with your references and qualifiers, then it will be easier to combine.

In answer to the 'how to combine?"

VBA Code:
Sub RunBoth()
test
copysheet
End Sub

If you have multiple separate actions it is generally wise to keep them separate so you can call each independently.
 
Upvote 0
@dave3009 my goal when save the file as xlsx should filter data based on first code with keep orignal data in open file without any change .
 
Upvote 0
I tested it and it works ok. The blank Col E/F rows were deleted and a new file was saved as per your orginal code. The original file was not closed. I suspect you ran it on data that did not have blank rows to filter.

Before
tmpB.xlsm
ABCDEFGHI
1ductworkfeedbagunnookedredbudsoverlapzikuratsapolloniaromauntphotalgia
2Row 1trapposecelledskiddinghoweverhuckwarreeglottic
3Row 2pataqueobscurestkeddahshirerssiggerclartscalcar
4Row 3notabenebepaidtapsterlyoceanianperchescheduledpanels
5Row 4granchhandymikirgalleassreturnershordarianmacrouridwastland
6Row 5glumkivakendalpedaryseraglivaloursvehicle
7Row 6kestrelupholdingpersonateaffydavybludgingimaginateseawards
8Row 7carneoltranshapechampacspentinecaravansdodecantrecountalpettish
9Row 8whirredhemipodanornationkestreldeforcingvervetepeisodia
10Row 9obduracyadvancingkeddahsfiretowernibliclaurinranariumwashery
11Row 10cierzodovekiesimaginatemuggsfluxationposh
12Row 11woozyneuronistwekauyearningsmanegesobscurest
13Row 12grownerrorscampanulasharpenglochidstinne
14Row 13granulesdolmansbilleddrokpafistliketriosteum
15Row 14levecheinvolucelperituravitaceaequerendibassi
16Row 15plewchtongsdemisangchampacsbottieroutseekchylous
17Row 16mungcorntriarianamphioxiarapahobetoweredblindweedmikirpaper
18Row 17shoutersalariegotoadishclosestromauntscarflessmasonriesbeeware
19Row 18kapelledeiridunsharingrootlerecountalnitrating
20Row 19diopterszenonianakkadiantargingfoysagrias
21Row 20tarriersunvetoedfibrinoseimmergingmidcoursetetrose
22Row 21dillweedfrowsilycaskfurfuralunleftrewroughtinterhyalbaffed
Sheet13


After
tmpB.xlsm
ABCDEFGHI
1ductworkfeedbagunnookedredbudsoverlapzikuratsapolloniaromauntphotalgia
2Row 1trapposecelledskiddinghoweverhuckwarreeglottic
3Row 2pataqueobscurestkeddahshirerssiggerclartscalcar
4Row 3notabenebepaidtapsterlyoceanianperchescheduledpanels
5Row 4granchhandymikirgalleassreturnershordarianmacrouridwastland
6Row 5glumkivakendalpedaryseraglivaloursvehicle
7Row 6kestrelupholdingpersonateaffydavybludgingimaginateseawards
8Row 7carneoltranshapechampacspentinecaravansdodecantrecountalpettish
9Row 8whirredhemipodanornationkestreldeforcingvervetepeisodia
10Row 9obduracyadvancingkeddahsfiretowernibliclaurinranariumwashery
11Row 15plewchtongsdemisangchampacsbottieroutseekchylous
12Row 16mungcorntriarianamphioxiarapahobetoweredblindweedmikirpaper
13Row 17shoutersalariegotoadishclosestromauntscarflessmasonriesbeeware
14Row 21dillweedfrowsilycaskfurfuralunleftrewroughtinterhyalbaffed
Sheet13


As @dave3009 says you have a lot of elements in your code that do not make sense.
 
Upvote 0
I get the principle of what you are trying to do, but the code is unclear. For example, if you had a different workbook open and active and triggered this macro then it would perform actions on the active workbook. That is a recipe for trouble.

You need to fix that before someone can offer a robust solution.
 
Upvote 0
@dave3009 my goal when save the file as xlsx should filter data based on first code with keep orignal data in open file without any change .

The combined code only does what your original code does and your original code will not do that. It will not keep the original data unchanged because, as written, it changes the ActiveSheet data, not the Sheets("OUT") data. If you want the original data not to change then the original code must be changed. For example:

VBA Code:
Sub combined()
    Dim OUTrng As Range
    Dim wsOUT As Worksheet, StrPath As String

    Set wsOUT = Sheets("OUT")
    wsOUT.UsedRange.Cells.Clear

    If wsOUT.Name = ActiveSheet.Name Then
        MsgBox "Worksheet '" & wsOUT.Name & "' is the destination worksheet. Please choose another worksheet before running this macro.  ", vbOKOnly Or vbInformation, Application.Name
        Exit Sub
    End If

    Select Case MsgBox("Source worksheet: '" & ActiveSheet.Name & "'" & vbCrLf _
                     & "Destination worksheet: '" & wsOUT.Name & "'" & vbCrLf _
                     & "" & vbCrLf _
                     & "Proceed with data copy?", vbOKCancel Or vbQuestion, Application.Name)
    Case vbCancel
        Exit Sub
    End Select

    ActiveSheet.Range("A1").CurrentRegion.Copy wsOUT.Range("A1")

    Set OUTrng = wsOUT.Range("A1").CurrentRegion
    With OUTrng
        .AutoFilter 5, ""
        .AutoFilter 6, ""
        Application.DisplayAlerts = False
        .Offset(1).SpecialCells(xlCellTypeVisible).Delete
        Application.DisplayAlerts = True
        .AutoFilter
    End With

    Application.DisplayAlerts = False
    StrPath = ActiveWorkbook.Path & "\"

    wsOUT.Copy

    ActiveWorkbook.SaveAs Filename:=StrPath & " RTP " & Format(Date, "MM-DD-yyyy") & ".xlsx", FileFormat:=51
    ActiveWorkbook.Close False
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
@rlv01 thanks for trying to fixing code , honestly I no know how deal with this code , when I change to sheet name OUT based on your version , it gives me message as in first code , I try following your instruction , but I don't understand how works , what I suppose to do?
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top