Autofill Copy down up until next empty row- Excel VBA Macro - excel

i would like to paste a value in a given cell in Column A (empty column), i would like to autofill copy this down up until the next empty row or data in column B.
I can do this by specifying a range to copy down to but i would like for this to be dynamic since the amount of rows copied can be different. There are blocks of data, with a blank row in between. I would like to essentially copy and paste a value for each block of data in column A. Thank you!
Selection.AutoFill Destination:=Range(Selection, Selection.End(xlDown)), Type:=xlFillCopy

Since Column A is empty end(xldown) will return row 1048576. You need to look at Column B for the end then refer back to Column A.
You can use offset to grab Column B from your Selection then back to Column A.
Selection.AutoFill Destination:=Range(Selection, Selection.Offset(0, 1).End(xlDown).Offset(0, -1)), Type:=xlFillCopy

Related

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

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

Copy a cell from one sheet to another sheet if that cell contains a particular text

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.

Inserting Contents of Range of Cells Into Another Range

I am working on setting up a column of cells in Excel and I would like each cell in the column to pull data from multiple columns of cells from another sheet, with each cell having a one-on-one relationship with each other. For instance, I have Column A on Sheet 1 and it is automatically populated from data in Column A and Column B on Sheet 2 (once Column A runs out of data). If any of the data is changed on Sheet 2, the changes will be updated on Sheet 1. If an item is added, it will automatically be inserted on Sheet 1. Is this possible using standard formulas or array formulas, or do I need to use macros or VBA? Any suggestions would be appreciated. Thank you much in advance.
Either
Copy your formula such as IF(ISBLANK(Sheet2!A2),"",Sheet2!A2+Sheet2!B2 down a greater number of rows than you're likely to need.
or
Write a macro to run each that will copy your formula for you.
lr = Sheets("Sheet2").UsedRange.SpecialCells(xlCellTypeLastCell).Row
Sheets("Sheet1").Select
Range("A2").Select
Selection.Copy
Range("A3:A" & lr).Select
ActiveSheet.Paste

how to select rows adjacent to blank cells in excel?

In the excel sheet, I have two columns A and B. For every value in rows in column A there are values in column B. But some of the rows in column B are blank because of unavailability of value for the value in column A.
Now I want to pick only the row values in column A for which corresponding row in column B are blank. How do i do that? Thanks a lot.
Option Explicit
Public Sub filterBlanks()
With ActiveSheet.UsedRange
.AutoFilter Field:=2, Criteria1:="="
.Columns(1).SpecialCells(xlCellTypeVisible).Select
End With
End Sub
Use the Range.SpecialCells method to find hte blanks in column B and then Range.Offset property to select their corresponding cells from column A.
with activesheet
with .columns("B").specialcells(xlcelltypeblanks)
with .offset(0, -1)
.select 'do something with the cells in column A
end with
end with
end with
The above can have problems if you have cells merged across columns A and B (e.g. A1:B1 are merged). It also depends upon the cells being truly blank; not cells with formulas returning zero-length strings (which are not blank cells).

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