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
Related
I have a for loop that iterates through each sheet in my workbook. Each sheet is identical for most purposes. I am making an overview sheet in my workbook to express the results from the data in the other sheets(the ones that are identical).
There are 11 vehicles, each with their own sheet that has data from a test from each day. On any given day there can be no tests, or all the way to 30,000 tests. The header of each column in row 47 states the date in a "06/01/2021 ... 06/30/2021" format. The data from each iteration of the test will be pasted in the column of the correct date starting at row 49.
So what my overview page needs to display is the data from the previous day. In one cell on my overview there is a formula for obtaining just the number of the day of the month like 20 or 1 etc. Using this number, the number of the day is the same as the column index that the previous day's data will be in conveniently. So in my for loop I want to have a table that has the name of each sheet in column B, in column C I want to display the total number of tests done in that day(not the sum of all the data), in column D I need the number of tests with a result of 0, in column E I need the number of tests that have a result above the upper tolerance, and in column F I need the number of tests that have a result below the lower tolerance minus the result in column D.
I've been playing with the Application.WorksheetFunction.Count and CountIf functions but I keep getting 0 for every single cell. I made two arrays for the upper and lower tolerance values which are type Longs called UTol and LTol respectively. TabList is a public array that has each of the sheet names for the vehicles stored as strings. finddate is an integer that reads in Today's day number which is yesterday's column index. I've included a picture of the table in question and my for loop code:
finddate = Worksheets("Overview").Range("A18").Value
For TabNRow = 1 To 11
startcol = 3
Worksheets(TabList(TabNRow)).Activate
Debug.Print (TabList(TabNRow))
'Total number of tests done that day
Worksheets("Overview").Cells(startrow, startcol).Value = Application.WorksheetFunction.Count(Range(Cells(49, finddate), Cells(30000, finddate)))
startcol = 4
'Total number of 0 results, this used to get the number of 0's from each sheet but that row has since been deleted
Worksheets("Overview").Cells(startrow, startcol).Value = Worksheets(TabList(TabNRow)).Cells(48, finddate).Value
startcol = 5
'Total number of results above tolerance
Worksheets("Overview").Cells(startrow, startcol).Value = Application.WorksheetFunction.CountIf(Range(Cells(49, finddate), Cells(30000, finddate)), ">" & UTol(TabNRow))
startcol = 6
'Total number of results below tolerance
Worksheets("Overview").Cells(startrow, startcol).Value = Application.WorksheetFunction.CountIf(Range(Cells(49, finddate), Cells(30000, finddate)), "<" & LTol(TabNRow))
startrow = startrow + 1
Next TabNRow
```
No need to select/activate sheets when referencing them. See: How to avoid using Select in Excel VBA
Something like this should work:
Dim wsOv As Worksheet, wsData As Worksheet, rngData As Range
Set wsOv = ThisWorkbook.Worksheets("Overview")
finddate = wsOv.Range("A18").Value
For TabNRow = 1 To 11
Set wsData = ThisWorkbook.Worksheets(TabList(TabNRow))
Set rngData = wsData.Range(wsData.Cells(49, finddate), _
wsData.Cells(Rows.Count, finddate).End(xlUp))
Debug.Print wsData.Name, rngData.Address 'edited
With wsOv.Rows(2 + TabNRow)
.Columns("C").Value = Application.Count(rngData)
.Columns("D").Value = Application.CountIf(rngData, 0)
.Columns("E").Value = Application.CountIf(rngData, ">" & UTol(TabNRow))
.Columns("F").Value = Application.CountIf(rngData, "<" & LTol(TabNRow)) _
- .Columns("D").Value
End With
Next TabNRow
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 would like to draw automatically accumulated inventory plot using Excel VBA 2016.
I have inventory data for different year in the format of "20XXYY". Here "XX" indicates year and "YY" indicates week number in a year (1 to 52). I will have data similar to picture 1. Then I would like to sort it as picture 2. Finally draw plot as picture 3. However, I would like to have it automatically using VBA.
My question is that how can I create sequential YearWeek column automatically for the plot?
I have used "=SUM($Y$2:Y2)"^^ formula in "Accumulated Inventory" column. I will highly appreciate if I will get any clue.
'This is the answer. I have figure it out. Thanks for your support.
Sub consecutiveNumber()
Dim wsFrom As Worksheet
Dim Com As String
Dim lLastNumber,FirstNumber, l, i As Long
'change to names of sheets you are coptying from and to
Set wsFrom = ThisWorkbook.Sheets("Sheet1")
'Get the value in the last and first used text box of column A
lLastNumber = wsFrom.Range("A" & Rows.Count).End(xlUp).Value
FirstNumber = wsFrom.Range("A1").Value
'clear column B
wsFrom.Range("B:B").Clear
'Initializing the cell index value
i = 1
'fill column B on sheet 1 with first to last number on column A
For l = FirstNumber To lLastNumber
'Converting number to string
Com = Right(CStr(l), 2)
If CInt(Com) >= 53 Then
l = l + 48
wsFrom.Range("M" & i) = l
i = i + 1
Else
wsFrom.Range("M" & i) = l
i = i + 1
End If
Next
End Sub
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
My other macro scrapes mutual fund sizes from web and returns the values in Worksheets("Updater").Cells(xx,4) (so column C). The macro scrapes also the date in which the fund has been updated on morningstar and returns it in column F on the same sheet. I've got 83 fund names in column A.
On a sheet called "Tracker" I've got the fund names transposed. They start from cell (1,4) and go to (1,86). In column B I've got DATEVALUE of dates in column C. There're some issues with the date formats so I'll just hide the column B later.
The below macro works as it is, but when you uncomment lines 11 and 22, it doesn't work as intended anymore. Idea is to look at the fund sizes on sheet "Updater" and return the fund size to the corresponding date on sheet "Tracker". As said, this works.
However, with the nested if statement, I'd want to modify the macro so it would keep all the cells that contain other than "-" untouched. This way I would start to cumulate the fund size data when the macro is executed on a daily basis and doesn't overwrite the old fund sizes with "-".
Now the macro flips the funds who knows where and I simply can't solve this nested if statement problem. Is this even the correct/best way to get where I want?
Sub fundSizeTracker()
Dim f As Integer
Dim numRows As Integer
numRows = 86
f = 2 'Sheet fund names & sizes
For m = 2 To 4 'Sheet row number, starting at rowNo 2
For i = 4 To numRows 'Sheet column number (set to include all 83 funds)
' If Worksheets("Tracker").Cells(m, i) = "-" Then
' Are Datevalues same? 'Tracker datevalue 'Updater datevalue
If StrComp(Worksheets("Tracker").Cells(m, 2).Value, Worksheets("Updater").Cells(f, 13).Value) = 0 Then
Worksheets("Tracker").Cells(m, i) = Worksheets("Updater").Cells(f, 4).Value
Else
Worksheets("Tracker").Cells(m, i) = "-"
End If
If f = numRows - 1 Then
f = 2
End If
f = f + 1
' End If
Next i
Next m
End Sub