How to match data using Excel or Access [closed] - vba

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

Related

Delete/hide 9 out of every 10 rows? [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 cannot find a method to do the following:
Say I have a data set of anywhere between 5-50,000 rows of data output (only ~20ish columns). I would like to graph this data set but it is quite memory taxing. Is there a macro to hide 9 out of every 10 rows to essentially cut the dataset down to 10% (more manageable to graph)? The data is collected every few seconds so I can easily cut out superfluous data without impact to the graph.
I've tried "delete/hide every other row loops" but it still leaves a large amount of information. I would think a 90% (or, if tunable, an X% cut) would help best. Thanks for any comments.
This code will randomly select/delete 90% of rows. It might not be exactly 90% because of randomness. Plenty of methods of ensuring exactly 90% but I prefer this for simplicity.
Sub RemoveRows()
Dim inputRange As Range, removeRange As Range, r As Range
Set inputRange = Sheet1.Range("A1:A1000")
Randomize
For Each r In inputRange
If Rnd < 0.9 Then
If removeRange Is Nothing Then
Set removeRange = r
Else
Set removeRange = Union(removeRange, r)
End If
End If
Next r
removeRange.EntireRow.Select
'removeRange.EntireRow.Delete
End Sub
Below code will hide every x number of rows in the range. You can assign value of x in variable rowCnt which is =9 in the code.
Sub Demo()
Dim rng As Range, cel As Range, hideRng As Range
Dim lastRow As Long, rowCnt As Long, i As Long
Dim ws As Worksheet
rowCnt = 9 'number of rows to hide
Set ws = ThisWorkbook.Sheets("Sheet5") 'change Sheet5 to your data sheet
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row with data using Column A
For i = 2 To lastRow Step rowCnt + 1 'loop through Column A starting from Row 2
If hideRng Is Nothing Then
Set hideRng = Range(.Cells(i, 1), .Cells(i + rowCnt - 1, 1)) 'get range of 9 cells
Else
Set hideRng = Union(hideRng, Range(.Cells(i, 1), .Cells(i + rowCnt - 1, 1))) 'union range of 9 cells
End If
Next i
End With
hideRng.EntireRow.Select 'use this line to select rows
'hideRng.EntireRow.Hidden = True 'use this line to hide rows
'hideRng.EntireRow.Delete 'use this line to delete rows
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

VLOOKUP through dates and copying the values [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 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

Excel VBA: locate details in one sheet, then copy (append) to another [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 8 years ago.
Improve this question
I would really appreciate your help with building some macro logic. I've messed around modifying recorded macros in the past, but am not familiar enough with VBA to build something from scratch. I've looked over the many examples here as well (mindboggling!), but did not find anything close enough to what I need to do...
I know it probably only takes a few lines of code - which makes it even more frustrating. Can someone please put me on the right track?
I have a single workbook with two sheets. Sheet1 contains a long list (~25k rows, but variable) of product details. Column A has the product ID, then columns B through G hold specific details on each product. Sheet2 is similar, again with product ID in column A and (different) properties in columns B through E. Sheet2 is much smaller, at about 100 rows (also variable).
What I need to do is to loop through the products (rows) in Sheet2, find the corresponding Product ID in Sheet1, and copy/paste the product properties in Sheet1 (B through G) to Sheet2 (to the right of the existing properties, so starting at column F in my example) - effectively merging all product properties in Sheet2.
I would be very grateful if one of you wizzards can provide skeleton code for that....
Set the two ranges from your two sheets, loop through them and find matches in product id, then just copy the values from the first sheet into the second one. Also replace the ranges to match your conditions.
Sub search()
Dim cell As Range, rng As Range, rng2 As Range, cell1 As Range, n As Integer, m As Integer
Set rng = Sheet1.Range("A2:A9")
Set rng2 = Sheet2.Range("A2:A3")
n = 1
m = 1
For Each cell In rng
n = n + 1
For Each cell1 In rng2
m = m + 1
If cell.Value = cell1.Value Then
Sheets("Sheet2").Range("F" & m & ":J" & m).Value = Sheets("Sheet1").Range("B" & n & ":F" & n).Value
End If
Next cell1
m = 1
Next cell
End Sub

Searching for a particular value then pasting into another workbook [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Let me give you a basic understanding of my what I'm trying to do. I have two workbooks: Master Workbook and Workbook A. Information in Workbook A will be inputted into the Master Workbook. In Workbook A, there is Column X with numbers between the ranges of 1 to 25. All I care about are values greater than 14.
Problem: How do I create a VBA function that looks at Column X (Row 1) to see if it is greater than 14? If it is then it copies the entire row and pastes it into the Master Workbook, else it moves onto Column X2. Also, after copying row 1 and pasting into Master Workbook, I also need it to go back to Workbook A and check the rest of Column X if it is greater 14.
Thank you so much in advance!
This code should do what you want:
Private Sub checkAndCopy()
Dim i As Integer
Dim lastRow As Integer
Dim foundItem As String
Dim j As Integer
Dim pasteTo As Range
Dim k As Integer
k = 0
lastRow = WorksheetFunction.CountA(Range("A:A"))
For i = 1 To lastRow
If Cells(i, 24).Value > 14 Then
k = k + 1
For j = 1 To 24
foundItem = Cells(i, j).Value
Set pasteTo = Workbook(yourWorkbook).Cells(k, j)
pasteTo.Value = foundItem
Next j
End If
Next i
End Sub
Note that it copies into the new workbook starting on Row 1. You can have it search for the next empty line and add to that by including:
k = Workbook(yourWorkbook).WorksheetFunction.CountA(Range("A:A")) + 1
I hope this helps!