Extract data from single row and paste into new worksheet - excel

I have some data in a worksheet in a single row (row 44) where the required data is in columns C,F,I,L and so on (i.e. data required every 3rd column starting from C).
This ends at column 'ET'
I need to extract this and paste it into another worksheet row where there are no column spaces.
I've looked around for solutions but its usually columns but this is data i need in one row.

Assuming this needs to be done even when the data in row 44 changes, you could do a macro. In a procedure, the following code could work as a guideline:
Public Sub copyover()
Dim c As Long
For c = 1 To 50
Worksheets("Sheet2").Cells(1, c).Value = _
Worksheets("Sheet1").Cells(44, c * 3).Value
Next
End Sub

Does this have to be a macro? Put this in the first destination cell on the other worksheet:
=INDEX(Sheet1!$C$44:$ET$44,1,3*(COLUMN(A1)-1)+1)
Then copy right

Related

Enter value to last row/first empty cell in a new column IN table using VBA

I have a problem with a vba macro that i can't seem to find the answer to anywhere. Feels like i've tried everyting so i'll put the question out there to see if anyone here can help me :)
My macro loops through 50 woorkbooks that all have a "Firstpage" where the data from all the other data worksheets are summarized. In that Firstpage i have a table called "Tabell_1". The table has a header row (B4:F4) and then one row for each data worksheet in the workbook and the a sum row. We have decided to add a new column (column D) to the table to add in data from a specific cell in all the other worksheets (B4).
I now loop through the data worksheets to copy the value in B4 and then i want to paste that value to the first empty row in the table on the "Firstpage" (starting from the cell under the header). The method to find the last row that i use in other parts of the macro doesn't work, it gives me the first row after the sum row and then pastes the values under the table.
The picture shows the table that i'm working with for one of the workbooks.
enter image description here
Hej Johanna!
Assuming that you are using listobjects to manage the data I would just do this..
Sub Test2()
Dim lo As ListObject
Dim lr As ListRow
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set lo = ws.ListObjects("Tabell_1")
Count = 1
For Each lr In lo.ListRows
If lo.ListColumns("Test2").DataBodyRange(lr.Index) = "" Then
lo.ListColumns("Test2").DataBodyRange(lr.Index) = Count
Count = Count + 1
End If
Next lr
End Sub
This is will fill the first empty row in column Test2.
Hopefully you can use this example :)

Creating a Copy/Paste VBA loop

I have a massive data set (1000s of values) in a large spreadsheet, and I need to copy and paste these values into a new sheet in sets of 10. Essentially I need to copy the first 10 cells in a column and paste them into their own column in a new sheet. I then need to repeat this with cells 11-20 and paste them into their own column in a new sheet. So: copy 1-10, paste into new sheet in column A. Then copy 11-20 and paste into same new sheet, column B. 21-30 in column C, and so on.
I have already tried some basic code, I am really new to this but find it interesting. I have figured out how to copy a range of 10 and paste to a new sheet, but I do not know how to set the loop up so I can continue this process into new columns with increasing sets of 10. I also have tried recording a macro, however it just recreates literally the keystrokes I input which isn't useful to me.
I wrote this to copy first 10 in my source column and paste to my new sheet in their new column.
Option Explicit
Sub CopyCells()
Worksheets("Sheet1").Range("A3:A12").Copy Worksheets("Sheet2").Range("B2:B11")
End Sub
I need it to loop and continuously cut and paste sets of 10 cells from one target column into a new sheet, and each set of 10 into their own new column. If you post an answer could you please add explanations as well? I really like this stuff and am trying to learn, not just copy paste some code. Thanks!
You can do something like this:
Sub CopyCells()
Const NUM_ROWS As Long = 10 'size of block to copy
Dim rngCopy As range, col As Long
Set rngCopy = Worksheets("Sheet1").Range("A3").Resize(NUM_ROWS, 1)
col = 2
'copy while rngCopy has any data
Do While Application.Counta(rngCopy) > 0
rngCopy.Copy Worksheets("Sheet2").Cells(2, col)
col = col + 1 'increment destination column
Set rngCopy = rngCopy.Offset(NUM_ROWS, 0) 'move copy range down 10
Loop
End Sub

Copy unique values in Excel VBA

I have written VBA code that copies a filtered table from one spreadsheet to another. This is the code:
Option Explicit
Public Sub LeadingRetailers()
Dim rngRows As Range
Set rngRows = Worksheets("StoreDatabase").Range("B5:N584")
With rngRows
.SpecialCells(xlCellTypeVisible).Copy _
Destination:=Worksheets("LeadingRetailersAUX").Range("B2")
End With
Sheets("Leading Retailers").Activate
End Sub
The filter is applied before the code is ran and then the code selects the visible cells and copies them so as to get only those rows that passed the filter.
In the filtered table to be copied I have, in column L of the range, a certain set of names, some of which are repeated in several rows.
I would like to add to the code so that it only copies one row per name in column L. In other words, I would like the code to copy only the first row for each of the names that appears in Column L of the filtered table.
Pehaps something like this can help you. Code will loop through your rows (5 to 584). First it checks if row is hidden. If not, will check if the value in column "L" is already in the Dictionary. If it is not, it will do two things: copy the row to Destination Sheet, and add the value to the Dictionary.
Option Explicit
Public Sub LeadingRetailers()
Dim d As Object
Dim i As Long
Dim k As Long
Set d = CreateObject("scripting.dictionary")
i = 2 'first row of pasting (in "LeadingRetailersAUX")
For k = 5 To 584
If Not (Worksheets("StoreDatabase").Rows(k).RowHeight = 0) Then 'if not hidden
If Not d.Exists(Worksheets("Hoja1").Cells(k, 12).Value) Then 'if not in Dictionary
d.Add Worksheets("StoreDatabase").Cells(k, 12).Value, i 'Add it
Worksheets("LeadingRetailersAUX").Cells(i, 2).EntireRow.Value = Worksheets("StoreDatabase").Cells(k, 1).EntireRow.Value
i = i + 1
End If
End If
Next
End Sub
You could apply another filter to the table to only show the first occurrence of each set of names and then run your macro as usual. See this answer:
https://superuser.com/a/634284

Refer to a cell on another sheet using the contents of another cell

I have an Excel file with many sheets.
Each sheet contains some raw data in two columns. The first column is the item (name in text) and the second is the value (number).
I like to draw a graph which shows how one item evolves. So I have created a new sheet and started making references to all raw data sheets to the item in interrest.
Doing this manually takes a long time for two reasons:
a) There is many raw-data sheets
b) The item is not on the same row in all raw-data sheets
So I'm wondering if it is possible to make a macro that would fetch all the values. If I gave the macro the item, e.g. apples, the macro would search all raw data sheets, find the row containing item apples and copy the value to a new row in my graph-sheet.
Is it doable? And how would I start writing such a macro?
Note: I'm not asking for a complete solution. I'm asking for names of functions that would be useful. Example: Which function can I use to iterate over all raw data sheets, which function can I use to find the item in each raw data sheet and so on.
Updates after comments
Yes, the item names are always in the same column, i.e. A. The value is always in column B (in the same row). But the row number may change from sheet to sheet.
It is only the value I want to copy (i.e. from column B in the row where column A contains the item name I'm interrested in.
Consider:
Sub dural()
Dim Master As Worksheet, sh As Worksheet
Dim GotIt As Range, r As Range, K As Long
Set Master = Sheets("Master")
arr = Array("raw1", "raw2", "raw3")
K = 1
For Each a In arr
Set sh = Sheets(a)
Set r = sh.Range("A:A").Cells
Set GotIt = r.Find(What:="apples", after:=r(1))
If GotIt Is Nothing Then
Else
GotIt.Resize(1, 2).Copy Master.Cells(K, 1)
K = K + 1
End If
Next a
End Sub
Where arr is the array of sheet names of the raw data sheets and Master is the name of the summary worksheet.

Validation among the sheets in Excel using macro

I want to create a macro which Compares data between the sheets.
I have data in the Sheet1 under Column A (Size of row / length is not constant) which needs to be compared with the data available in the Sheet2 under Column A. If that particular cell is matching then I would require the data next to the column A in Sheet1 (i.e. Column B in Sheet1) to be pasted in the column C (Sheet1). I'm trying to write a macro using the If and Loop but I fail and I forgot to save the macro I wrote.
When I use INDEX MATCH Excel becomes slow. Also I got up a new criterion now regards to my data. If column A in first sheet is matched with column A in second sheet then I want the column B to be validated with that off in second sheet and if both the conditions are satisfied then I want a value in column C.
I believe this complications can be sorted with macros loop or For each Next. I'm not sure about it.
Please sort it for me.
You might need to modify this slightly to work with your data but it should do the trick(based on my understanding of your problem).
Sub compare()
Dim i As Integer
i = 1
For Each cell1 In Sheet1.Range("A1:A100")
For Each cell2 In Sheet2.Range("A1:A100")
If cell1.Value = cell2.Value Then
Sheet3.Cells(i, 1).Value = cell1.Value
i = i + 1
End If
Next
Next
End Sub

Resources