I have a table in excel with a lot of data and dates. Like this:
I open a second sheet and created a formula to extract data from the first sheet that fall between two dates. Like this:
Now I want to be able to change the data on the new sheet and the old sheet will automatically get updated. For example I want to be able to change N to Y under Shipped and have the first sheet be updated to Y.
Can anyone tell me how I can do that?
please use VBA:
open VBA editor (ALT+F11), add new module and paste code below.
After enterering all corrections to your second sheet, please run macro.
It isn't optimal solution but it should work:
Sub CopyChangesInSheepment()
Set wksSheet1 = Worksheets("sheet1") 'put name of yours FIRST sheet here
Set wksSheet2 = Worksheets("sheet2") 'put name of yours SECOND sheet here
F = wksSheet1.Range("a1").CurrentRegion.Rows.Count
S = wksSheet2.Range("a6").CurrentRegion.Rows.Count
For j = 6 To S
For i = 1 To F
id1 = wksSheet1.Cells(i, 1)
id2 = wksSheet2.Cells(j, 1)
ord1 = wksSheet1.Cells(i, 8)
ord2 = wksSheet2.Cells(j, 8)
If id1 = id2 And ord1 = ord2 Then 'check if order number and sku are the same
wksSheet1.Cells(i, 24) = wksSheet2.Cells(j, 15) 'column shipped is 24. column (X) in sheet1 and 15. (O) in sheet2
End If
Next i
Next j
End Sub
Related
I have two sheets :
Sheet 1 consist of :
Sheet 2 consist of :
And the output should show in M column in Sheet1. I am attaching the sample output here :
So,what I have here is ID in Sheet 1, for eg : ID 'US' has Abhay,Carl and Dev
and in Sheet3, I have names in column and ID in Rows.
What i want is my Sample output column should populate using macro based on matched values from Sheet3
I am using below logic but something is going wrong :
For i = 2 To 10
j = i + 1
If ThisWorkbook.Sheets("Input").Range("N" & i) = ThisWorkbook.Sheets("Sheet3").Range("A" & i) And ThisWorkbook.Sheets("Input").Range("K" & i) = ThisWorkbook.Sheets("Sheet3").Range("B1") Then
ThisWorkbook.Sheets("Input").Range("O" & i) = ThisWorkbook.Sheets("Sheet3").Range("B" & j)
End If
Next i
Since you asked for a VBA solution, please see the code below.
Dim colLen As Integer
Dim i As Integer
Dim colPt As Integer
Dim rowPt As Integer
' Counts number of rows on Sheet 1, column B.
colLen = Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row
' Loops through all names on Sheet 1.
For i = 2 To colLen
' Retain US or NA ID for blank cells.
If Sheets(1).Cells(i, 1) <> "" Then
If Sheets(1).Cells(i, 1) = "US" Then
colPt = 2
Else
colPt = 3
End If
End If
' Find name on Sheet 2 and set row.
rowPt = Sheets(2).Range("A:A").Find(Sheets(1).Cells(i, 2)).Row
' Add ID from Sheet 2 to Sheet 3
Sheets(1).Cells(i, 3) = Sheets(2).Cells(rowPt, colPt)
Next i
Assumptions:
Sheet 1 is the main worksheet, sheet 2 has the lookup data.
All names in the lookup data are unique.
I would recommend including the ID in every row instead of treating it as a heading but that's preference. There are formula solutions that would work for this as well if you want to skip VBA.
There are a few ways to approach this. Below is one of them:
NOTE: for simplicity, I have kept my data on one sheet. You can amend the below formulas as your data is on 2 sheets. Saying that, I have used the same columns as you have in your query
Solution:
Have a "holding column". In my example, I used column J as the holding column (you can hide this column if you want). In J2, type the following formula: =IF(ISBLANK($K2), $J1,$K2). Copy the formula down to all used rows. Then copy the following formula in M2: =VLOOKUP($L2,$A$3:$C$8,IF($J2="US",2,3),FALSE). As per before, copy the formula down to all used rows. This should give you your results
I am working with a VB macro. Essentially what I am trying to do is for the macros to read the input and first determine whether or not a cells ID number matches the one in the row. Example: If row 1 has an ID of 1122 and rows 2,3,4 and 5 all match, I want the macro to read that and create a count in the NbrOfA cell. Once it realizes that there is not an ID match it moves on to the next ID and looks for matches of that ID number and continues to create a count. While it is doing this, I also need it to read from another column that has specific strings such as "open", "closed" ect. read that input, and create a separate row titled NbrofOpenA. Once it runs out of data, I then want to have a singular cell that shows the number of actions (NbrOfA) that match the ID number as well as the number of open actions (NbrOfOpenA).
Currently I receive the error: “compile error: sub or function not defined” highlighting the Set Cell(Sheet2.Cells(FirstRowOfI, 23) = NbrOfA
Attached in the excel sheet attached it shows 2 cells deleted. They will not actually be deleted, just wanted to give an idea of what I was looking for
Sub ACount()
Dim FirstRowofI
Dim NbrOfA as Integer
Dim NbrOfOpenA as Integer
Row = 2
Set FirstRowofI = (Sheet2.Cells.Range(Row, 14))
NbrOfA = 0
NbrOfOpenA = 0
If (Sheet2.Cells(Row, 14).Value <> "") Then
NbrOfA = 1
If (Sheet2.Cells(Row, 22) <> "Closed") Then
NbrOfOpenA = 1
Set Row = FirstRowofI
Row = Row + 1
Do While (Sheet2.Cells(Row, 14) = (Sheet2.Cells(FirstRowofI, 14)))
NbrOfOpenA = NbrOfOpenA + 1
If (Sheet2.Cells(Row, 22) <> "Closed" Then
NbrOfOpenA = NbrOfOpenA + 1
Range(Row).EntireRow.Delete
Return
End If
Set Cell(Sheet2.Cells(FirstRowofI, 23)) = NbrOfA
Set Cell(Sheet2.Cells(FirstRowofI, 24)) = NbrOfOpenA
Loop
End Sub
[1
Do you need VBA? You can easily achieve what you're looking for with formulas, heck even a Pivot Table! Here's an example with formulas:
I have 2 rows of numbers in a source workbook. I would like to copy the first row into another workbook, then make the row below it the sum of the source's first & second rows. My code runs but I can't seem to get excel to read the values of the second row. (aa is the source workbook's sheet)
With aa
For i = 1 To Range(Range("U2").Offset(1, 0), Range("U2").Offset(1, 0).End(xlToRight)).Cells.Count
Set NewValue = Range("U2").Offset(1, i - 1)
TargetWB.Worksheets("Sheet1").Range("D2").Offset(1, i - 1).Value = Range("D2").Offset(0, i - 1) + NewValue.Value
Next
End With
I am having a problem with finding duplicates in an excel column that is created via VBscript.
I currently am grabbing data from a DB opening an excel file, placing the data within and then sorting the data alphabetically ascending on column E (if this isn't needed it can easily be removed).
Now the problem that I am faced with is that I am trying to find any duplicates within that column E (Errors).
If there is a duplicate I would like to copy the duplicate and paste it into another sheet (column A) that I have created
Set oWS7 = oWB.Worksheets(7)
oWB.Sheets(7).Name = "Dups"
And in column B of oWS7 I would like to put all the corresponding column C's (accounts) from the original worksheet.
So that there would be a 1 Error to many account's ratio. If there are no duplicates I would like to have them left alone. I'm not sure how clear this is but any questions/help on this would be much appreciated.
Thanks in advance.
I'm going to make the following assumptions:
The content in the worksheet starts in the first row
The first row (and only the first row) is a header row.
There are no empty rows between header row and data rows.
The data is already sorted.
If these assumptions apply the following should work (once you put in the correct sheet number):
Set data = oWB.Sheets(...) '<-- insert correct sheet number here
j = 1
For i = 3 To data.UsedRange.Rows.Count
If data.Cells(i, 5).Value = data.Cells(i-1, 5).Value Then
oWS7.Cells(j, 1).Value = data.Cells(i, 5).Value
oWS7.Cells(j, 2).Value = data.Cells(i, 3).Value
j = j + 1
End If
Next
'How To find Repeted Cell values from source excel sheet.
Set oXL = CreateObject("Excel.application")
oXL.Visible = True
Set oWB = oXL.Workbooks.Open("ExcelFilePath")
Set oSheet = oWB.Worksheets("Sheet1") 'Source Sheet in workbook
r = oSheet.usedrange.rows.Count
c = oSheet.usedrange.columns.Count
inttotal = 0
For i = 1 To r
For j = 1 To c
If oSheet.Cells(i,j).Value = "aaaa" Then
inttotal = inttotal+1
End If
Next
Next
MsgBox inttotal
oWB.Close
oXL.Quit
I am trying to develop a Macro in Excel 2010 that can pull data from sheet 2 based on the date that the macro is run, and provide a total of the different drawing statuses in sheet 1 based on the drawing statuses given in sheet 2. For example, if I run the macro on 3/18/2011, the sheet 1 (3/18/2011) Row should have the totals of ColB = 4, ColC = 0, ColD = 2, ColE = 1, ColF = 3. So each day I run the macro, sheet 1 will get updated based on the current date and updated data from sheet 2 on the same file. Can anyone give me a hint on how to start this code?
Sheet 1 data is as follows:
A B C D E F
Date Preliminary Review Design Construction Final
The Date column has the following rows:
3/18/2011
3/20/2011
3/21/2011
3/22/2011
and so one till end of the month
Sheet 2 data is as follows:
Col L has the drawing numbers while Col M has the drawing status. Below is the drawing number with status on sheet 2
L M
**Drawing Status**
DWGT2010001 Design
DWGT2010002 Preliminary
DWGT2010003 Final
DWGT2010004 Preliminary
DWGT2010005 Design
DWGT2010006 Construction
DWGT2010007 Final
DWGT2010008 Preliminary
DWGT2010009 Preliminary
DWGT2010010 Final
This will work, though you could modify it more based on your specific layout.
Private Sub CommandButton1_Click()
Dim WorkingRow As Range
Dim StatusColumn As Integer
Dim i As Integer
Dim ColumnNames(1 To 5) As String
Set WorkingRow = Sheets("Sheet1").UsedRange.Find(Date, LookIn:=xlValues, _
SearchOrder:=xlByRows).Rows(1)
StatusColumn = 13 ' Column M on Sheet 2
ColumnNames(1) = "Preliminary"
ColumnNames(2) = "Review"
ColumnNames(3) = "Design"
ColumnNames(4) = "Construction"
ColumnNames(5) = "Final"
For i = 1 To 5
WorkingRow.Cells(1, i + 1).Value = _
Application.CountIf(Sheets("Sheet2").Columns(StatusColumn), ColumnNames(i))
Next i
End Sub
I am not sure i understood well what you wanted to do...
As a start, you can try to record a macro doing what you want and adapt the code :
http://office.microsoft.com/en-us/excel-help/create-a-macro-HP005204711.aspx