How to copy an entire column based on the cell value in the first row? - excel

I have a code sheet for different digits that is being used for certain calculations. I need to select the entire column based on the first value in the top row. I'm able to select the entire column based on the range but is there a VBA lookup function that looks up for a value specified in it, and compares with the value in defined cell range, and allows us to copy the entire column? Here's what I have done so far:
Columns("F:F").Select
Selection.Copy
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Suppose I have a value called "string1", so in my case, "string1" is in the address "F1" which is why I have copied the "F" column & pasting it in front of all other columns. Similarly, if the value is changed to "string2" which is at the address "G1" then column "G" needs to be pasted in the front of the sheet.

Based on BigBen's suggestion here's what did my work. I hope it helps all the newbies in VBA Macros.
Dim c As Range
With Worksheets(1)
Set c = .Range("A1:XFD1").Find("string1", LookIn:=xlValues)
Range(c.Address).EntireColumn.Select
Selection.Copy
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
End With
Because I only want to search on the first row of the sheet so I used that range but it can be changed to any value depending on the requirement. The Find function returns the range (variable c) which can be used for desired selection & then it can be used in a way we want.
Edit: (To avoid selecting cells)
Dim c As Range
Set c = Worksheets(1).Range("A1:XFD1").Find("string1", LookIn:=xlValues)
c.EntireColumn.Copy
Columns("A:A").Insert Shift:=xlToRight

Related

Copy a specific range every nth number of rows in Excel VBA

I'm very new to VBA coding and cannot figure out this issue. I tried another solution but found it wouldn't work later down the road. I'm currently testing out this one.
On worksheet "BS growth" I need to read Column A to make sure it says "Asset" If it does I then need to copy and paste the entire rows, excluding A,B & C starting from D5 until D30. The loop then needs to skip 15 cells down and copy the next range of 25 rows (D45:D70, D85:D110, etc) until it no longer says Asset in column A. The first 402 rows in Column A say "Asset," and then the next 300 say "Liability" and the final 200 says "Equity."
I'll then need to paste the data into worksheet "Chart Ref" starting at row A2.
Here is a sample of the data. I am not sure if it is working/embedded properly.
enter image description here
I do not know how to make a loop repeat as needed.
Sub ChartReference4()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("BS growth")
For Each cell In ActiveWorkbook.Worksheets("BS growth").Range("A:A")
If cell.Value = "Asset" Then
Range("D5:D30").Select
'I need this to not be hard coded and only copy from D5 down 25 and then skip 15 cells and repeat the loop
Range(Selection, Selection.End(xlToRight)).Select 'Need all information to right
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets
End If
Next cell
End Sub
Currently this code just copies everything in the sheet from column D to the right.
Thanks so much, please comment if this doesnt make sense or I need more information.

Return cells content from range

Yesterday I learned here how to copy a row to a second sheet.
Sub maJolieProcedure(Texte As String)
With Worksheets("employes").Range("A:A")
Set c = .Find(what:=Texte)
If Not c Is Nothing Then
firstAddress = c.Row
Worksheets("employes").Rows(firstAddress).Copy _
Destination:=Worksheets("rapport").Range("A1")
MsgBox "Ok"
Else
MsgBox "Nok"
End If
End With
End Sub
To respect the formatting of the second sheet, I want to copy and paste the contents of each cell one by one.
I can identify the line number. However, I can't figure out how the Range object can return each cell one by one. For example, C3 content if Rows = 3.
Thanks a lot.
If you don't want to paste the formatting from one range to another paste values only.
Worksheets("employes").Rows(firstAddress).Copy
Worksheets("rapport").Range("A1").PasteSpecial xlValues
That's the same for all ranges, whether 1 cell or a million. The Copy process copies the subject to memory as one block. Any parsing must be done before the Copy instruction. An alternative is to read a range into an array.
Dim Arr As Variant
Arr = Worksheets("employes").Rows(firstAddress).Value
This will create a 3D array of 1 row and about 16000 columns. You might decide to limit your enthusiasm to only what you need.
With Worksheets("employees")
Arr = .Range(.Cells(firstAddress, 1), .Cells(firstAddress, .Columns.Count).End)xlToLeft)).Value
End With
Pay attention to the leading periods within the With statement. Each such period stands for the object mentioned in the With statement.
If your goal is to respect the formating of the second sheet, you don't need to loose time copying cell by cell.
It is more effective to do a paste special, like you do with the mouse:
Range("A1").Copy
Range("B1").PasteSpecial Paste:=xlPasteValues
works very well also with bigger ranges if you need:
Range("A1:A12").Copy
Range("B1:B12").PasteSpecial Paste:=xlPasteValues
or even
Range("A1:A12").Copy
Range("D3").PasteSpecial Paste:=xlPasteValues
If your goal is to really access all cell of a range individually , you just iterate on the range. For example:
For Each cell In Range("A1:A12")
cell.Value = cell.Value + 2
Next cell

Find cell & select/copy from active cell to last row including blanks

I'm attempting to find a column using the column header name, then select all the data from the column (including the blank cells) & paste into another range.
Currently I can only copy until the 1st blank cell. I have seen similar problems on the board but the solutions I have seen are coming from the angle of knowing which column it is in first & then finding the last row from the bottom of the worksheet.
Workbooks("PS & Config - Actuals & FC.xlsm").Worksheets(2).Range("A3").CurrentRegion.Find(What:="FFA Name").Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Range("A3").Select
ActiveSheet.Paste
To find the column use Match wit the column header name. Once you have that column, set the values in the destination range to be the same as that column. Selecting, copying, and pasting in VBA are unnecessary as the values themselves can be moved--plus it adds load.
I don't have Office anymore so I'm running this from memory. I hope it helps.
dim wb as workbook, ws1, ws2 as worksheet, myCol, myRow as long
set wb=excel.thisworkbook 'assuming this code goes in that workbook
set ws1=wb.sheets(2) 'set variables
myCol=worksheetfunction.match("FFA Name",ws1.[a1:zz1],0) 'search through 1st row
myRow=ws1.[a3].currentregion.rows.count 'grab last row containing data in this set
'do not select, nor copy and paste, if it can all be done with VBA. This can.
set ws2=wb.sheets.add 'a little rusty on this line, you can get the method from the macro recorder
ws2.cells(1,1).resize(myrow).value=ws1.cells(1,myCol).resize(myrow).value
The reason your original code stops when there's a blank cell is because the .end() method simulates pushing the end key and then an arrow, which goes to the first/last contiguous cell with data.
Range(ActiveCell, ActiveCell.End(xlDown)).Select

Copying values in a column to another column in another worksheet

I have created a button that copies all of the cells in one worksheet to a new worksheet. However I want the Total Quantity (H) column of the first worksheet to go into the Previous Quantity(G) column of the new worksheet. With that I want the Current Quantity (F) column to clear the values. With each new sheet the length of the column could grow so I can't set a specific range.
ActiveSheet.Range("F").ClearContents ??
Anything I've done has had the copy paste commands and I would prefer a direct method.
Thanks so much!!!
To select an entire column in column F, use
ActiveSheet.Range("F1").EntireColumn.ClearContents
You can also extend this to EntireRow, etc.
Edit - now that I'm clear on what you're asking.
If you already have a button that copies the entire worksheet to another one, then I assume that it copies all the original data as well. I don't know exactly what's on your worksheets, but the following code should show you how to copy & paste entire columns.
Assume there are two columns, G and H. The first row of G is named "Previous" and the first row of H is named "Current". The following code replaces column G with column H while preserving the column names in the first row:
Sub Test()
'Clear Column G:
Range(Cells(2, 7), Cells(Rows.Count, 7)).ClearContents
'Copy & Paste H to G:
Range(Cells(2, 8), Cells(Rows.Count, 8)).Copy
Range("G2").Select
ActiveSheet.Paste
'Clear Column H:
Range(Cells(2, 8), Cells(Rows.Count, 8)).ClearContents
End Sub
This is Copy and Paste. If you have no formulas that need preserving, you can get around all that code by simply Cut and pasting:
Sub Test()
Range(Cells(2, 8), Cells(Rows.Count, 8)).Cut
Range("G2").Select
ActiveSheet.Paste
End Sub
Would something like this work?
Columns("F:F").Select
Selection.ClearContents

VBA User Form - Need it to copy formula from column F, 1 row above

I have a form I made for users to enter data. The data get's inserted into the next available row. That works well, but now I need the formula from the cell above in Column F to be copied into the new cell. Is there a code I could use this to work along with the copying of the input values to the sheet?
Any help with this would be greatly appreciated.
Edit: Method 2 is probably better suited for your purpose as it will adapt the cell references in the previous line to the next line.
Method 1: To set the formula of a cell, use
Sheets("sheet name").cells(row, column).Formula = ....
Row and column must be specified as integer values, I.e. For column F it would be 6. Assuming you have the number of the next free line in a variable called NextFreeLine:
Sheets("sheet name").cells(NextFreeLine, 6).Formula = Sheets("sheet name").cells(NextFreeLine-1, 6).Formula
Method 2: If you insist on literally copying the formula from somewhere else, try this (this will adapt the cell references of the formula for the previous line to the next one-- just like copy/pasting it using Ctrl-C, Ctrl-V):
Sheets("sheet name").cells(NextFreeLine-1, 6).Copy
Sheets("sheet name").cells(NextFreeLine, 6).PasteSpecial Paste:=xlPasteFormulas
The method you are using to put into 'the next available row' would have been nice to see but perhaps you can translate this for your own purposes.
'put the newval into the next available cell in column A
cells(rows.count, 1).end(xlup).offset(1, 0) = newval
'copy the formula in column F to the new row
cells(rows.count, 1).end(xlup).offset(-1, 5).resize(2, 1).filldown
Your value goes into the next available row based on column A. The same column is used in much the same manner but offset up a row and resized to two rows in height before executing the fill down command.

Resources