Delete rows and columns in multiple worksheet using Access VBA - excel

Hundreds of xlsx files in a directory are imported into a MS Access 2010 Database.
I've to clean the worksheet before importing.
Question is: How to delete all rows that have no data in column A and all columns starting from the O to XFD?
The code below works but for one file a time.
All red must be deleted.
Private Sub Comand_Click()
Dim FullPath As String
Dim oXL As Object, oWb As Object, oWs As Object
FullPath = "D:\Access\_Test_XlsImport\FileName.xlsx"
Set oXL = CreateObject("Excel.Application")
Set oWb = oXL.Workbooks.Open(FullPath)
Set oWs = oWb.Sheets("Worksheet_name")
oXL.Visible = True
With oWs
.Columns("O:XFD").Delete
.Rows("xx:xx").Delete ' <---problem to identify the starting point to delete below..
End With
oWb.Save
CleanUp:
oWb.Close False
oXL.Quit
Set oWb = Nothing
Set oXL = Nothing
Set oWs = Nothing
End Sub

I would pull out the oXL variable and make it global to your module so you only open it once.
Then put the other Excel objects into the subroutine that cleans the worksheets
Something like this should work - substitute your folder for the constant
The DIR command just matches all files that match the xlsx file spec and processes each of them in the loop
Just a warning - there is no check for files that have NO Data in
column A - if that happens the program will continue until all rows
have been exhausted.
EDIT - Modified to remove all empty rows up until last non-empty cell
Option Compare Database
Option Explicit
' Use these as global
Private oXL As Object
Private Sub Comand_Click()
Const SEARCH_FOLDER As String = "C:\Databases\"
Const EXCEL_FILES As String = "*.xlsx"
Dim FullPath As String
Dim strExcelFolder As String
Dim strFilename As String
' Open Excel
Set oXL = CreateObject("Excel.Application")
oXL.Visible = True
strFilename = Dir(SEARCH_FOLDER & EXCEL_FILES)
While strFilename <> ""
ProcessExcelFile SEARCH_FOLDER, strFilename
strFilename = Dir()
Wend
CleanUp:
oXL.Quit
Set oXL = Nothing
End Sub
Private Sub ProcessExcelFile(strExcelFolder As String, strExcelFile As String)
Dim oWb As Object, oWs As Object
Dim strFullPath As String
Dim LastRow As Long
strFullPath = strExcelFolder & strExcelFile
Set oWb = oXL.Workbooks.Open(strFullPath)
Set oWs = oWb.Sheets(1)
With oWs
LastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
.Columns("O:XFD").Delete
' Select All rows in Column A up to last filled row
.Range(“A1:A" & LastRow).Select
' Delete all rows with empty cell in A - up to last filled row
oXL.Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
.Save
.Close False
End With
Set oWb = Nothing
Set oWs = Nothing
End Sub

Related

To list Folders name and count number of files in each folder

I need help in the code i have found from youtube and request if anyone could please edit it so that it displays the following requirement.
currently it only counts excel files, Can anyone please edit so that it should read all the extension in folder(s).
Secondly it just count one main directory, is it possible if it can be edited so it should read the subfolders and count files in them as well.
third for now it displays the count answer in a message box, it is possible if it displays the answer in Column B.
E.g. There are 5 sub folders with different names and each folder contains files with different extensions.
The code can read all the Subfolders and list down the name of folder in excel and also count and return the answer in front of each folder name.
Sub CountFiles()
Dim strDir As String
Dim fso As Object
Dim objFiles As Object
Dim obj As Object
Dim lngFileCount As Long
strDir = "E:\2022\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFiles = fso.GetFolder(strDir).Files
lngFileCount = objFiles.count
MsgBox lngFileCount 'Total number of files
'***************************************************
'NOTE: Ensure that the following code does not overwrite _
anything in your workbook.
'Active worksheet should be a blank worksheet
For Each obj In objFiles
ActiveSheet.Cells(Rows.count, "A").End(xlUp).Offset(1, 0) = obj.Name
Next obj
Set objFiles = Nothing
Set fso = Nothing
Set obj = Nothing
End Sub
I shall remain thankful
List Subfolders
Sub ListSubfolders()
' Define constants.
Const FolderPath As String = "E:\2022\"
' Reference the folder.
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(FolderPath) Then
MsgBox "The folder """ & FolderPath & """ doesn't exist.", vbCritical
Exit Sub
End If
Dim fsoFolder As Object: Set fsoFolder = fso.GetFolder(FolderPath)
' Reference the first cell.
If ActiveSheet Is Nothing Then Exit Sub ' no visible workbooks open
If Not TypeOf ActiveSheet Is Worksheet Then Exit Sub ' not a worksheet
Dim ws As Worksheet: Set ws = ActiveSheet
Dim fCell As Range
Set fCell = ws.Cells(ws.Rows.Count, "B").End(xlUp).Offset(1)
' Write the folder properties.
' If you don't want this, then out-comment it but also copy the line
' 'Set fCell = fCell.Offset(1)' to the bottom of the loop.
fCell.Value = fsoFolder.Name
fCell.Offset(, -1).Value = fsoFolder.Files.Count
' Write the subfolders' properties.
Dim fsoSubfolder As Object
For Each fsoSubfolder In fsoFolder.Subfolders
Set fCell = fCell.Offset(1)
fCell.Value = fsoSubfolder.Name
fCell.Offset(, -1).Value = fsoSubfolder.Files.Count
Next fsoSubfolder
' Inform.
MsgBox "Folders listed.", vbInformation
End Sub

How to export data from multiple emails to Excel workbook but different worksheets?

I want to export data from selected Outlook emails to a workbook. Each email's data (subject, body, etc.) should be stored in a different worksheet.
I'm trying to edit this macro because it is almost what I need—and especially the part of olFormatHTML and WordEditor—because of split.
The idea is
Select multiple emails in Outlook
Open file path
Data for each email selected will be stored in a single worksheet from file opened
The issue with the macro is in this third part
From the selected items, the macro does a loop and just takes the first email selected,
The data is stored in different workbooks; it should be stored in the same workbook that I opened.
Public Sub SplitEmail()
Dim rpl As Outlook.MailItem
Dim itm As Object
Dim sPath As String, sFile As String
Dim objDoc As Word.Document
Dim txt As String
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim i As Long
Dim x As Long
'----------------------------
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection
For x = 1 To myOlSel.Count
'----------------------------------------------
Set itm = GetCurrentItem() 'A)I think the issuefrom selecting 1 item is located here
'|||||||||||||||||||||||||||||||||||||||||
sPath = "C:\Users\Ray\"
sFile = sPath & "Macro.xlsm"
If Not itm Is Nothing Then
Set rpl = itm.Reply
rpl.BodyFormat = olFormatHTML
'rpl.Display
End If
Set objDoc = rpl.GetInspector.WordEditor
txt = objDoc.Content.Text
'||||||||||||||||||||||||||||||||||||||||||||||
Set xlApp = CreateObject("Excel.application")
xlApp.Visible = True
Set wb = xlApp.Workbooks.Open(sFile) 'B) tried to move it to the beginning and macro doesn't work
'||||||||||||||||||||||||||||||||||||||||||||||
For i = LBound(Split(txt, Chr(13)), 1) To UBound(Split(txt, Chr(13)), 1)
wb.Worksheets(x).Range("A" & i + 1).Value = Split(txt, Chr(13))(i) 'B)emails in diferrent sheet but no same workbook
Next i
'------------------------------------------------------
Next x
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
GetCurrentItem.UnRead = False
Set objApp = Nothing
End Function
I made an update to this macro
as macro do loop in For x it open the file x times,
and then close it and open again instead of working on the first workbook opened
but the macro leaves open instances
here is the current code
Public Sub SplitEmail()
Dim rpl As Outlook.MailItem
Dim itm As Object
Dim sPath As String, sFile As String
Dim objDoc As Word.Document
Dim txt As String
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim i As Long
Dim x As Long
'----------------------------
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection
For x = 1 To myOlSel.Count
'----------------------------------------------
Dim objApp As Outlook.Application
Dim GetCurrentItem As Object
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.item(x)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
GetCurrentItem.UnRead = False
Set objApp = Nothing
'-----------------------------------------------
Set itm = GetCurrentItem
sPath = "C:\Users\Ray\"
sFile = sPath & "Macro.xlsm"
If Not itm Is Nothing Then
'de lo contrario, se crea un Reply del correo en formato HTML
Set rpl = itm.Reply
rpl.BodyFormat = olFormatHTML
'rpl.Display
End If
Set objDoc = rpl.GetInspector.WordEditor
txt = objDoc.Content.Text
'||||||||||||||||||||||||||||||||||||||||||||||
Set xlApp = CreateObject("Excel.application")
xlApp.Visible = True
Set wb = xlApp.Workbooks.Open(sFile)
xlApp.Windows("Macro.xlsm").Activate
'Set wb = ActiveWorkbook
'||||||||||||||||||||||||||||||||||||||||||||||
For i = LBound(Split(txt, Chr(13)), 1) To UBound(Split(txt, Chr(13)), 1)
wb.Worksheets(x).Range("A" & i + 1).Value = Split(txt, Chr(13))(i)
Next i
xlApp.Windows("Macro.xlsm").Close SaveChanges:=True
xlApp.Workbook.Close SaveChanges:=False
'------------------------------------------------------
Next x
'------------------------------------------------------
'the instances should closed but not working, instances are empty
For Each wb In xlApp
wb.Close SaveChanges:=False
Next
End Sub
done, I added xlApp.Quit after saving files and deleted the last part For Each wb In xlApp...

How can I open multiple files to copy data into a master workbook in new tab?

I have a master Excel file that needs to inherit data from .csv files.
When I run the VBA, it will pop up the file explorer, let me select multiple files and loop over them and create new sheets.
When I try to copy the data into the sheet that it created, it gives me a type mismatch error.
Sub OpenLMSFiles()
Dim fd As FileDialog
Dim FileChosen As Integer
Dim FileName As String
Dim tempWB As Workbook
Dim i As Integer
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.InitialFileName = "Libraries\Documents"
fd.InitialView = msoFileDialogViewList
fd.AllowMultiSelect = True
FileChosen = fd.Show
If FileChosen = -1 Then
For i = 1 To fd.SelectedItems.Count
Set tempWB = Workbooks.Open(fd.SelectedItems(i))
Call ReadDataFromSourceFile(tempWB)
Next i
End If
End Sub
Private Sub ReadDataFromSourceFile(src As Workbook)
Application.ScreenUpdating = False
ThisWorkbook.Sheets.Add
Workbooks(src).Worksheets(src.ActiveSheet).Range("A1:Z500").Copy _
Workbooks(ThisWorkbook).Worksheets(ThisWorkbook.ActiveSheet).Range("A1:Z500")
End Sub
The cause of the error is the way you are referencing workbooks and worksheets, which are collections that take index arguments (integer or string). For example you can reference a workbook as Workbooks(1) (bad idea) or Workbooks("FileName.xlsx") (better). Similarly use Sheets(1) or Sheets("SheetName").
src is a Workbook -> simply use src.Sheets(). Because csv files have only 1 worksheet it is safe to use src.Worksheets(1) (Sheets and Worksheets are equivalent).
Anyway, here is a working code. I rearranged the code as I think ReadDataFromSourceFile should encapsulate opening and closing the csv file as well as reading data from it (only an opinion)
Sub ImportLMSFiles()
Dim fd As FileDialog
Dim FileChosen As Long
Dim FileName As String
Dim i As Long
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.InitialFileName = "Libraries\Documents"
fd.InitialView = msoFileDialogViewList
fd.AllowMultiSelect = True
FileChosen = fd.Show
If FileChosen = -1 Then
For i = 1 To fd.SelectedItems.Count
Call ReadDataFromSourceFile(fd.SelectedItems(i))
Next i
End If
End Sub
Private Sub ReadDataFromSourceFile(sSrcFilename As String)
' Validate the name
If Right(sSrcFilename, 4) <> ".csv" Then Exit Sub
Application.ScreenUpdating = False
Dim shtDest As Worksheet: Set shtDest = ThisWorkbook.Sheets.Add
Dim wbSrc As Workbook: Set wbSrc = Workbooks.Open(sSrcFilename)
' csv files have only 1 sheet
' UsedRange is exactly what it sounds like
With wbSrc.Sheets(1)
.UsedRange.Copy shtDest.Range(.UsedRange.Address)
' if you want to rename the new sheet
' Bug: another sheet might have the same name -> u need check for that
' Here I'm just ducking it: name not changed
On Error Resume Next
shtDest.Name = .Name
On Error GoTo 0
End With
wbSrc.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub

Save PPT File Name where Value from Excel Column

I'm trying to make a VBA macro where You can save each slide with Different Files names, the value of each File Name will come from a specific column of Excel file.
Here's the code that I've been trying so far..
Dim oWB As Object
Dim oXL As Object
Dim xlWS As Object
Dim strFile As String
Dim FName As String
Dim xlColumn As String
Dim getName As String
Dim r As Long
Dim m As Long
' Open Excel File
Set oXL = CreateObject("Excel.Application")
strFile = oXL.GetOpenFilename("Excel Worksheets (*.xlsx),*.xlsx", , "Select Excel file")
If strFile = "False" Then
Beep
Exit Sub
End If
On Error Resume Next
Set oWB = GetObject(Class:="Excel.Application")
If oWB Is Nothing Then
Set oWB = CreateObject(Class:="Excel.Application")
If oWB Is Nothing Then
Beep
Exit Sub
End If
End If
On Error GoTo 0 ' ErrHandler
oWB.Visible = msoCTrue
xlColumn = CStr(InputBox("What Column of Worksheet?", "Column Designation"))
Set xlWS = oWB.Workbook.Open(strFile, , , msoFalse)
m = oWB.Range("A" & Rows.Count).End(xlUp).Row
For r = m To 2 Step -1
FName = oWB.worksheets("Sheet1").Range(xlColumn & r).Value
Next r
While running the macro I'm getting an error:
Application defined or object defined error
Set xlWS = oWB.worksheets.Open(strFile, , , msoFalse)

Concatenate index name in the Workbooks Object

I am trying to create For Each loop for a several workbooks; however I am not able to set the workbook name in the array and thus resulted into this. I'm stuck in trying to concatenate the workbook name.
Here's my code:
'Open all .csv file in folder location
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(ThisWorkbook.Path)
For Each objFile In objFolder.Files
If InStr(objFile, ".csv") Then
Workbooks.Open (objFile)
End If
Next
' Declare variables
Dim WrkBkPrm As Workbook
Dim WrkBkSrc As Workbook
Dim WrkShtPrm As Worksheet
Dim WrkShtSrc As Worksheet
Dim TextSrc(4) As String
Dim SrcRng As Range
Dim DRng As Range
' Assign values to TextSrc() Array
TextSrc(0) = Cable
TextSrc(1) = Care
TextSrc(2) = MSD
TextSrc(3) = Business
'Set WrkBkPrm and WrkShtPrm values
Set WrkBkPrm = Workbooks("MasterFile" & ".xlsm")
Set WrkShtPrm = WrkBkPrm.Worksheets("Canvas")
'Activate Canvas Sheet
WrkBkPrm.Activate
WrkShtPrm.Select
Application.ScreenUpdating = False
'Start For Each Loop
For Each Src In TextSrc()
Set WrkBkSrc = Workbooks(Src & ".csv")
Set WrkShtSrc = WrkBkSrc.Worksheets(Src)
'Copy loop for 1st section
For i = 2 To 49
For j = 7 To 25
Set SrcRng = WrkShtSrc.Cells(i, j)
Set DRng = WrkShtPrm.Cells(i, j)
If SrcRng <> "" Then
DRng.Value = SrcRng.Value
End If
Next j
Next i
Next Src
Application.ScreenUpdating = True
I'd suggest you work on the file as soon as you get hold of the relevant info you need.
Something like:
Dim wb As Workbook
For Each objFile In objFolder.Files
If InStr(objFile.Name, ".csv") <> 0 Then
Set wb = Workbooks.Open(Thisworkbook.Path & "\" & objFile.Name)
'~~> Do your cool stuff with the opened CSV File
wb.Close False '~~> Close without saving
Set wb = Nothing '~~> Clean up although most of the time not necessary
End If
Next
As for the route you took, for you to use For Each Loop on TxtSrc, you need to declare it as variant.
Something like:
Dim TxtSrc As Variant, Src As Variant
TxtSrc = Array("Cable", "Care", "MSD", "Business")
For Each Src In TxtSrc
Set wb = Workbooks.Open(Thisworkbook.Path & "\" & Src & ".csv")
'~~> More cool stuff here
wb.Close False
Set wb = Nothing
Next
It is important that provide the correct argument for the Workbook Open Method.
You should always include the complete path in string format. HTH.

Resources