im new to this and i want to ask one question if anyone can help.
im working with excel 2003 and what i want is that i have one column which at cell A1 i have name and in cell A2 i have surname, in cell A3 i have name and in cell A4 i have surname and so on.
what i want to do is to merge cell A1 with A2 and then A3 with A4 and so on.
its about 3000 rows.
is there any vba code to do it automatically?
You don't really need a VBA program for this, just use Excel formulas as so:
In the column next to your names fill a sequential list of numbers starting with 1. So this should have 1, 2, 3, 4 and so forth. You can go to the bottom of your A column, but half-way will be fine. TO do this quickly, type a 1 in the first row (B1), then in the second (B2) add a formula: =B1+1. Then copy this formula all the way down B column to the extent of the A column. The trick here is to go to B2, select Edit/Copy, then move to A column and Ctrl-Down, move to the B column and then use Shift+Ctrl-Up, then paste.
In C column you will put a formula to merge two cells together from A column, as so:
=INDEX($A$1:$A$<ENDROW>,(B1-1)*2+1,1)&" "&INDEX($A$1:$A$<ENDROW>,(B1-1)*2+2,1)
Where <ENDROW> is the last row in A.
Copy this formula down at least half the rows of the A column. If you go beyond this, you will get #REF! error; just clear those cells.
Now you want to copy the formulas in C to another column, say D. Select them, use Edit/Copy, then move to D1 and right-click and choose "Paste special". In the dialog click on "Values" and then "OK". Now you have a column with the values of every two rows concatenated together.
to do this in vba:
Sub nameMerger()
Range("A1").Select
While ActiveCell.Value <> vbNullString
ActiveCell.Value = ActiveCell.Value & " " & ActiveCell.Offset(0, 1).Value
ActiveCell.Offset(0, 1).EntireColumn.Delete
ActiveCell.Offset(0, 1).Select
Wend
End Sub
Tested and works
Related
What is the VBA code in Excel for selecting the cell where your macro has initially started. For instance, I will run the macro n times in one workbook and each time before starting it I will choose different cell - A2, A3, A4 etc. What I need is that the very last line of the macro must be to select the cell where the macro has initially started - A2, A3, A4 etc.
My code now looks like this:
Selection.Copy
Range("I2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
So, the first line is copy - I started the macro from cell A2 and copied the content and pasted in cell I2. Then I want to select again cell A2 because I will copy all cells 3 columns rightward from cell A2 (B2, C2 and D2 must then be copied). On the next run, I will start the macro from cell A3 and then I want to copy all cells 3 columns rightward from cell A3 and so on.
I hope I explained my issue as clear as possible. Could you please help me with that problem?
You don not need to select anything, but if you insist you can firstly set the cell to be used as the active cell:
Sub copyFromReferenceCell()
Dim cellCopy As Range
Set cellCopy = ActiveCell 'if you want using a previously selected cell
cellCopy.Copy Destination:=cellCopy.Offset(0, 8)
Range(cellCopy.Offset(0, 1), cellCopy.Offset(0, 3)).Copy Destination:=cellCopy.Offset(0, 9)
End Sub
You did not tell us where do you need copying the range of three cells, I only supposed that you try copying in a similar way and choose "J2". If my supposition is wrong, you can set a different range
I have a spreadsheet where I filter from row 1 column W and then apply a formula to the next visible cell. The next visible cell varies each week. It could be 1160 one week then 1165 the next, etc.
How can I write macro code that will pick up on the next visible cell no matter what row it is. It could be something simple that says "start on E1 and drop down to the next visible cell in column E" because then I don't mind using active cell formula entry.
Does anyone have any ideas? Thank you.
Here's what I have:
With Sheets("Data").Range.SpecialCells(xlCellTypeVisible)("W1")
.Value = "My Formula"
End With
If I understand the problem correctly:
With Sheets("Data").Range("W:W").SpecialCells(xlCellTypeVisible).Cells(2)
.Formula = "My Formula"
End With
I have compiled wheel data but want a VBA macro that copies any cell from sheet 1 (named: SheetSJ) that matches partial text, and then copies that cell's data into sheet 2. This is to make the data much easier to work with.
Search each row for any cells in SheetJS that contain text "Product ID", if no matches then ignore
If any cell (text) matches, copy that cell, and paste the contents to sheet 2 column B (beginning with row 2)
Search each row for any cells in SheetJS that contain text "Bolt Pattern", if no matches then ignore
If any cell (text) matches, copy that cell, and paste the contents to sheet 2 column D (beginning with row 2)
Wheel Data
As evident in the picture, the data is all over the place in each column and thus the macro cannot use any particular cell for reference. It can only match text values (which are unique).
Sub Test()
For Each Cell In Sheets(1).Range("A1:ZZ200")
If Cell.Value = "Product ID" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
End If
Next
End Sub
I managed to find some examples online but they copy the entire row not the individual cell.
How about this code?
I cannot use English well, but if you want, I will help you with my best.
Sub test()
For Each cell In Sheets(1).Range("A1:ZZ200")
matchrow = cell.Row
If cell.Value Like "*Product ID*" Then 'You have to use "Like" Operator if you want to use wildcard something like *,?,#...
Sheets(2).Range("B" & matchrow).Value = cell.Value
'I recommend you to use ".value" Property when deal only text, not else(like cell color, border... etc), rather than "select-copy-paste". It could be slower while hoping both sheets
ElseIf cell.Value Like "*Bolt Pattern*" Then
Sheets(2).Range("D" & matchrow).Value = cell.Value
End If
Next
End Sub
I don't think you need a macro at all. In sheet2 column B, row 2, place the following formula:
=iferror(index(SheetJS!2:2,match("*Product ID*",SheetJS!2:2,0)),"")
The iferror part just keeps the cell empty if no match is found (as opposed to giving an ugly error message). Match tells how far into row 2 the product id occurs, and index goes that far in and gets the value.
Now grab the handle at the bottom right corner of the cell, and drag it down as many rows as you have rows in the first sheet. That should bring all product IDs from Sheet JS into column B.
Similarly start in row 2 column D with
=iferror(index(SheetJS!2:2,match("*Bolt Pattern*",SheetJS!2:2,0)),"")
and drag that on down.
I'm assuming no row has more than one product id or bolt pattern, which appears to be true.
This approach does have a mild drawback, that it will leave a blank space in the sheet 2 column if the SheetJS does not have that entry in that row.
I have two excel sheets both with matching product numbers. What I need to do is match the product numbers and then copy a column from the first sheet to the second.
My example:
Column C in the first sheet contains product numbers
Column A in the second sheet contains product numbers
I want to match C & A and then copy column B from the first to the second sheet.
Sorry if this has been answered before, my knowledge is basic but I am trying to learn
Thanks for any help.
CD
Assuming your numbers to match start in C2 (headers in row 1) and numbers to copy in AC2 on Sheet1, this should do what you want. Paste it into AC2 on Sheet2 and then drag copy to the length of column A on Sheet2.
=IF(IFNA(MATCH(A2,Sheet1!C:C,0), FALSE), INDIRECT("Sheet1!AC"&MATCH(A2, Sheet1!C:C, 0)), "Not Found")
Note this will give "Not Found" for a value on Sheet2 which is not found on Sheet1, you can change that by just replacing the string "Not Found" in the formula with whatever you want (e.g. 0).
Formula:
You can use OFFSET with IF. In B1 of sheet 2 for example you could put, drag down for as many rows as required,
=IF(A1=Sheet1!C1,OFFSET(Sheet1!A1,0,COLUMNS(Sheet1!A:AC)-1,1,1))
If you set your data up as a table in sheet 2 (Ctrl + T with an populated cell selected) then add this formula to the appropriate column it will autofilter down the formula.
Or:
With code in a standard module:
Public Sub AddFormula()
Dim rng As Range
With Worksheets("Sheet2")
If Application.WorksheetFunction.Subtotal(103, .Columns(1).Cells) > 0 Then
For Each rng In Intersect(.Columns(1), .UsedRange)
If Not IsEmpty(rng) And Not IsError(rng) Then 'assume ignore empty cells
If rng = Worksheets("Sheet1").Cells(rng.Row, "C") Then Worksheets("Sheet1").Cells(rng.Row, "AC").Copy rng.Offset(, 1) 'determine where you want value to go
End If
Next rng
End If
End With
End Sub
Note:
This is based on your comment that you are comparing sheet2 col A with sheet1 col C and copying sheet1 col AC if match.
I assume that the product numbers in both sheets are unique. If that, I suggest use the offset and match formulas to achieve what you want.
sorry for my half-baked answer due to i just typed them with phone and it is not convenient to input complicated style, here is the additional information:
the match formula is used to query the location of the target product number in the original sheet( sheet 1 in your case above);
after we located the position of the target product number, extract the information you wanted through the offset formula.
here are the specific process:
Sheet 1 represent the original information and sheet 2 represent the target one. What we required to do is copy the info in col D of sheet 1 to col D of sheet through the product:
OFFSET($D$5,MATCH(I6,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I7,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I8,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I9,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I10,$D$5:$D$16,0)-1,1,1,1)
or you can refer to the output directly:
enter image description here
for more information about these two formulas, you can refer to the help of excel of google.
I have a submission form, Sheet1, and a datasheet that does calculations, Sheet2. Sheet2 contains a list of Names and Dates and empty cells ready to be filled.
On Sheet1, the user selects a Name from a list in A2 (using data validation, with the source being a 3rd reference sheet), then enters values in C1, D1, E1, F1, etc. The user then clicks a button that prints the form sheet, Sheet1.
I also want to also transfer the data from C1, D1, E1 etc into Sheet2, and slot them into the correct row, which is which ever row has the corresponding Name entered in Sheet1!B1 in Sheet2!A1.
I'm comfortable with VBA copying defined cells to other defined cells, but am unsure of how to combine that with finding the correct position in Sheet2 based on the value in A. Below are Sheet1 and Sheet2.
Please modify to suit your requirement
Sub copyrange()
Dim sheet2_row As Integer
sheet1_name = Sheets("Sheet1").Cells(2, 2).Value
sheet2_row=WorksheetFunction.Match(sheet1_name,Sheets("Sheet2").Range("A1:A10"), 0)
Sheets("Sheet1").Range("C2:AG2").Copy
Sheets("Sheet2").Range("B" & sheet2_row & " : AF" & sheet2_row).PasteSpecial xlPasteValues
End Sub