Attempting to make this a working code instead of a rigid recorded macro

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
Maybe someone could take a look at this code and tell me if I’m on the right path. I am trying to open a file we export data to, do a text to columns on one column, remove duplicates based on two criteria, sort the file, then copy and paste two different regions of the worksheet into my worksheet. Not sure if I got it right as I am working on this on my phone but will try it later on the computer. Any help or guidance is greatly appreciated, thanks.

Code:
[COLOR=#454545][FONT=UICTFontTextStyleBody]
[/FONT][/COLOR]
[COLOR=#454545][FONT=UICTFontTextStyleBody]
[/FONT][/COLOR]
[COLOR=#454545][FONT=UICTFontTextStyleBody]Sub ImportAllData()

' ImportData Macro
[FONT=UICTFontTextStyleBody]
DIM wb AS Workbook: Set wb = Thisworkbook
[/FONT][FONT=UICTFontTextStyleBody]DIM df AS Workbook: Set df = 1.xls[/FONT]
[FONT=UICTFontTextStyleBody]
[/FONT]
[FONT=UICTFontTextStyleBody]DIM ws AS Worksheet: Set ws = wb.sheets(“Today”)[/FONT]
[FONT=UICTFontTextStyleBody]DIM ds AS Worksheet: Set ds = df.Activesheet[/FONT]
[COLOR=#000000]
[/COLOR]
'  This section removed duplicates
    
    WaitingMsg.Show
    
    Application.ScreenUpdating = False
    
    Workbooks.Open Filename:="I:\Location\1.xls"        Ds.Range("E2:E" & rows.count).Select
    
    Selection.TextToColumns Destination:=Range("E2"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 9), Array(10, 1)), TrailingMinusNumbers:=True
    ds.Range("$A$1:$AJ$600").RemoveDuplicates Columns:=Array(2, 3), _
        Header:=xlYes
    Application.DisplayAlerts = False
    
    ' Sort1 Macro
    Range("I1").Select
    ds.Sort.SortFields.Clear
    ds.Sort.SortFields.Add Key:=Range("I1"), SortOn _
        :=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ds.Sort
        .SetRange Range("A2:K600")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
                
    ' This section copies the data and pastes into the Workbook
           
    ds.Range("A2:C" & rows.count).Select
        Selection.Copy
       
       ws.Range("D3").Select
       
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False         ds.Range("F2:K" & rows.count).Select
            Application.CutCopyMode = False
            Selection.Copy

      ws.Range("I3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
    df.Activate
    Application.CutCopyMode = False
    ActiveWindow.Close SaveChanges:=False
    Application.DisplayAlerts = False
   ws.Range("D3").Select
    
    Unload WaitingMsg
    
        End With
     [/FONT][/COLOR]

[COLOR=#000000][FONT=UICTFontTextStyleBody]    End Sub[/FONT][/COLOR]
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
This is a cleaned copy of the code, but I don't understand why you are opening a User form and apparently do not use it in the code.

Code:
Sub ImportAllData()
' ImportData Macro
Dim wb As Workbook: Set wb = ThisWorkbook
Dim df As Workbook
Dim ws As Worksheet: Set ws = wb.Sheets("Today")
Dim ds As Worksheet
'  This section removed duplicates
    [COLOR=#ff0000]WaitingMsg.Show
[/COLOR]    Application.ScreenUpdating = False
    Set df = Workbooks.Open("I:\Location\1.xls")
    Set ds = df.ActiveSheet
    ds.Range("E2:E" & ds.Cells(Rows.Count, 5).End(xlUp)).TextToColumns Destination:=Range("E2"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 9), Array(10, 1)), TrailingMinusNumbers:=True
    ds.Range("$A$1:$AJ$600").RemoveDuplicates Columns:=Array(2, 3), Header:=xlYes
    Application.DisplayAlerts = False
    ' Sort1 Macro
    ds.Sort.SortFields.Clear
    ds.Sort.SortFields.Add Key:=Range("I1"), SortOn _
        :=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ds.Sort
        .SetRange Range("A2:K600")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ' This section copies the data and pastes into the Workbook
    ds.Range("A2:C" & ds.Cells(Rows.Count, 1).End(xlUp).Offset(, 2)).Copy
    ws.Range("D3").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    ds.Range("F2:K" & Rows.Count).Copy
    ws.Range("I3").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    df.Close SaveChanges:=False
    Application.DisplayAlerts = False
    ws.Range("D3").Select
    [COLOR=#ff0000]Unload WaitingMsg[/COLOR]
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,080
Messages
6,053,410
Members
444,662
Latest member
AaronPMH

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