How do I copy a column's data with reference to another? - vba

I am trying to write a code to copy the data from 1 column with reference to another column, however it is not including blanks.
For Eg - Column A has 10 values and Column B has 9 values and the last 1 is blank. I need to copy all the data, including the blank and paste it into another sheet. Can anyone please help? Please see the image for example. Column B is dynamic, so Offset doesn't work.
enter image description here

Have you tried the following code?
Columns ("A:A").select
Selection.Copy
Columns("B:B)"
Activesheet.Paste
If you want to paste it in another sheet you should refer to the desired sheet instead of ActiveSheet

Related

look for Column heading and sum up

I have been working with a excel file with a lot of data, which is arranged in 2 sheets.
I would like to get data from sheet 1 to sheet 2 with reference to the column headings.
For example:
So if I want to find the sum of function 1 person A with criteria 1, the command have to go and find the heading "sum of function 1" in sheet 1 and choose the data that are only under criteria 1 and sum it up in sheet 2 cell D5. (By using column heading reference instead of cell reference).
The table range is A2 : U80.
Thanks.
First you have to format your data as table (select the data -> Menu Insert -> Table). Then you rename your table, for example Table1. Let's say one of the columns you want to sum on the sheet2 is called ColumnName.
On the sheet 2 you write a formula
=SUM(Table1[ColumnName])
The result will be what you are after.
You should try it by SUMIFS(). Syntax will be
=SUMIFS(AgeRange from sheet1,NameRange Sheet1, Name cell Sheet2, PlaceRangeSh1, Place Cell Sh2)
Tell me if requires further help.

VBA Drag down Vlookup

All,
I have a data sheet of around 1.000 values which need a matching amount (the amount of valueschanges every day) . These amounts can be found in another tab "Data".
so using a Vlookup code in VBA should help me. The code I'm using is:
Sheets("Data").Range("E2") = Application.WorksheetFunction.VLookup(Sheets("Data").Range("D2"), Sheets("Blocked").Range("C:D"), 2, False)
Result should appear in column E responding with the row of the lookup_value which can be found in column D.The table and column index don't change being Sheets("Blocked").Range("C:D")and 2
This code gives me the result I wanted but as I tried to drag down the formula with this function:
Range("E2").AutoFill Destination:=Range("E2:E440")
How can I drag down this formula without needing to create a seperate vlookup for each row?
You should use the Formula as an R1C1 reference and then u will be able to drag down
Sheets("Data").Range("E2").FormulaR1C1 = "=VLookup('Data'!R[-1]C,'Blocked'!C[-2]:C[-1],2,0)"

Copy rows with a specific value to another sheet

I have a huge Excel worksheet which contains 200,000 rows. The data is about 120 subjects which are listed in column B, like sinani-01 to sinani-120. I need to copy all rows where column B contains sinani-01, for example, and paste them to new sheet. Or at least just select them?
Can you please let me know how to do this in VBA?
Excel has an AutoFilter function that you can use to select only the rows that contain a specific value. You use it like this:
' Filter column B by a specific value...
Range("B:B").AutoFilter 1, "sinani-01"
' Copy all visible rows to a new sheet...
Range("B:B").SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("sinani-01").Range("A1")
' Turn the AutoFilter back off...
Range("B1").AutoFilter

SUMIFS formula to fill data in cell depends upon date and name and also keep previous data in a cell

I have an excel with two sheets named raw and data here
raw sheet :
date sheet :
In raw sheet I will be uploading data manually.
What I am looking for :
In this, I want to create a formula which will check each name from the data sheet in raw sheet and it has to pick the total value (D column in raw) and update the same in data sheet in a cell which matches with last modified date.
For example :
Name is R420864561 and its last modified date is 20141201 and total value is 10.
So in my data sheet, it has to be updated like this
Above, value 10 is updated in a cell which matches the last modified from raw sheet with date in data sheet, here both are 20141201.
I am using below formula for this (formula in C3)
=SUMIFS(raw!$D:$D,raw!$A:$A,$A3,raw!$C:$C,C$2)
Now, if last modified date 20141202 with total 50
Now, data sheet will updated like below using above formula
What I need now :
In above, if I update value in another date, the data updated in previous date is getting changing to 0. But I need to keep that data in that cell, if there is no update in that.
In above case, I need to keep previous data (for date 2014201)10 in data sheet cell C3, and update 50 in D3 (for date 20141202)
I have added below code with macro, to copy paste the formula value, as simple value
Dim rngCell As Range
For Each rngCell In ActiveWindow.RangeSelection
rngCell.Value = rngCell.Value
Next rngCell
But that is not working here, It still resetting previous value to 0 when I update for a different date.
Any suggestion to correct this is much appreciated
I have uploaded the copy of the excel which I am working in below location :
https://www.dropbox.com/s/jpubzqbco5mflu5/1.1_test.xlsm?dl=0
This should work assuming your loop is correct.
rngCell.Copy
rngCell.PasteSpecial Paste:=xlPasteValues
Try For Each rngCell In Application.Selection to start the loop
you VBA is wrong:
update it using this one:
Sub saveMyData()
Set ws = ActiveSheet
ws.Range("C3:E999").Copy
ws.Range("C3").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Range("C3").Formula = "=SUMIFS(raw!$D:$D,raw!$A:$A,$A3,raw!$C:$C,C$2)"
Range("D3").Formula = "=SUMIFS(raw!$D:$D,raw!$A:$A,$A3,raw!$C:$C,D$2)"
Range("E3").Formula = "=SUMIFS(raw!$D:$D,raw!$A:$A,$A3,raw!$C:$C,E$2)"
End Sub
Here is ***UPDATED***the example sheet to try and see if it works.
it was because your VBA code was mixed up with unncessary codes.
From all appearances, the problem is with how you are presenting the question and plan to use the raw spreadsheet. From the information provided, it seems like you plan on entering records in raw, but then you also intend to change them. Also, based on your example and how you are describing the problem, it's not clear why you are using SUMIFS rather than INDEX(MATCH).
It would seem that your raw spreadsheet should have multiple records, one for each Name/Last Modified combination, and more than one for each combination to necessitate the use of SUMIFS. Then your formula in data will pick up the relevant info in raw.
If you plan on only having one entry for each Name in raw, and changing the last modified date and the total in raw it seems rather pointless to have separate spreadsheets at all, just enter it into data in the first place under the respective last modified date.

Copy columns from one Excel sheet to another, skipping columns in between [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Copy column from one Excel sheet to another Excel sheet
I have 5 sheets in my Excel file. Out of these 5, one is the master sheet which has all the data I want to copy to the other 4 sheets.
All the sheets have one common column, ID. The master has more than 10000 IDs.
What I want to do is:
If ID in sheet1 = 24356 = ID in master sheet then copy x, y, z columns from master sheet to sheet1. This is same for all other sheets.
Also, since the master sheet is from another source than the rest of the sheets, its formatting is different. Is there a way to remove all formatting in sheets before running copy/paste?
Can anyone please tell me the VBA code to do this.
This is what my Master sheet looks like:
I want the other sheet (e.g. sheet1) in this case to look like:
Also, while searching the master sheet, is it possible for the code to look through the last column of the sheet?
Nupur
Here is the non-VBA way.
Assume your master data is on a sheet called Master and the data is in range A1:H9. Now assume I have Sheet1 with a list of IDs in range A2:A5.
1 ID Name Type Question1 Type3 Type4
2 475
3 479
4 501
5 503
Then in Sheet1 in cell B2 I have the following to retrieve Name for ID=475:
=VLOOKUP($A2,Master!$A$2:$H$9,2,0) //returns f1
To get Type for ID=475 I use:
=VLOOKUP($A2,Master!$A$2:$H$9,3,0) //returns adm1
All I am doing is setting up a reference to the data table on Master (note absolute ref with $ signs), using ID as the lookup value, and then changing the column value (3rd input in VLOOKUP) to retreive the relevant value.
Once you have set this up for the first row you can then just copy down and get the right data populated.
Does this solve the issue?