VLOOKUP through dates and copying the values [closed] - vba

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table in Prices worksheet which consists of prices and dates. I am trying to use VLOOKUP with VBA to look for prices pertaining to the month of January and then copying these prices onto another worksheet. But because the dates are in DATE format, I am stuck with the VBA code.
Eg.
Column A shows the dates
Column B shows the prices.
There maybe prices shown on different dates e.g. 01/01/2014 or 02/01/2011 etc
I wanted to copy the prices for January.

You can use this, but it is only taking into account the prices that are for January. It's easy enough to make it work for all the months, but I'm not sure how you would want to lay that out. This grabs all January, no matter what the year. If you wanted to add the year as well, you'd just nest another IF statement. Make the first IF YEAR, and the second Month.
Sub MonthFromDate()
Dim tempMonth As Long
Dim tempDate As Date
Dim lastSourceRow As Long, tRow As Long
Dim source As String, target As String
source = "Prices" 'Source Sheet Name is set here
target = "Annual Prices" 'Target Sheet
tRow = Sheets(target).Range("A" & Rows.count).End(xlUp).row + 1
lastSourceRow = Sheets(source).Range("A" & Rows.count).End(xlUp).row
For lRow = 2 To lastSourceRow 'Start looping through source sheet at Row 2 for Headers
tempDate = Sheets(source).Cells(lRow, 1)
tempMonth = Month(tempDate)
If tempMonth = 1 Then 'This is where you would insert a VARIABLE for 1.
Sheets(target).Cells(tRow, 1).Value = Sheets(source).Cells(lRow, 1).Value
Sheets(target).Cells(tRow, 2).Value = Sheets(source).Cells(lRow, 2).Value
tRow = tRow + 1
End If
Next lRow
End Sub

Related

I would like to copy data into a new row on excel and create a new input row [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to VBA on excel and I am trying to automate a spreadsheet as much as possible.
I have attached an image of an example sheet. What I am trying to do is copy the data in cells K9/K10 into a new row on the table on the left. This table increases in number of rows every week and I want to be able to automate this process, so I can click a button and the data is inputted in the correct columns automatically and everything autofills with it.
I hope this makes sense!
Thanks!
I'm not sure how you are calculating the value for Processed, but the code below will add a new row and populate every field apart from that one, simply put the code under the Sheet you are using.
This also assumes that your Percentage is calculated with a formula, which will get dropped into the new row without having to explicitly add it.
It will check for changes in cell K10 and if something has changed it will take them values and add a row to your table:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$K$10" Then
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
ws.Cells(LastRow + 1, 1).Value = ws.Cells(LastRow, 1) + 1 'add one to the Week Number
ws.Cells(LastRow + 1, 2).Value = ws.Cells(LastRow, 2) + 7 'add 7 to the Week Beginning Date
ws.Cells(LastRow + 1, 3).Value = ws.Range("K9").Value + ws.Range("K10").Value 'get the Total
ws.Cells(LastRow + 1, 4).Value = ws.Range("K9").Value 'copy the Passes
ws.Cells(LastRow + 1, 5).Value = ws.Range("K10").Value 'copy the Fails
End If
End Sub
All you need to do is, open your Workbook, press Alt+F11, then the VBA environment will open up, then double-click on the Sheet you are using and paste it there, like in the image below:

Excel data entry with date matching plus move to another sheet after table run out [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to VBA and would like to learn by creating expense database, How do I transferring and making monthly database?
If dashboard date is matching to Aug17 column A, move data from dashboard to Aug17 respective row. If possible, I would like it to search dashboard date to all worksheet and move data to respective row if matching found. Thanks in advance.
DashBoard
Aug17
Based on your response to my questions in the comments here's code that does what you asked. Notice that the final msgbox will never be encountered if the date is found. Hopefully you will be able to adjust this code to suit your needs once you understand it.
Sub test()
Dim r As Range, dashSh As Worksheet, dashR As Range, sh As Worksheet
Dim mo As String, yr As String
Set dashSh = Worksheets("Dashboard")
Set dashR = dashSh.Range("A5:J5")
mo = Application.WorksheetFunction.Text(dashR.Columns(1), "mmm")
yr = Application.WorksheetFunction.Text(dashR.Columns(1), "yy")
Set sh = Worksheets(mo & yr)
sh.Activate
Set r = sh.Range("A5")
While r <> ""
If r = dashR.Columns(1) Then
r.Select
dashR.Copy
sh.Paste
End
End If
Set r = r.Offset(1, 0)
Wend
MsgBox ("date not found")
End Sub

Merge columns where cells have same values, excluding "0"s [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've seen many questions on merging, but nothing that I can manipulate (in my entry-level abilities) to answer this particular question and would be forever be grateful for your expertise and help!!!
I am looking to merge different groups of cells with the same values for particular ranges.
Below is an example of inputs and the desired outcome. I have the file formatted so that the "0"s and "no"s don't show, however the actual descriptions (substituted with like 50% off) are quite long and cannot be viewed in a single cell, hence the need for merge cells to better display information. There are also multiple stores with new stores being added weekly, thus I would like to avoid merging cells manually.
Input
Month January February March April May
Store 1 Campaign Period no yes yes yes no
Campaign Details 0 50% off 50% off 50% off 0
Store 2 Campaign Period no no no yes yes
Campaign Details 0 0 0 spring fling spring fling
Desired Output
Month January February March April May
Store 1 Campaign Period no yes yes yes no
Campaign Details 0 50% off 0
Store 2 Campaign Period no no no yes yes
Campaign Details 0 0 0 spring fling
This should get you started. The data is assumed to be in "Sheet1". It will merge cells in rows that are labeled with "Campaign Details" in Column 'A'. The merge is performed on adjacent row cells that have the same value - at least two adjacent cells with the same value will be merged.
Option Explicit
Sub MergeSameDetails()
Dim sht As Worksheet
Set sht = Worksheets("Sheet1")
Application.DisplayAlerts = False
With sht
Dim lastrow As Integer, i As Integer, j As Integer, cnt As Integer
Dim val As Variant
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To lastrow
If .Cells(i, "A").Value = "Campaign Details" Then
cnt = 1
val = .Cells(i, 2).Value
For j = 3 To 7
If val = .Cells(i, j).Value Then
cnt = cnt + 1
Else
If cnt >= 2 And val <> "0" Then
.Range(Cells(i, j - cnt), Cells(i, j - 1)).Merge
.Cells(i, j - cnt).HorizontalAlignment = xlCenter
End If
cnt = 1
val = .Cells(i, j).Value
End If
Next
End If
Next
End With
Application.DisplayAlerts = True
End Sub

VBA code to transpose one multiple data column into several columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Hello I have a list of 65000 rows of stocks price for one column, and I would like to adapt it like in the second pic, anyone has any idea how to code it with vba ? Thank you!
Assuming that the data structure is always the same (12 months of data and 3 rows for data set id's). Change Sheet Name on Code Line 9 to suit your Sheet Name
Sub TRANSPOSE_DATA()
Dim lRow, i, x As Long
'------------------
With ThisWorkbook
'ADD OUTPUT WORKSHEET
.Sheets.Add After:=.Sheets(.Sheets.Count) 'add a sheet for output
.ActiveSheet.Name = "OUTPUT" 'sheet rename
'COPY DATA
With .Sheets("Hoja1") 'Change sheet name for yours
lRow = .Range("A1048576").End(xlUp).Row 'last row definition
.Range("A1:B15").Copy Destination:=ThisWorkbook.Sheets("OUTPUT").Range("A1") 'First dataset copy (including headers)
x = 3 'first iteration column definition
For i = 16 To lRow - 14
.Range("B" & i & ":B" & i + 14).Copy Destination:=ThisWorkbook.Sheets("OUTPUT").Cells(1, x) 'copy data from each iteration
x = x + 1 'transpose 1 column for next iteration
i = i + 14 'transpose 12 months plus header rows for next iteration
Next i
End With
End With
End Sub

How to match data using Excel or Access [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a master sheet with 10,000 entries that has a common identifier in column A. I have many other sheets that have data and the same common ID, but for smaller populations. For example, there is a sheet for 1,500 senior citizens. Column A is the unique ID, Column B is a y for SENIOR_CITIZEN. How do I match these two sheets so that in my master there will be a new column in the master identifies the matches from the 1500 IDs from the senior citizen sheet to the 10,000 IDs on the master? VLOOKUP won't work because there are way more entries in the master than in the senior citizen file.
Using simple loops, this is very easy to accomplish. I'm not sure if I understood your question exactly, but I'm pretty certain this is what you want.
Note: in the code, I use Cells(sRow, "B") once and (tRow, 2) another time. They accomplish the same thing, and I'm writing it like that to show you how it works. You can either set the column value with a letter or a "LONG" typed variable. This lets you use a counter and loop through the rows and columns, skipping about however you see fit, logically for your needs.
Not knowing your sheet names, using "Seniors" and "Master":
TESTED:
Sub SeniorMatch()
Dim sh1 As String
Dim sh2 As String
Dim lastRow1 As Long 'For sh1
Dim lastRow2 As Long 'sh2
Dim tempID As String 'In case you use any letters in your ID
sh1 = "Seniors" 'Set the Sheet Names
sh2 = "Master"
lastRow1 = Sheets(sh1).Range("A" & Rows.Count).End(xlUp).row
lastRow2 = Sheets(sh2).Range("A" & Rows.Count).End(xlUp).row
'using sRow for SOURCE Row, and tRow for Target Row
For sRow = 2 To lastRow1
If Sheets(sh1).Cells(sRow, "B").Value = LCase("y") Then
tempID = Sheets(sh1).Cells(sRow, 1).Text
For tRow = 2 To lastRow2
If Sheets(sh2).Cells(tRow, 1) = tempID Then
Sheets(sh2).Cells(tRow, 2) = "y" 'Set col B to "y"
End If
Next tRow
End If
Next sRow
End Sub