I'm trying to write a macro to autofill a range dynamically as you can see in the photo :
I've tried this code and it return an error :
Sub Auto()
Dim selection1 As Range
Dim selection2 As Range
h = 1
g = 1
This two loops I used to detect the the first empty cell in column AZ to refer to its range later
Do Until Cells(h + 1, 52).Value = ""
h = h + 1
Loop
Do Until Cells(g + 1, 1).Value = ""
g = g + 1
Loop
Set selection1 = Range("AZ" & h & ":" & "BD" & h)
Set selection2 = ActiveSheet.Range("AZ" & g & ":" & "BD" & g)
This where I tested the above idea and it worked fine and the range selected as it should as shown in the photo
Range("AZ" & h & ":" & "BD" & h).Select
'Autofill
This where I got the error:
'Selection.AutoFill Destination:=Range("AZ" & g & ":" & "BD" & g), Type:=xlFillDefault
End Sub
Any Ideas?
This will autofill the data down.
The method for finding the last row is the same as going to the last row and pressing Ctrl+Up.
Sub Test()
Dim LastRow1 As Long, LastRow2 As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRow1 = .Cells(.Rows.Count, "AY").End(xlUp).Row 'Find last row in column AY.
LastRow2 = .Cells(.Rows.Count, "AZ").End(xlUp).Row 'Find last row in column AZ.
.Range(.Cells(LastRow2, "AZ"), .Cells(LastRow2, "BD")).AutoFill _
Destination:=.Range(.Cells(LastRow2, "AZ"), .Cells(LastRow1, "BD"))
End With
End Sub
Related
I'm trying to make multiple selections from Sheet2. The value is from the same column but different rows (thinking if using ActiveCell.Offset(1,0) will be feasible).
My code takes the value from an ActiveCell select and runs a macro compares it to another sheet (Sheet10) with some information to copy and paste in a target sheet (Sheet5).
The following is the code that I have right now.
a = Sheet10.Cells(Rows.Count, 1).End(xlUp).Row
c = Sheet2.Cells(Rows.Count, 5).End(xlUp).Row
For Each cell In Range(ActiveCell, ActiveCell.Offset(1, 0))
For i = 2 To a 'from Row 1 to the last row of "DMP"
Debug.Print ("i = " & i)
If cell.Value = Sheet10.Cells(i, 1).Value Then 'if selected cell matches (i,1) of "Sheet10 (DMP)"
Debug.Print ("ActiveCell =" & ActiveCell.Value)
For k = 1 To 20 'from Column 1 to Column 20
Debug.Print ("k = " & k)
For r = 1 To c 'from Row 1 to the last row of "Sheet 2(LightOn SKU)"
Debug.Print ("r = " & r)
If Sheet10.Cells(i, k).Value = Sheet2.Cells(r, 4).Value Then 'if value of (i,k) of "Sheet10 (DMP)" = (r,4) of "Sheet2 (LightOn SKU)"
Sheet2.Range("A" & r & ":G" & r).Copy
Sheet5.Activate
b = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
Sheet5.Cells(b + 1, 1).Select
ActiveSheet.Paste
Range("A" & r & ":L" & r).Borders.Color = vbBlack
End If
Next
Next
End If
Next
Next
Right now, it's running on an endless loop.
Still Unclear
Sub ACCopy2()
Dim a As Long
Dim c As Long
Dim r As Long
Dim i As Long
Dim k As Integer
Dim b As Long
a = Sheet10.Cells(Rows.Count, 1).End(xlUp).Row
c = Sheet2.Cells(Rows.Count, 5).End(xlUp).Row
For r = 1 To c ' from Row 1 to the last row of "Sheet 2(LightOn SKU)"
Debug.Print ("r = " & r)
For i = 2 To a ' from Row 1 to the last row of "DMP"
Debug.Print ("i = " & i)
' if selected cell matches (i,1) of "Sheet10 (DMP)"
If Sheet2.Cells(r, 1).Value = Sheet10.Cells(i, 1).Value Then
Debug.Print ("Sheet2 =" & Sheet2.Cells(r, 1).Value)
For k = 1 To 20 ' from Column 1 to Column 20
Debug.Print ("k = " & k)
' if value of (i,k) of "Sheet10 (DMP)" = (r,4) of
' "Sheet2 (LightOn SKU)"
If Sheet10.Cells(i, k).Value = Sheet2.Cells(r, 4).Value Then
With Sheet5
b = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
Sheet2.Range("A" & r & ":G" & r).Copy .Cells(b + 1, 1)
.Range("A" & r & ":L" & r).Borders.Color = vbBlack
End With
End If
Next
End If
Next
Next
End Sub
So in column A and column B there are two sets of values from workbook 1 that should match with the position in workbook 2.
**WORKBOOK 1**
column A column B column C
a 1 32
b 2 45
f 6 12
g 9 55
e 5 99
On this diagram, X marks location where the cells from workbook 1 match the position in workbook 2. The columns represent column A and the rows represent column B. Once the location has been found the corresponding value in column C will be pasted into the location.
**WORKBOOK 2**
1 2 5 9 6
_ _ _ _ _
a |X
b | X
f | X
g | X
e | X
FYI - the rows and columns in this example are from workbook 1, not the default column and row numbers.
My attempt, but what would you use instead of putting "B2" for the location?
Sub Location()
Dim i as Long, k as Long, ws1 as Worksheet, ws2 as Worksheet
Set ws1 = Workbooks("A").Worksheets("Sheet 1")
Set ws2 = Workbooks("B").Worksheets("Sheet 2")
For i = 1 to 5
variable = ws1.Cells(i, 1) && ws1.Cells(i, 2)
For k = 1 to 5
If ws2.Cells(i, 1) && ws2.Cell(1, i) = variable Then
ws1.Range("C1").Copy
ws2.Range("B2").Paste
End if
Next k
Next I
End Sub
Desired output
1 2 5 9 6
_ _ _ _ _
a |32
b | 45
f | 12
g | 55
e | 99
Any suggestions on how to begin this? Also I know the vlookup function exists but using VBA code would this be achievable?
Errors:
And this is the input, column B in this image is acting as column C in the example.
Input code:
Sub Location()
Dim i As Long, k As Long, ws1 As Worksheet, ws2 As Worksheet
Dim lastrow As Long
Set ws1 = Workbooks("Copy of Retrofit Monthly Invoicing 2017.xlsm").Worksheets("Sheet1")
Set ws2 = Workbooks("Book4").Worksheets("Sheet1")
lastrow = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
For i = 2 To ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
For k = 2 To ws2.Cells(1, ws2.Columns.Count).End(xlToLeft).Column
ws2.Cells(i, k).Value = ws2.Evaluate("IFERROR(INDEX(" & ws1.Range("B1:B" & lastrow).Address(0, 0, xlA1, 1) & ",AGGREGATE(15,6,ROW(" & ws1.Range("A1:A" & lastrow).Address(0, 0, xlA1, 1) & ")" & _
"/((" & ws1.Range("A1:A" & lastrow).Address(0, 0, xlA1, 1) & " = " & ws2.Cells(i, 1).Address(0, 0) & ")*(" & ws1.Range("C1:C" & lastrow).Address(0, 0, xlA1, 1) & "=" & _
ws2.Cells(1, k).Address(0, 0) & ")),1)),"""")")
Next k
Next i
End Sub
If you are not adverse to formula:
=IFERROR(INDEX(Sheet1!$C:$C,AGGREGATE(15,6,ROW(Sheet1!$A$1:$A$5)/((Sheet1!$A$1:$A$5 = $A2)*(Sheet1!$B$1:$B$5=B$1)),1)),"")
In the first cell, then copy and drag over and down.
Then just use Evaluate and the formula:
Sub Location()
Dim i As Long, k As Long, ws1 As Worksheet, ws2 As Worksheet
Dim lastrow As Long
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
lastrow = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
For i = 2 To ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
For k = 2 To ws2.Cells(1, ws2.Columns.Count).End(xlToLeft).Column
ws2.Cells(i, k).Value = ws2.Evaluate("IFERROR(INDEX(" & ws1.Range("C1:C" & lastrow).Address(0, 0, xlA1, 1) & ",AGGREGATE(15,6,ROW(" & ws1.Range("A1:A" & lastrow).Address(0, 0, xlA1, 1) & ")" & _
"/((" & ws1.Range("A1:A" & lastrow).Address(0, 0, xlA1, 1) & " = " & ws2.Cells(i, 1).Address(0, 0) & ")*(" & ws1.Range("B1:B" & lastrow).Address(0, 0, xlA1, 1) & "=" & _
ws2.Cells(1, k).Address(0, 0) & ")),1)),"""")")
Next k
Next i
End Sub
Do you need VBA? You can do this with an Array formula.
I'm using Sheets, but just have both workbooks open and use that, to fix the references:
Data is like this on Sheet1:
Then, in your Sheet2 (or other workbook, etc), place this in A1, and enter with CTRL+SHIFT+ENTER
=IFERROR(INDEX(Sheet1!$C$1:$C$5,MATCH(ROW()&SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1",""),Sheet1!$B$1:$B$5&Sheet1!$A$1:$A$5,0)),"")
Drag over and down:
I keep getting this error on the line with Application.WorksheetFunction and from reading on the topic for hours I feel that I've gotten just about nowhere.
Does it have something to do with the way I'm referencing Sheet2? Or am I not understanding fully what Application.WorksheetFunction is supposed to do?
Sub SearchForValues()
i = 4 'starts the iterator at column D
Do While Cells(1, i) <> ""
Dim l As Long, searchRange As String
n = 2
Do While Range("A" & n) <> "" 'loop until the last row of data in the first column
StartRow = Range("B" & n)
EndRow = Range("C" & n)
searchRange = "A" & StartRow & ":Q" & EndRow
l = Application.WorksheetFunction.Match(Cells(1, i), Worksheets("Sheet2").Range(searchRange), 0)
Range("D" & n) = l
n = n + 1
Loop
i = i + 1
Loop
End Sub
Here's a screenshot of the data I have. Columns B and C are the ranges of rows that I want to search in on Sheet2 for each row on sheet 1 and each cell across the top is a term I want to search for in that range.
Scott Craner already answered your question in his comments with "
Match only works on 1 dimensional arrays; either one row or one column" and "Use the VBA Find()".
Here is an example of how you can use Range.Find
Sub SearchForValues()
Application.ScreenUpdating = False
Dim Target As Range
Dim x As Long, y As Long
With Worksheets("Sheet1")
For x = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
For y = 4 To .Cells(1, .Columns.Count).End(xlToLeft).Column
Set Target = Worksheets("Sheet2").Range("A" & .Cells(x, "B").Value & ":Q" & .Cells(x, "C").Value)
.Cells(x, y).Value = Not Target.Find(.Cells(1, y).Value) Is Nothing
Next
Next
End With
Application.ScreenUpdating = True
End Sub
I have a user input transaction information (Name, Amount, Date) in cells E33:G33.
User clicks button and the transaction is recorded in the last row +1 used in a Transaction history list starting on E46:G46.
So far my code doesnt work.
i want simply:
-copy range (E33:G33)
-find last row used in column E
-go one row lower
-past the copied range
Cant find a working answer, please help! Thanks.
use this:
Sub test()
Dim e&: e = Cells(Rows.Count, "E").End(xlUp).Row + 1
Range("E" & e & ":G" & e).Value = [E33:G33].Value 'past only values
End Sub
or
Sub test2()
Dim e&: e = Cells(Rows.Count, "E").End(xlUp).Row + 1
[E33:G33].Copy Range("E" & e & ":G" & e) 'past with cells format
End Sub
or
Sub test3()
Dim e&: e = Cells(Rows.Count, "E").End(xlUp).Row + 1
[E33:G33].Copy
Range("E" & e & ":G" & e).PasteSpecial xlPasteAll 'past with cells format
End Sub
or
Sub test4()
Dim e&: e = Cells(Rows.Count, "E").End(xlUp).Row + 1
[E33:G33].Copy
Range("E" & e & ":G" & e).PasteSpecial xlPasteValues 'past only values
End Sub
also:
[E33:G33] can be replaced by:
Range("E33:G33")
or
Cells(Cells(33,"E"),Cells(33,"G"))
or
Cells(Cells(33,5),Cells(33,7))
I have multiple Excel workbooks that contain about 8,000 rows so it would be nice to use a macro.
Basically, if any row has a zero (0) in all columns (at the same time) B, D, E, I, J, and K it will delete.
Here is what I have so far...way too new with VB to figure out.
Sub DeleteRowsZeros()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
If (Range("B") = "0" And Range("D" & i) = "0" And Range("E" & i) = "0" And Range("I" & i) = "0" _
And Range("J" & i) = "0" And Range("K" & i) = "0") Then Rows(i).Delete
Next i
End Sub
Try this :
Sub DeleteRowsZeros()
Dim cell As Range, notZeroColumns As Range, row As Range
Set row = Range("A" & Rows.Count).End(xlUp).EntireRow.Offset(1, 0)
Set notZeroColumns = Range("B:B,D:E,I:k")
While row.row <> 1
Set row = row.Offset(-1, 0)
For Each cell In Intersect(row, notZeroColumns)
If cell.Text <> "0" Then GoTo continueLbl
Next
row.Offset(1, 0).Delete
continueLbl:
Wend
End Sub
EDIT : bugfixe