VBA code to transpose one multiple data column into several columns [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 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

Related

Multiple results with VBA Vlookup [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 have got two sheets, one with data like this:
For one person there can be couple of rows with answers.
And second sheet with this data:
Here one person had only one row with answers.
Based on the surnames from second table I want to search the person in first table and if the person is present copy the whole row to another sheet. So my final output table will looks like this:
My idea is that the algorithm should take the surname from table in Data 2 sheet on look for it in Data 1 sheet if is present then copy whole row into A3 Output sheet, next search the rest of Data 2 for another appearance. If blank cell then take another surname from Data 2 table and do it up to the point where in Data 2 table blank space is detected. But I have no idea how to translate it into VBA code.
Could anyone help with some clues? Or macro that I can use here?
I would be really thankful for any help.
Here's a VBA subroutine that I believe will do what you asked, not sure if it is what you want. This is assuming you already have a tab to receive the data to be copied; the tabs being used are MRWV1 = names to select, MRWV2 = Data sheet of rows to copy and MRWV3 = sheet to receive the copied data.
Sub MRWV()
'
' MRWV Macro
'
' Housekeeping
vFoundKt = 1
vSourceRows = 0
vDataRows = 0
' Select the sheet with the five rows with the names to select
Sheets("MRWV1").Select
vSourceRows = Cells(Rows.Count, 1).End(xlUp).Row
Sheets("MRWV2").Select
vDataRows = Cells(Rows.Count, 1).End(xlUp).Row
'Get names from source sheet
For iSource = 2 To vSourceRows
Sheets("MRWV1").Select
Range("A" & iSource).Select
vSourceName1 = ActiveCell.Value
Range("B" & iSource).Select
vSourceName2 = ActiveCell.Value
'Look through data sheet for matching names
For iData = 2 To vDataRows
Sheets("MRWV2").Select
Range("A" & iData).Select
vDataName1 = ActiveCell.Value
Range("B" & iData).Select
vDataName2 = ActiveCell.Value
If vSourceName1 = vDataName1 And vSourceName2 = vDataName2 Then
vFoundKt = vFoundKt + 1
Range("A" & iData & ":I" & iData).Select
Selection.Copy
Sheets("MRWV3").Select
Range("C" & vFoundKt).Select
Selection.PasteSpecial Paste:=xlPasteValues,Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Next
Next
End Sub

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:

VBA - Add Sheets based on a value and name accodingly [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 5 years ago.
Improve this question
I am having trouble with my code. I need to create new sheets and name them according to the name of a machine. Which is the range "B" & (12 * x - 5). Yes the range changes based on x which in this example is equal to 1 to 33, and is within the proper For statement. However my code is creating a large number of sheets and not just 11, in this example. Sheets("Tool Setup").Range("C18") = 11 in this example. Also, my intention is to name these sheets according to the value in Range "B" & (12 * x - 5).
Dim Sheetcnt As Integer, Tabs As Integer
Sheet = ThisWorkbook.Names(Sheets("Reporting").Range("B" & (12 * x - 5))).RefersToRange.Value
Sheetcnt = Sheets("Tool Setup").Range("C18")
For Tabs = 1 To Sheetcnt
Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = Sheet 'renames the new worksheet
Next Tabs
If someone can help me figure out how to create a code that will satisfy this problem, it would be much appreciated. I have a hunch it has to do with the variable Tabs.
Thank you in advance.
Based on what you've provided, I've come up with the code below which names 11 sheets according to a formula similar to yours. To demonstrate that it works, I placed consecutive numbers in the first 127 rows of Column B of the active sheet. Then, the code calculates which row to grab using the formula Range("B" & (12 * i - 5) That formula gives the following sequence: 7 19 31 43 55 67 79 91 103 115 127, and so 11 sheets with those numbers are made.
If this is not what you intended, perhaps you can use this working code as a place from which to start. Or, better yet, provide more definition for your question.
Option Explicit
Sub sheetNamer()
Dim num As Integer, i As Integer, sh As Worksheet
num = 11
Set sh = ActiveSheet
For i = 1 To num
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = sh.Range("B" & (12 * i - 5))
Next i
End Sub

How to do a loop within a loop vba [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 7 years ago.
Improve this question
I cannot figure out how to do a loop within a loop.
There is a list of words on sheet 1 that need to be copied and pasted if they match up with any of the 20 desired key words on sheet two, column 1.
This then needs to be copy pasted onto sheet 3. Then I need to look at the same list from sheet 1 and copy paste those onto sheet 4 if they match up with any of the key words from sheet 2, column 2. I could use any help.
Single loop
Dim i As Integer
For i = 1 To 6
Cells(i, 1).Value = 100
Next i
Double Loop
Dim i As Integer,
Dim j As Integer
For i = 1 To 6
For j = 1 To 2
Cells(i, j).Value = 100
Next j
Next i
Good Luck
You don't need 2 loops (which are slow)
Loop through the values in sheet 1 testing if they exists using this:
If WorksheetFunction.CountIf(Sheets("Sheet2").Range("A1:A20"), Range("A1")) > 0 Then
'Your copy and paste code goes here for sheet 3
ElseIf WorksheetFunction.CountIf(Sheets("Sheet2").Range("B1:B20"), Range("A1")) > 0 Then
'Your copy and paste code goes here for sheet 4
End If

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