I have a Master file which monitors shipment progress for my company. I update this on a weekly basis with progress on existing shipments (shipments which are already in the master file) and input new shipments. These updates come in the form of a update sheet which is in the same format as the master file. I have automated the input of new shipments, but I am stuck on how I could update the existing shipments progress.
Currently I perform a vlookup manually based off a shipment reference number. This is a pain because there are multiple shipment progress columns and I have to trawl through each new input to remove the vlookup formula.
FOR EXAMPlE: shipment reference is in column "C", shipment pickup is in column "AG", shipment dispatched is in column "AH" and shipment arrived is in column "AI".. and so on. In the master sheet I might have the reference number and pickup date. The update sheet might have the dispatch date for this shipment. I would want to take the dispatch date and copy it into the master file for this shipment.
I essentially want to search for blanks in each column of the master file and copy this data from the update file based on the shipment reference. I cannot copy and past over the master file data because I have added a lot of data manually the previous week which the updated file may not have or agree with.
Thank you for your help
Please see my attempted code:
Sub Eupdatedates()
Dim lr As Long, rw As Long, extRNG As Range
Set wb = Workbooks("masterfile.xlsm")
Set ws = wb.Sheets("RawDATA")
lastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
Set extRNG = Workbooks("updatefile.csv").Worksheets("report").Range("C:AL")
For i = 33 To 38 ' columns which contain dates that need updating
ws.Range("$A$2:$CV$" & lastRow).AutoFilter field:=i, Criteria1:="=" 'filter out cells containing data
lastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row ' find last row after filtering
firstRow = Range("Cells(2,i): Column(i)" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row ' indentify first row after filtering
For rw = firstRow To lastRow ' loop through each each empty row cell and perform a vlookup to the update file
Cells(rw, i) = Application.VLookup(Cells(rw, i), extRNG, i - 2, False)
Next rw
Next i
End Sub
I am getting the error with "Range("Cells(2,i): Column(i)" I think I have looked online and cannot seem to find a way to define the range within a for loop. I am new to VBA so a lot of ground to make up. Any thoughts would be greatly appreciated!!
This is a job for VBA. I have done something similar in the past and used VBA to complete the task but it's not something I could code without having the workbooks in front of me. As a pointer I usually start with a big sheet of blank paper and write out the process in logical steps before beginning to code, e.g.
How many rows are there? (I want to know when to stop)
Where is the update sheet and what is it called?
start with first row (row number = 1)
What is the shipment reference (ship ref)?
select cell (row number, column of interest number)
is it blank? YES - find corresponding value in update sheet based on ship ref and update, NO - move to next column of interest
No more columns of interest for this row? - move to next row (row number +1)
etc.
Related
My data range needs to be copied from one tab to another. The key to this is it needs to paste the information in the data range into a new row, even if the information is the same. So each time it is run a new row will be populated. Another key here is the row data range consists of criteria, depending on the value of criteria the code will decide which tab to copy into.
I was able to create the code to copy but without if statement and it copies into the same row each time
if Cell E3 = "Revenue" then copy into Revenue worksheet, if not then copy into Cost worksheet.
Each time code is run, the data will be copied into the last unoccupied row available in that worksheet
If I understood You right, this should work:
Sub copy2sheet()
Dim cond As String
Dim lastRow As Integer
With Sheets(ActiveSheet.Name)
'taking name of the sheet from "E3"
cond = .Range("E3").Value
'checking last filled row in sheet we gonna copy data
lastRow = Sheets(cond).Cells(Rows.Count, 1).End(xlUp).row
'copy data starting from "A1" to every cell is close to - CurrentRegion
'a paste it into first empty cell in column "A"
'.Range("A1").CurrentRegion.Copy Sheets(cond).Range("A" & lastRow + 1)
.Range("E3:H3").Copy Sheets(cond).Range("A" & lastRow + 1)
End With
End Sub
Let’s say I have a rental car company and I have 12 sheets with 10 columns each and unknown amount of rows. Each sheet is holding information about cars rented. Below are the column headings for each spreadsheet
- A. Date rented
- B. Customer Name
- C. Customer Address
- D. Customer Phone
- E. Customer email
- F. Car Year
- G. Car Make
- H. Car Model
- I. Car Plate number
- J. Car Vin
I have a master sheet that I want to get specific information from all sheets and copy the cellValues of those sheets into the master sheet. I’m not familiar with VBA so Here is the sudocode of the loop I want to do:
For each sheet
For each row
Copy customer name, customer phone, car plate number into next available row on master sheet
In the master sheet, the columns would be respectively how I put them in the sudocode
- A. Customer Name
- B. Customer Phone
- C. Car Plate number
Can someone show me what the VBA macro code would be for this?
Disclaimer: this is not my actual information in my spreadsheet as what I am working on is confidential so I can’t provide screenshots. This is just example information that simulates what I want to do.
I've tried =HLOOKUP(B1,'Sheet1 (51)'!1:1048576,2:2,FALSE) but getting a value error or an NA error, depending on what range or values I try in the parameters. The way I was understanding the HLookup function is this:
lookup value is the column heading I'm looking for within the source sheet: for customer name I would but B2 for the lookup value
The table array would be the whole source sheet
The row array would be the row I'm getting the cell value from in the source sheet
range lookup is either T or F or nothing as it is optional. If i use True or False, I get the NA error if I use nothing I get the value error.
The idea is that once I get this formula working for one cell then expand it to one row then expand it to the loop i have in my sudocode for all rows within the source sheet, then expand it to look or go to the next source sheet.
You can accomplish this fairly easily by looping through all the available sheets, excluding the master sheet and setting the relevant range for each sheet. Then using the find function to get the last row of data in the master sheet to be able to append the next rows.
This should produce the desired result:
Sub MasterGrab()
Dim master As Worksheet
Dim subSheet As Range
Dim i As Integer
Dim lastRow As Long
Set master = Worksheets("MasterSheet")
x = Sheets.Count
lastRow = master.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'find last row on master sheet
lastRow = lastRow + 1
For i = 1 To x
If Not Sheets(i).Name = "MasterSheet" Then 'capture all sheets except MasterSheet
Set subSheet = Sheets(i).Range("A1:J" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row) 'set each sheet range to cover required data
For Each r In subSheet.Rows
master.Cells(lastRow, 1) = r.Cells(2) 'Customer name
master.Cells(lastRow, 2) = r.Cells(4) 'Customer phone
master.Cells(lastRow, 3) = r.Cells(9) 'Car Plate number
lastRow = lastRow + 1
Next r
End If
Next i
End Sub
I am using a workbook that has various sheets. I want to copy all the rows from the last 5 sheets that have the value "Pending" in their column "J". I want to create a new tab named "Pending week" and paste all these rows there. Any help would be really appreciated.
Thanks
You can create this yourself very easily if you just break it down:
Add a new Sheet
Name the sheet to Pending Week
Find the five latest sheets.
Create some kind of loop that copy paste row if cells in column J contains the value "Pending"
You have not provided any code, so I'll give you a base to work from:
You add a new sheet & name it using:
Worksheets.Add
ActiveSheet.Name = "Pending week"
Find the five latest sheets
To my knowledge, you can't find the latest sheets. Sheets doesn't contain the date and time of when they were created. But if we ignore that and expect the five latest sheets to be placed in the workbook to the far most right (Default position for newly created sheets). Then you need to figure out how many sheets you have and count backwards.
You can use: Worksheets.Count to count all the sheets. Use this number and count it backwards. My first thought would be to use a For Loop
Dim X As Integer
For X = (Worksheets.Count - 4) To Worksheets.Count
Next
X would be the identifier to find our latest sheets. So you should incorporate that into our loop below. You want to place the loop within this For Block.
Loop
There are many ways to find a value in a sheet, but you need to figure out what the last row of your sheets are. Without it we don't know when the code should stop.
You can use a Do Until Loop if there is a value in all J cells. Then you can simply insert the entire row into Pending week
It would look something like:
Dim XLrow As Integer
XLrow = 1
Do Until Worksheets(1).Cells(XLrow, "J") = ""
If Worksheets(1).Cells(XLrow, "J") = "Pending" Then
Worksheets(1).Range(XLrow & ":" & XLrow) = Worksheets("Pending week").Cells(XLrow, "J").Value
End If
XLrow = XLrow + 1
Loop
You will need to change the Range to the length of the range you want to copy. Note: the value Pending is case sensitive, so keep that in mind.
Alright, this is what you need to create your code. Of course you need to change values to fit your own workbook, but this is the base.
I have a current Sub that organizes data certain way for me and even enters a formula within the first row of the worksheet, but I am running into the issue of wanting it to be able to adjust how far down to fill the formula based on an adjacent column's empty/not empty status. For example, each time I run a report, I will get an unpredictable amount of returned records that will fill out rows in column A, however, since I want to extract strings from those returned records into different Columns, I have to enter formulas for each iteration within the next three columns (B, C, and D). Is there a way to insert a line that will evaluate the number of rows in Column A that are not blank, and then fill out the formulas in Columns B, C, and D to that final row? (I know that tables will do this automatically once information is entered in the first row, but for logistical reasons, I cannot use tables).
My current code that fills out the formula in Column B, Row 2 is:
Range("B2").Select
ActiveCell.FormulaR1C1 = "=MID(RC[-1],FIND(""By:"",RC[-1])+3,22)"
Thanks!
The formula that you actually need is
=IF(A2="","",MID(A2,FIND("By:",A2)+3,22))
instead of
=MID(A2,FIND("By:",A2)+3,22) '"=MID(RC[-1],FIND(""By:"",RC[-1])+3,22)"
This checks if there is anything in cell A and then act "accordingly"
Also Excel allows you to enter formula in a range in one go. So if you want the formula to go into cells say, A1:A10, then you can use this
Range("A1:A10").Formula = "=IF(A2="","",MID(A2,FIND("By:",A2)+3,22))"
Now how do we determine that 10? i.e the Last row. Simple
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change the name of the sheet as applicable
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find Last Row in Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("B2:B" & lRow).Formula = "=IF(A2="""","""",MID(A2,FIND(""By:"",A2)+3,22))"
End With
End Sub
More About How To Find Last Row
You can use this to populate columns B:D based on Column A
Range("B2:D" & Range("A" & Rows.Count).End(xlUp).Row).Formula = _
"=MID($A2,FIND(""By:"",$A2)+3,22)"
I have vba question I have been trying to find the answer for for a long time. I have numerous spreadsheets from numerous clients that I run macro's on, I'm new to coding and have been able to mostly figure out what I need to do. My clients send us data monthly and every month the number of rows change. The columns don't change but the amount of data does. My previous macro's I have just chosen the entire column to copy and paste onto our companies template. This worked fine for must things but has created some really long code and macros take a long time. I would like to write a code that counts how many rows are in a certain column and then from there copies and pastes that however many rows it counted in each column. Only a few columns contain data in every row, so I need it to count the rows in one specific column and apply to that every column. Any help would be appreciated.
Thanks
Tony
Hi Guys,
Still having issues with this, below I pasted the code I'm using if anyone can see why it won't run please help.
Windows("mmuworking2.xlsx").Activate
Workbooks.Open Filename:= _
"C:\Users\I53014\Desktop\QC DOCS\Sample_Data_Import_Template.xlsx"
Windows("mmuworking2.xlsx").Activate
Dim COL As Integer
COL = Range("A:DB").Columns.Select
**Range(Cells(2, COL), Cells(Range("E" & Rows.Count).End(xlUp).Row, COL)).Copy Destination:=Windows("Sample_Data_Import_Template.xlsx").Range("A2")**
Range("A2").Paste
Range("A5000").Formula = "='C:\Users\I53014\Desktop\[Import_Creator.xlsm]sheet1'!$B$2"
ActiveWorkbook.SaveAs Filename:="Range (A5000)", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
I bolded where it keeps stopping.
This should give you the last row containing data:
ActiveSheet.UsedRange.Rows.Count
This will give you the last row in a specific column:
Range("B" & Rows.Count).End(xlUp).Row
here is an example of how I can copy every row in the first three columns of a worksheet
Sub Example()
Dim LastRow As Long
LastRow = ActiveSheet.UsedRange.Rows.Count
Range(Cells(1, 1), Cells(LastRow, 3)).Copy Destination:=Sheet2.Range("A1")
End Sub
You have to be careful as there are some caveats to both methods.
ActiveSheet.UsedRange may include cells that do not have any data if the cells were not cleaned up properly.
Range("A" & Rows.Count).End(xlUp).Row will only return the number of rows in the specified column.
Rows(Rows.Count).End(xlUp).Row will only return the number of rows in the first column.
Edit Added an example
Edit2 Changed the example to be a bit more clear
For this example lets say we have this data
You could copy any other column down to the number of rows in column A using this method:
Sub Example()
Dim Col as Integer
Col = Columns("C:C").Column
'This would copy all data from C1 to C5
'Cells(1, Col) = Cell C1, because C1 is row 1 column 3
Range(Cells(1, Col), Cells(Range("A" & Rows.Count).End(xlUp).Row, Col)).Copy Destination:=Sheet2.Range("A1")
End Sub
The end result would be this: