problem clear header when using currentregion and need merging for two columns

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
174
Office Version
  1. 2019
Platform
  1. Windows
I have this code
VBA Code:
Option Explicit
Sub CopyRangeFromSetFolder()

Dim desWS As Worksheet, wb As Workbook, lRow As Long
Dim wbNm As String, Fld As String

Application.ScreenUpdating = False

Set desWS = ThisWorkbook.Sheets("Sheet1")

desWS.Range("A2").CurrentRegion.ClearContents

Fld = ThisWorkbook.Path & "\"
    wbNm = Dir(Fld & "*.xls*", vbNormal)

    Do While wbNm <> ""
        If wbNm <> ThisWorkbook.Name Then
        
            With GetObject(Fld & wbNm)
                With .Sheets("MATCH")
                    lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                    .Range("A2:E" & lRow).Copy
                    
                    desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                End With
                .Close False
            End With
        End If

        wbNm = Dir()
    Loop

Application.ScreenUpdating = True
End Sub
I need two thing:
1- fixing problem clear the headers when using this line
VBA Code:
desWS.Range("A2").CurrentRegion.ClearContents
should clear from row 2 not the headers
2- I need add procedure to merging numirc values in columns D,E based on column B (it means there are many dupliactes items in columnB should merging with summing values for column D,E.
 
You can get a lot of the VBA code snippets needed for these pieces using the Macro Recorder.
actually I have not ever used Macro Recorder , in reality I used ready procedures from the internet . so I'm afraid my way is wrong when using Macro Recorder .
I will try based on your guiding .
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
actually I have not ever used Macro Recorder , in reality I used ready procedures from the internet . so I'm afraid my way is wrong when using Macro Recorder .
I will try based on your guiding .
You just turn on the Macro Recorder, and record yourself performing the actions manually, then stop the recorder.
Then, view the code you just recorded.

This is very useful in getting code snippets to cut and paste into your code.
Sometimes you need to edit them slightly to make them more dynamic.
It is is great way to help you get started in writing VBA code,

Give it a shot, see how far you get, and post back here when you need help.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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