Add data to specific cells in a column - excel

The below code copies from one file to another.
I only want TO ADD the word "AVA" to cells in the Column H but only until the last row.
So basically the macro filters on "PENDING" and I have 14 rows of Pending data , then all the 14 cells of Column H should be showing "AVA".
Any recommendations?
Sub DS()
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceWorkbookPath As String
Dim targetWorkbookPath As String
Dim lastRow As Long
Dim i As Long
' Define workbooks paths
sourceWorkbookPath = "H:\Roy\Transfers Project\ Transfers 2020 - Roy.xlsm"
targetWorkbookPath = "H:\Roy\ 2020\SAP - ZPSD02_template2.xlsx"
' Set a reference to the target Workbook and sheets
Set sourceWorkbook = Workbooks.Open(sourceWorkbookPath)
Set targetWorkbook = Workbooks.Open(targetWorkbookPath)
' definr worksheet's names for each workbook
Set sourceSheet = sourceWorkbook.Worksheets("S TO S")
Set targetSheet = targetWorkbook.Worksheets("Sheet1")
Application.ScreenUpdating = False
With sourceSheet
' Get last row
lastRow = .Range("J" & .Rows.Count).End(xlUp).Row
For i = 1 To lastRow
.Range("H" & i).Value = "AVA" & .Range("H" & i).Value
Next i
.Range("A1:O1").AutoFilter Field:=12, Criteria1:="PENDING"
.Range("A1:O1").AutoFilter Field:=10, Criteria1:="U3R", Operator:=xlOr, Criteria2:="U2R"
.Range("J2:J" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("A1")
.Range("C2:C" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("B1")
.Range("D2:D" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("E1")
.Range("H2:H" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("F1")
End With
With targetSheet
For i = 1 To lastRow
.Range("H" & i).Value = "AVA"
Next i
End With
Application.ScreenUpdating = True
End Sub

Sub DS()
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceWorkbookPath As String
Dim targetWorkbookPath As String
Dim lastRow As Long
Dim i As Long
Application.ScreenUpdating = False
' Define workbooks paths
sourceWorkbookPath = "H:\Roy\Transfers Project\ Transfers 2020 - Roy.xlsm"
targetWorkbookPath = "H:\Roy\ 2020\SAP - ZPSD02_template2.xlsx"
' Set a reference to the target Workbook and sheets
Set sourceWorkbook = Workbooks.Open(sourceWorkbookPath)
Set targetWorkbook = Workbooks.Open(targetWorkbookPath)
' Define worksheet's names for each workbook
Set sourceSheet = sourceWorkbook.Worksheets("S TO S")
Set targetSheet = targetWorkbook.Worksheets("Sheet1")
With sourceSheet
' Get last row
lastRow = .Range("J" & .Rows.Count).End(xlUp).Row
.Range("A1:O1").AutoFilter Field:=12, Criteria1:="PENDING"
.Range("A1:O1").AutoFilter Field:=10, Criteria1:="U3R", Operator:=xlOr, Criteria2:="U2R"
.Range("J2:J" & lastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=targetSheet.Range("A1")
.Range("C2:C" & lastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=targetSheet.Range("B1")
.Range("D2:D" & lastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=targetSheet.Range("E1")
.Range("H2:H" & lastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=targetSheet.Range("F1")
End With
With targetSheet
For i = 1 To lastRow
.Range("H" & i).Value = "AVA"
Next i
End With
Application.ScreenUpdating = True
End Sub

Related

VBA to copy and append filtered data to another workbook

Sub DS()
Dim sourceWorkook As Workbook
Dim targetWorkbook As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceWorkbookPath As String
Dim targetWorkbookPath As String
Dim lastRow As Long
' Define workbooks paths
sourceWorkbookPath = "H:\L\Roy\RT\Transfers\Transfers 2020 - Roy.xlsm"
targetWorkbookPath = "H:\L\Roy\H and E\2020\SAP - ZPSD02_template2.xlsx"
' Set a reference to the target Workbook and sheets
Set sourceWorkbook = Workbooks.Open(sourceWorkbookPath)
Set targetWorkbook = Workbooks.Open(targetWorkbookPath)
' definr worksheet's names for each workbook
Set sourceSheet = sourceWorkbook.Worksheets("ST TO ST")
Set targetSheet = targetWorkbook.Worksheets("Sheet1")
With sourceSheet
' Get last row
lastRow = .Range("J" & .Rows.Count).End(xlUp).Row
.Range("A1:O1").AutoFilter Field:=12, Criteria1:="PENDING"
.Range("A1:O1").AutoFilter Field:=10, Criteria1:="U3R", Operator:=xlOr, Criteria2:="U2R"
.Range("J2:J" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("A1")
.Range("C2:C" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("B1")
.Range("D2:D" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("E1")
.Range("H2:H" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("F1")
End With
End Sub
I want to Append the filtered data under the last (used row) in the target workbook. This code copies data from source to target but I want to append this data to the target workbook since I am using the target workbook for most of the work.
Only a few bits you need to add to append data to first empty row:
Sub BitsAndPeicesExample()
' ... ... ...
Dim tLastRow As Long
' ... ... ...
' Whatever column works > \|/
tLastRow = targetSheet.Range("A" & Rows.Count).End(xlUp).Row + 1
' /|\
' ... ... ...
.Range("J2:J" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("A" & tLastRow)
' ... ... ...
End Sub
I would encourage you to consider alternatives to .copy such as [...].value = [...].value
You might need .resize to make source and destination ranges the same size. idk

Type mismatch in range

I am trying to copy a filtered range from a master dataset to a spreadsheet for each country (loop). I am getting a type mismatch error for Set rng1 = ws2.Range("E2:L" And lRow1) when I set a range in the filtered sheet. Can someone please help me identify the cause of the mismatch?
Sub CopyData_To_TemplateWorkbook2()
Dim wb As Workbook
Dim SavePath, TemplatePath, TemplateFile As String
Dim ws1, ws2, ws3, wbws1, wbws2, wbws3 As Worksheet
Dim rng1, rng2 As Range
Dim MSi As Variant
Dim lRow1, lRow2 As Long
Application.DisplayAlerts = False
'Application.ScreenUpdating = False
TemplatePath = "C:\Users\xyz\Test\"
TemplateFile = "Template_blank.xlsx"
SavePath = "C:\Users\xyz\Test\"
Set ws1 = ThisWorkbook.Sheets("Lists")
Set ws2 = ThisWorkbook.Sheets("Responses 2006 2020")
ws1.Select
For i = 2 To 5 'Loop through list of country names
Set MSi = ws1.Range("A" & i)
ws2.Activate
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
ws2.Range("B1").AutoFilter Field:=2, Criteria1:=MSi 'Filter Criteria1 = i
lRow1 = ws2.Range("E" & Rows.Count).End(xlUp).Row
Set rng1 = ws2.Range("E2:L" And lRow1) 'Type mismatch here
'Set rng1 = ws2.Range("E2:L") 'Application-defined or object-defined error here if used
Set wb = Workbooks.Open(Filename:=TemplatePath & "Template_blank.xlsx", Editable:=True)
Set wbws1 = wb.Sheets("Cover sheet")
Set wbws2 = wb.Sheets("Responses")
wbws1.Range("B2").Value = MSi
wbws2.Range("B2").Value = MSi
lRow2 = wbws2.Range("A" & Rows.Count).End(xlUp).Row 'But there is no last row - blank sheet
Set rng2 = wbws2.Range("A6:H") ' And lRow2 ??
rng2.Value = rng1.Value
wb.SaveAs Filename:=SavePath _
& MSi & "_text" & Format(Date, "yyyymmdd") & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
wb.Close SaveChanges:=False
Set rng1 = Nothing
ws1.Select
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

how to Create Pivot Table at another xlfile via VBA

I want to create Pivot table at another xlfile, but when I Define Pivot Cache it doesn't work
Can anyone know reason?
Option Explicit
Dim date_sheet As String
Dim csv_sheet As String
Dim ptl_workbook As String
Dim csv_workbook As String
Dim insert_day As String
Sub AOI_PTL_Updater()
Dim rng As Variant
Dim myrng As Range
Dim mypath As String
Dim ptl_last_r As Integer
Dim csv_last_r As Integer
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
insert_day = Application.InputBox("ex) 20201217", "Enter the day", Left:=1000, Top:=800, Type:=2)
date_sheet = Right(insert_day, 2)
csv_sheet = InputBox("Ex) 3-1-1", "Enter sepcifitc data name", , 2000, 4000)
mypath = ThisWorkbook.Path & "\" & insert_day & "\"
csv_workbook = Dir(mypath & csv_sheet & ".csv")
ptl_workbook = "AOI_Report.xlsm"
Workbooks.Open Filename:=mypath & csv_workbook
Rows("1:6").Delete Shift:=xlUp
With Columns("B:B")
.Insert Shift:=xlToRight
.Insert Shift:=xlToRight
.Insert Shift:=xlToRight
.Insert Shift:=xlToRight
End With
Range("B1").FormulaR1C1 = "CRD"
Range("C1").FormulaR1C1 = "Pannel"
Range("D1").FormulaR1C1 = "Part_number"
Range("E1").FormulaR1C1 = "Real_NG"
csv_last_r = Cells(Rows.Count, 1).End(xlUp).Row
Set myrng = Range("A2:A" & csv_last_r)
Windows(ptl_workbook).Activate
ptl_last_r = Cells(Rows.Count, "A").End(xlUp).Row
Windows(csv_workbook).Activate
For Each rng In myrng
rng.Offset(0, 1).FormulaR1C1 = "=LEFT(RC[-1],FIND(""["",RC[-1],1)-1)"
rng.Offset(0, 2).FormulaR1C1 = "=MID(RC[-2],FIND(""["",RC[-2],1)+1,FIND(""]"",RC[-2],1)-(LEN(RC[-1])+2))"
rng.Offset(0, 3).FormulaR1C1 = "=INDEX('[" & ptl_workbook & "]PTL_Updater'!R1C3:R" & ptl_last_r & "C3,MATCH(""*""&RC[-2]&""*"",'[" & ptl_workbook & "]PTL_Updater'!R1C10:R" & ptl_last_r & "C10,0))"
rng.Offset(0, 4).FormulaR1C1 = "=AGGREGATE(9,6,RC20:RC28)"
Next
ActiveWorkbook.Save
Call Create_PT
Windows(csv_workbook).Activate
ActiveWindow.Close
Windows(ptl_workbook).Activate
MsgBox ("PTL Update Complete !")
Application.Calculation = xlCalculationAutomatic
End Sub
Sub Create_PT()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
Dim PWorkbook As Workbook
Dim DWorkbook As Workbook
Dim destination_range As String
Dim D_Data As String
'Declare Variables
Windows(ptl_workbook).Activate
Sheets(date_sheet).Select
Set PWorkbook = Workbooks(ptl_workbook)
Set PSheet = PWorkbook.Worksheets(date_sheet)
Set DWorkbook = Workbooks(csv_workbook)
Set DSheet = DWorkbook.Worksheets(csv_sheet)
destination_range = PSheet.Range("B31").Address(ReferenceStyle:=xlR1C1)
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
With DSheet.Cells
D_Data = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)).Address(ReferenceStyle:=xlR1C1)
End With
PWorkbook.Activate
'Define Pivot Cache
Set PCache = PWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:="[" & DWorkbook.Name & "]" & DSheet.Name & "!" & D_Data). _
CreatePivotTable(TableDestination:="[" & PWorkbook.Name & "]" & PSheet.Name & "!" & destination_range, _
TableName:="PTL-data")

How to pick file with workbook object VBA

I have code like that:
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim LastRow As Long
Set wsCopy = Workbooks("file1.xlsm").Worksheets("Order")
Set wsDest = Workbooks("orders.xlsm").Worksheets("All Data")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(0).Row
wsCopy.Range("A1:I9" & lCopyLastRow).Copy _
wsDest.Range("A" & lDestLastRow)
LastRow = Sheets("All Data").UsedRange.Rows.Count
Sheets("All Data").Range("L" & lDestLastRow).Value = "order made?:"
Sheets("All Data").Range("L" & lDestLastRow + 1).Value = "Yes/No"
Range("L" & lDestLastRow).Font.Bold = True
wsDest.Activate
In place where i have Workbooks("file1.xlsm") i want pick file to copy.
It is possible using Workbooks or any other method?
With the GetOpenFilename-method, Workbooks.Open and replacing your string with the variable:
Dim selectedFilename As Variant
selectedFilename = Application.GetOpenFilename(FileFilter:=”Excel Files,*.xl*;*.xm*”)
If selectedFilename <> False Then
Workbooks.Open FileName:=selectedFilename
End If
Set wsCopy = Workbooks(Mid$(selectedFilename, InStrRev(selectedFilename, "\") + 1)).Worksheets("Order")

Copy data from 3 workbooks to 1 master

I'm struggling with my code to copy data from 3 workbooks with same worksheet name to one master workbook also having the same name. The main problem is to define the last row. After I copy the first data set from the first workbook and then go to second one I want to paste the data below the first data in master workbook and so on. Do you guys have any suggestions?
Below is my unfinished code:
Sub refresh()
Dim wball, wb1, wb2, wb3 As Workbook
Dim ws, sht As Worksheet
Dim wbpath As String
Dim LastRow As Long
Application.ScreenUpdating = False
wbpath = Application.ThisWorkbook.Path
'wball = ThisWorkbook 'master workbook
Application.DisplayAlerts = False
'clears master wb
Set ws = ThisWorkbook.Worksheets("Tab")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
On Error Resume Next
ws.Rows(3).ClearContents
'ws.Rows("3:" & LastRow).Delete
ws.Range("Tab").Delete
Set wb1 = Workbooks.Open(wbpath & "\file1.xlsm")
LastRow = wb1.Sheets("Tab").Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Range("A3:CD" & LastRow).Value = wb1.Sheets("Tab").Range("A3:CD" & LastRow).Value
Set wb2 = Workbooks.Open(wbpath & "\file2.xlsm")
LastRow = wb2.Sheets("Tab").Cells(ws.Rows.Count, "A").End(xlUp).Row
'ws.
ws.Range("A3:CD" & LastRow).Value = wb2.Sheets("Tab").Range("A3:CD" & LastRow).Value
wb1.Close SaveChanges:=False
wb2.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
Ok, I have made some changes and everything is working now as it should be.
Sub refresh()
Dim masterwb As Workbook
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim wb3 As Workbook
Dim masterws As Worksheet
Dim ws As Worksheet
Dim wbpath As String
Dim LastRow As Long
Dim LastRowSource As Long
Dim LastRowDestination As Long
Application.ScreenUpdating = False
wbpath = Application.ThisWorkbook.Path
'masterwb = ThisWorkbook
Application.DisplayAlerts = False
'clears master wb
Set masterws = ThisWorkbook.Worksheets("Tab")
'LastRow = masterws.Cells(ws.Rows.Count, "A").End(xlUp).Row
masterws.Rows(3).ClearContents
masterws.Range("A4:CD9999").Delete
'start to copy data from 3 workbooks
Set wb1 = Workbooks.Open(wbpath & "\file1.xlsm")
LastRowDestination = wb1.Sheets("Tab").Cells(Rows.Count, "A").End(xlUp).Row
masterws.Range("A3:CD" & LastRowDestination).Value = wb1.Sheets("Tab").Range("A3:CD" & LastRowDestination).Value
LastRowSource = masterws.Cells(masterws.Rows.Count, "A").End(xlUp).Row + 1
'LastRowSource + LastRowDestination -3 because im getting 3 extra rows with #N/D
Set wb2 = Workbooks.Open(wbpath & "\file2.xlsm")
LastRowDestination = wb2.Sheets("Tab").Cells(Rows.Count, "A").End(xlUp).Row
masterws.Range("A" & LastRowSource & ":CD" & LastRowSource + LastRowDestination -3).Value = wb2.Sheets("Tab").Range("A3:CD" & LastRowDestination).Value
LastRowSource = masterws.Cells(masterws.Rows.Count, "A").End(xlUp).Row + 1
Set wb3 = Workbooks.Open(wbpath & "\file3.xlsm")
LastRowDestination = wb3.Sheets("Tab").Cells(Rows.Count, "A").End(xlUp).Row
masterws.Range("A" & LastRowSource & ":CD" & LastRowSource + LastRowDestination -3).Value = wb3.Sheets("Tab").Range("A3:CD" & LastRowDestination).Value
wb1.Close SaveChanges:=False
wb2.Close SaveChanges:=False
wb3.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
Thanks for your help.

Resources