Excel VBA autofill based on dynamic range - excel

Please can you help with the below; I have an increasing list of staff listed in A1, and I want my formulas in E1, F1, G1, etc. to be autofilled and not static like I have now.
I have the below but its throwing up the error: Method 'Range' of object '_Global' failed
My vba code;
Sub AutoFill()
Dim RowCount As Variant
RowCount = Range(("A1"), Range("A1").End(xlDown)).Rows.Count
Range("E1").Select ' Default formula's in E1
Selection.AutoFill Destination:=Range(RowCount - 1), Type:=xlFillDefault
End Sub
What am I missing, i'm stumped and have been since yesterday...
Thanks in Advance.

The Range.FillDown method method may be easier to code.
Sub DynAutoFill()
Dim lastRow As long
lastRow = cells(rows.count, "A").end(xlup).row 'should -1 be added here?
Range(cells(1, "E"), cells(lastRow, "E")).filldown
End Sub
FillDown can easily handle multiple columns.
Sub DynAutoFill()
Dim lastRow As long
lastRow = cells(rows.count, "A").end(xlup).row 'should -1 be added here?
Range(cells(1, "E"), cells(lastRow, "G")).filldown
End Sub
Your own code needs the entire range as the destination.
Sub DynAutoFill()
Dim RowCount As Variant
RowCount = Range(("A1"), Range("A1").End(xlDown)).Rows.Count
Range("E1").AutoFill Destination:=Range(cells(1, "E", cells(RowCount - 1, "E")), Type:=xlFillDefault
End Sub
I wouldn't use a reserved word for the name of the sub procedure.

Related

Copy range of data from sheet 1 Inputs to a sheet 2 inspection log

I have created a spreadsheet with a sheet 1 input table, and want to transfer/copy that data into a sheet 2 log table. The input table on sheet 1 will have an inspection date and an inspection name cells. What I am having an issue with is that I can get the first line of the log to input, but the 2nd line I get a "Run0time error '1004': Application-defined or object defined error". Not sure what to look at from here.
Here's my code (I know, it's stiff rough and needs to be cleaned up):
Private Sub Add_Click()
Dim InspectionDate As String, InspectionName As String
Dim LastRow As Long
Worksheets("sheet1").Select
InspectionDate = Range("B4")
InspectionName = Range("B5")
Worksheets("sheet2").Select
Worksheets("sheet2").Range("B3").Select
If Worksheets("sheet2").Range("B3").Offset(1, 0) <> "" Then
Worksheets("sheet2").Range("B3").End(x1Down).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = InspectionDate
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = InspectionName
Worksheets("sheet1").Select
Worksheets("sheet1").Range("B4:B5").ClearContents
End Sub
Two main reasons why .Select, .Activate, Selection, Activecell, Activesheet, Activeworkbook, etc. should be avoided
The reasons are explained in the second answer on that page.
I have tested the code below and it works for me.
I'm autistic; so sometimes I appear to school others, when I'm only trying to help.
Option Explicit
Private Sub Add_Click()
Dim InspectionDate$, InspectionName$
Dim LastRow&
Dim WS As Worksheet, WS2 As Worksheet
Set WS = Worksheets("sheet1")
Set WS2 = Worksheets("sheet2")
InspectionDate = WS.Range("B4")
InspectionName = WS.Range("B5")
LastRow = 3
If WS2.Range("B" & LastRow + 1) <> "" Then
LastRow = WS2.Range("B" & Rows.count - 1).End(xlUp).Row
End If
WS2.Cells(LastRow + 1, 2) = InspectionDate
WS2.Cells(LastRow + 1, 3) = InspectionName
WS.Range("B4:B5").ClearContents
End Sub

Update an Empty Cell in a range

I'm looking to update a cell on a sheet when it's left empty. If there is data in column B but not in column AA, I need to insert something into column AA.
I have made the following code but have failed to make it update the cell:
Range("B2").Select
Do Until IsEmpty(ActiveCell)
Dim LoopRowNo As Integer
LoopRowNo = ActiveCell.Row
If IsEmpty(Range(Cells(LoopRowNo, 26))) Then Range(Cells(LoopRowNo, 26)).Value = "01/01/1990"
ActiveCell.Offset(1, 0).Select
Loop
Hoping someone can point me in the right direction.
Use Range or Cells, but not both.
Don't Select.
With ActiveSheet
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Dim i As Long
For i = 2 to lastRow
If IsEmpty(.Cells(i, "AA")) And Not IsEmpty(.Cells(i, "B")) Then
.Cells(i, "AA").Value = "01/01/1990"
End If
Next
End With

Move down one row all cells from one column (offset)

I would like to move down one row all the cells from a column (in my case column C). It means if I have “X” written in cell C1, it should move down to Cell C2, if I have “Y” written in cell C1000,it should move down to Cell C1001…
I have the following error message:
Run time error 1004, application defined or object defined error
Sub movedownrowcolumnC()
Range("C:C").Offset(1).Select
End Sub
try below macro.
Sub MoveDowncolumn()
Dim lastRow As Integer
With Worksheets("Sheet1")
lstrow = .Cells(Rows.Count, "C").End(xlUp).Row
For i = lstrow To 1 Step -1
.Cells(i + 1, "C").Value = .Cells(i, "C").Value
Next i
End With
End Sub
use:
Cells(Rows.Count, "C").End(xlUp).Offset(1).Select
although you most probably don't need to Select anything and just go with the Range variable:
Sub movedownrowcolumnC()
Dim myRange As Range
Set myRange = Cells(Rows.Count, "C").End(xlUp).Offset(1)
myRange.Value = "myValue"
End Sub
While much more than just "good coding practice" is to always explicitly qualify a Range object up to its Worksheet reference:
Sub movedownrowcolumnC()
Dim myRange As Range
With Worksheets("mySheetName") ' reference wanted worksheet (change "mySheetName" to your actual relevantsheet name)
Set myRange = .Cells(.Rows.Count, "C").End(xlUp).Offset(1) ' ser referenced worksheet column C cells right below last not empty one
End With
myRange.Value = "myValue"
End Sub
Did you ask for this?
Range("C1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
You can you:
Option Explicit
Sub Test()
With ThisWorkbook.Worksheets("Sheet1")
If .Range("C1").Value = "Test" Then
.Rows(.Range("C1").Row + 1).Select
End If
End With
End Sub

VBA code in excel operates inconsistently with very simple code

I wrote some pretty simple VBA (excel macros) code to manage my audio licencing excel experience. The code is supposed to look through the excel sheet in column 3, look for any that have "AMC" in their column, and then copy and paste the row to sheet 2 and continue searching through entire excel document. This code is very simple and worked once right before it stopped working right. It only takes the very last AMC value and puts that on sheet 2 but not the other 5 rows that have AMC in their column 3 value.
Please help! I would appreciate it very much :)
-Jeremy
VBA Code:
Sub CommandButton1_Click()
a = Worksheets("Sheet1").UsedRange.Rows.Count
b = 0
For i = 2 To a
If Worksheets("Sheet1").Cells(i, 3).Value = "AMC" Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet2").Activate
' b = ActiveSheet.UsedRange.Rows.Count
Worksheets("Sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select
End Sub
This should solve your problem :
If Worksheets("Sheet1").Cells(i, 3).Value = "AMC" Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet2").Activate
Worksheets("Sheet2").Cells(b + 1, 1).Select
b = b + 1
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
You could use Instr and Union.
Union is very efficient as you store all the qualifying ranges in memory and then write out only once to the sheet. Much less expensive operation than continually writing out to the sheet.
Instr allows you to use vbBinaryCompare which means you are doing a case sensitive match i.e. only AC not ac will be matched on.
The code belows avoids .Activate, which is again an expensive operation that isn't required.
UsedRange means you may be looping many more rows than required. You only want to loop to the last populated row in column C of sheet 1, as that is the column you are testing. Hence, I use .Cells(.Rows.Count, C").End(xlUp).Row to find that last row.
Use Option Explicit - research why! It will make your VBA life soooooo much better.
Code:
Option Explicit
Sub CommandButton1_Click()
Dim lastRow As Long, sSht As Worksheet, tSht As Worksheet, loopRange As Range
Set sSht = ThisWorkbook.Worksheets("Sheet1")
Set tSht = ThisWorkbook.Worksheets("Sheet2")
With sSht
Set loopRange = .Range("C2:C" & .Cells(.Rows.Count, C").End(xlUp).Row)
End With
Dim rng As Range, unionRng As Range
For Each rng In loopRange
If InStr(1, rng.Value, "AC", vbBinaryCompare) > 0 Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, rng)
Else
Set unionRng = rng
End If
End If
Next rng
If Not unionRng Is Nothing Then unionRng.EntireRow.Copy tSht.Cells(1, 1)
End Sub

Combobox not adding defined named range

I have two comboboxes set in sheet1. I need to add the list of sheets to combobox one, which works fine. I need to add the first column of sheet2 to combobox two with the first cell as the name (called "Name"). This code worked for my UserForm, with Me instead of Sheet1, but using either doesn't work for me.
I'm getting an "Object doesnt support this property or method" error.
Thanks,
Private Sub Workbook_Open()
Dim refSheet As Worksheet
Set refSheet = ActiveWorkbook.Sheets(2)
Dim oSheet As Excel.Worksheet
For Each oSheet In ActiveWorkbook.Sheets
Sheet1.ComboBox1.AddItem oSheet.Name
Next oSheet
Dim lastrow As Long
lastrow = refSheet.Cells(Rows.Count, 1).End(xlUp).Row
With refSheet.Columns(1)
Range(Cells(1, 1), Cells(lastrow, 1)).Select
Selection.CreateNames Top:=True
End With
Sheet1.ComboBox2.RowSource = "Name"
End Sub
Refer to the sheet:
lastrow = refSheet.Cells(Rows.Count, 1).End(xlUp).Row
should be:
lastrow = refSheet.Cells(refSheet.Rows.Count, 1).End(xlUp).Row
When you use "With" here:
With refSheet.Columns(1)
Range(Cells(1, 1), Cells(lastrow, 1)).Select
Selection.CreateNames Top:=True
End With
you should actually use it with the dots:
With refSheet.Columns(1)
.Range(.Cells(1, 1), .Cells(lastrow, 1)).Select
Selection.CreateNames Top:=True
End With
(Notice the dots prior to range and cells)
Not using dots in the "With" block refers to the ActiveSheet

Resources