joanna_sjw
New Member
- Joined
- Apr 28, 2020
- Messages
- 8
- Office Version
- 2013
- Platform
- Windows
Hello, I'm just a 2 days into VBA with alot of help from various online sources.
I am trying to automate a reporting whereby:
1) Downloads a data file from a web browser
2) Save this file in a location
3) Opens the downloaded file and do data cleaning (unmerge cells and delete blank rows)
4) Copy data from cleaned data till last row and paste to main compiled data from last empty row onwards
5) Fill dates in column A based on a value from main compiled data file till last available data row
Currently I am stuck at 4 where WB1 and WB2 does not seem to be able to be defined.. it is returning me nothing for both. Hence I can't get the lastrow defined as well.
Checked fname with debug.print and the directory was ok. Can anyone help to see what's wrong?
Any help is appreciated!
I am trying to automate a reporting whereby:
1) Downloads a data file from a web browser
2) Save this file in a location
3) Opens the downloaded file and do data cleaning (unmerge cells and delete blank rows)
4) Copy data from cleaned data till last row and paste to main compiled data from last empty row onwards
5) Fill dates in column A based on a value from main compiled data file till last available data row
Currently I am stuck at 4 where WB1 and WB2 does not seem to be able to be defined.. it is returning me nothing for both. Hence I can't get the lastrow defined as well.
Checked fname with debug.print and the directory was ok. Can anyone help to see what's wrong?
Any help is appreciated!
VBA Code:
Sub AutomateAccessReport()
Dim i As Long
Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim WHTTP As Object
On Error Resume Next
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0
'create download folder
If Dir("C:\Users\X\Desktop\RPA\Access", vbDirectory) = Empty Then MkDir "C:\Users\X\Desktop\RPA\Access"
'loop row 1 to 1
For i = 1 To 1
MyFile = Cells(i, 1)
WHTTP.Open "GET", MyFile, False
WHTTP.Send
FileData = WHTTP.ResponseBody
'putting the downloaded file into the directory
FileNum = FreeFile
Open "C:\Users\X\Desktop\RPA\Access\" & Cells(i, 3) For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum
Next
Set WHTTP = Nothing
MsgBox ("Download completed")
'Open a workbook
'Open method requires full file path to be referenced.
Const fpath As String = "C:\Users\X\Desktop\RPA\Access\" ' your fixed folder
Dim fname As String
' Below defines fname
fname = Format(Workbooks("RPA USS.xlsm").Worksheets("RPA").Range("B1").Value, "YYYY-MM-DD")
fname = fname & "_RPT_Access_Report_By_Operation.xls"
fname = fpath & fname
Dim wb As Workbook
Set wb = Workbooks.Open(fname)
If wb Is Nothing Then MsgBox "File does not exist":
Debug.Print (fname)
'Unmerge All Cells
ActiveSheet.Cells.UnMerge
'Delete Empty Rows
On Error Resume Next
Columns("N").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
MsgBox ("Data Cleaning Completed")
'Copy Data Range till last available data row from WB1 and paste to WB2 from last data row onwards
Dim WB1 As Worksheet
Dim WB2 As Worksheet
Set WB1 = Workbooks(fname).Worksheets("Sheet1")
Set WB2 = Workbooks("RPA USS.xlsm").Worksheets("DATA")
Dim lastrow As String
Dim lastrow2 As String
With WB1
lastrow = WB1.Range("A" & .Rows.Count).End(xlUp).Row
lastrow2 = WB2.Range("A" & .Rows.Count).End(xlUp).Offset(1).Row
End With
WB1.Range("A2:O" & lastrow).Copy WB2.Range ("B" & lastrow2)
'Fill Dates
Workbooks("RPA USS.xlsm").Worksheets("RPA").Range("B1").Value.Copy
WB2.Range("A" & lastrow2).Paste
End Sub